Summary of Java Day 20

Keywords: Java


1.1 Hash list

Hash List: You can understand that saving elements in an array is a chain table
Time-key-value pairs (K and V) saved in a Hash list  
The hashCode:hash algorithm, which changes variable-length data to fixed-length data, is a secure encryption algorithm but does not guarantee uniqueness.
If the same object generates hash values more than once, the values must be the same
It is also possible for different objects to generate the same hash value

Add process:
1 Call the added K first, call hashCode to generate hash value
2 Calculate array subscript based on hash value
3 Determine if there are elements in the data where the subscript corresponds
3.1 If no data is saved, place the object in the corresponding subscript
3.2 If the data is saved, the equals method of the added K is called and compared with the key s of all data corresponding to that subscript in the array
3.3 If the data in the chain table corresponding to the array subscript is not equal, add the data to the corresponding chain table
3.4 If it is consistent with the data in the list, the key is not added and the value is replaced (with the new one)
4 Java 1.8 new change, if the number of nodes in the list is greater than 7, the list is converted to a red-black tree

In java, there is no hash list, just hash lists are encapsulated as HashMap and HashTable, and HashTable is obsolete
And HashMap has a default capacity of 16

At the bottom of the HashSet is a HashMap, and it's just the key part of the Map, no value

1.2 hashSet use

public static void main(String[] args) {
		HashSet<String> set = new HashSet<String>();


2.1 Inheritance System


2.2 Map features

Map: Unordered, key not repeatable, value repeatable
Map and Collection are different, but the operations are basically the same
Collections hold individual objects, while maps hold key-value pair mappings

2.3 Common Methods

put(K,V): Add data
remove(K): Delete data
clear(): Empty
size(): number
isEmpty(): Determine if it is empty
get(K): Get value from Key
values(): Get all value s, return the set
containsKey(K): Determine if a key is included
containsValue(V): Determine if a value is included
Set keySet(): Gets all keys in the map, returns set
Set entrySet(): Gets the key-value pair in the map, returns set

2.4 HashMap

public static void main(String[] args) {
		// Create a map
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("a", 1);
		map.put("a", 2);
		map.put("A", 21);
		map.put("'A'", 22);
		map.put("65", 122);
		// Number 1
		// Get value from key, 2 because key repeats, value replaces
		// Does it contain a key
		// Whether to include a value
		// Delete the mapping relationship based on the key (K and V are deleted, this node is deleted in the chain table)
		// map cannot be traversed directly
		// Get all value s
		Collection values = map.values();
		for (Object object : values) {
		// Get all key s
		Set<String> sets = map.keySet();
		for (String key : sets) {
			System.out.println(key + ":" + map.get(key));
		// Convert map to set, encapsulate key and value in the entry class object, and save the entry class object in the set
		Set<Entry<String, Integer>> entries = map.entrySet();
		for (Entry<String, Integer> entry : entries) {
			// A=21
			// getKey is to get key,getValue is to get value
			System.out.println(entry.getKey() + "->" + entry.getValue());

2.5 Properties

  Properties: key and value mandatory must be strings

public static void main(String[] args) {
		Properties p = new Properties();
		// Add data
		p.setProperty("driver", "mysql");
		p.setProperty("username", "root");
		// get data
		// Non-existent key gets null
		// There is a method overload, and the second parameter is the default value, which is returned instead of null if no data is found based on the key
		// The key-value pair will not be added
		System.out.println(p.getProperty("qweqw","Default value"));

2.6 TreeMap

TreeMap: Saved elements can be sorted according to certain rules
1 The element to be added implements the Comparable interface
2 Write comparator class to implement Comparator interface
treeMap automatically calls the compareTo method of the key object when it is added, comparing with the key instead of the value

public static void main(String[] args) {
		TreeMap<Integer, String> map = new TreeMap<Integer, String>(
				new Comparator<Integer>() {
					public int compare(Integer o1, Integer o2) {
						return o2-o1;
		map.put(1, "a");
		map.put(2, "a");
		map.put(12, "a");
		map.put(11, "a");

		Set set = map.entrySet();
		for (Object object : set) {

2.7 Interview Questions

Map goes to list and sorts by value

public static void main(String[] args) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("a", 1);
		map.put("bbb", 2);
		map.put("bcd", 1);
		map.put("a1", 12);
		// Encapsulate K and V into entry and save to set
		Set<Entry<String, Integer>> set = map.entrySet();
		// set to list
		List<Entry<String, Integer>> list = new ArrayList<Map.Entry<String,Integer>>(set);
		// Change Sort
		Collections.sort(list, new Comparator<Entry<String, Integer>>() {
			public int compare(Entry<String, Integer> o1,
					Entry<String, Integer> o2) {
				// Compare by value
				return o1.getValue() - o2.getValue();

Posted by bufo on Mon, 25 Oct 2021 09:27:48 -0700