Java data structure

Posted by jason_kraft on Tue, 12 Nov 2019 16:28:42 +0100

  1. Hashtable
  2. Dictionary (Dictionary)
  3. Properties

1.Hashtable

Hash table provides a way to organize data based on user-defined key structure. In the hash table of the address list, the selection key can store and sort the data according to the zip code. The specific meaning of the hash table key depends entirely on the usage scenario of the hash table and the data it contains.

Located in the java.util package, hashtable is a specific implementation of Dictionary. Hashtable implements the Map interface. Therefore, hashtable is integrated into the collection framework. It is similar to the HashMap class, but it supports synchronization. Hashtable stores key / value pairs in the hash table. When using a hash table, you specify the object to use as the key and the value to link to it.

Construction method:

  1. Default construction method
    Hashtable()

     

  2. Create a hash table of the specified size
    Hashtable(int size)
  3. Created a hash table of the specified size, and specified the fill proportion through fillRatio
    Hashtable(int size,float fillRatio)
  4. Created a hash table with elements in M as initialization elements
    Hashtable(Map m)

Use:

package com.day6;

import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 * @author SFJ
 * @date 2019/11/12
 * @time 21:25
 **/
public class Test1 {
    public static void main(String[] args) {
        Hashtable hashtable = new Hashtable();//Create a hash table
        Enumeration names;
        String string;
        double b;
        hashtable.put("sang",new Double(666.66));
        hashtable.put("feng",new Double(999.99));
        hashtable.put("jiao",new Double(-111.11));
        names = hashtable.keys();
        while (names.hasMoreElements())
        {
            string =(String)names.nextElement();
            System.out.println(string+":"+hashtable.get(string));
        }
        System.out.println();
        b = ((Double)hashtable.get("sang")).doubleValue();
        hashtable.put("sang",new Double(b+100));
        System.out.println("sang's new hashtable value:"+hashtable.get("sang"));
    }
}

2.Dictionary

A Dictionary is an abstract class that defines the data structure of a key mapped to a value. When using Dictionary to access data through a specific key rather than an integer index. Because the Dictionary class is an abstract class, it only provides the data structure of key mapping to value, but does not provide a specific implementation

3.Properties

Properties inherits from the Hashtable.Properties class and represents a persistent property set. Each key in the property list and its corresponding value is a string. The properties class is used by many Java classes. For example, when you get an environment variable, it is the return value of the System.getProperties() method.

  1. Properties inherit from Hashtable. Represents a persistent property set. Each key in the property list and its corresponding value is a string.
  2. The Properties class is used by many Java classes. For example, when you get an environment variable, it is the return value of the System.getProperties() method.
  3. Properties defines the following instance variable. This variable holds a list of default properties related to the properties object.

Construction method:

  1. No parameter construction, default value is 10
    Properties()
  2. Use propDefault as the default. In both cases, the attribute list is empty
Properties(Properties propDefault)

Use:

package com.day6;

import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

/**
 * @author SFJ
 * @date 2019/11/12
 * @time 22:00
 **/
public class Test2 {
    public static void main(String[] args) {
        Properties properties = new Properties();
        Set set;
        String string;
        properties.put("sang","s");
        properties.put("feng","f");
        properties.put("jiao","j");
        set = properties.keySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext())
        {
            string = (String)iterator.next();
            System.out.println("The first letter of "+string+" is "+properties.getProperty(string));
        }
        System.out.println();
        string = properties.getProperty("liaocheng university","not found");
        System.out.println("The first letter of liaocheng university is"+string);
    }
}

Topics: Programming Java Attribute