hashmap,treemap

Implementation class of Map collection, HashMap

HashMap

Storage structure: hash table (array + linked list + red-black tree)

The thread is not safe, the operation efficiency is fast, and the use of null, key, value is allowed

The default initial capacity is 16, and the default load factor is 0.75 (when the used capacity exceeds 75% of the total capacity, the capacity will be expanded)

hashmap

Create a collection:

HashMap<Student,String> hashMap = new HashMap<Student, String>();

The key is the Student class, and the value is the String class

Judgment is repeated

Remove duplication when storing elements, rewrite hashcode and equals

Student s1 = new Student("张三",1001);
Student s2 = new Student("李四",1002);
Student s3 = new Student("王五",1003);

hashMap.put(s1,"北京");
hashMap.put(s2,"上海");
hashMap.put(s3,"广州");
//重写Student类中的hashcode和equals方法,判断重复
hashMap.put(new Student("张三",1001),"北京");
hashMap.contains(new Student("张三",1001),"北京")

Traverse:

foreach traversal:

//遍历1.keyset
for (Student key:hashMap.keySet()
     ) {
    
    
    System.out.println(key+"--"+hashMap.get(key));
}
System.out.println("------------");
//遍历2.entryset
for (HashMap.Entry<Student,String> kv:hashMap.entrySet()
     ) {
    
    
    System.out.println(kv.getKey()+"--"+kv.getValue());
}

Iterator traversal:

temp = iterator.next() needs to be assigned a temporary value. If you use iterator.next() directly as a key , you need to pay attention to the type of temp when assigning a value. In the keyset, the iteration is the key value, so the type of temp is Student.

In entryset, iterating is the encapsulated key: value value (Entry), so the type of temp is

HashMap.Entry<Student,String>

//Set<Student> key = hashMap.keySet();
Iterator<Student>it =hashMap.keySet().iterator();
while (it.hasNext()){
    
    
    Student keytemp =it.next();
    System.out.println(keytemp+"--"+hashMap.get(keytemp));
}
//entryset的迭代
Iterator<HashMap.Entry<Student,String>>iterator = hashMap.entrySet().iterator();
while (iterator.hasNext()){
    
    
    HashMap.Entry tempentry = iterator.next();
    System.out.println(tempentry.getKey()+"--"+tempentry.getValue());
}

HashMap source code analysis?

  1. When the HashMap is first created, the table is null. In order to save space, when the first element is added, the table capacity is adjusted to 16.
  2. When the number of elements is greater than the threshold (16*0.75), the expansion will be carried out, and the expansion will be twice the original size. The purpose is to reduce the number of adjustment elements.
  3. jdk1.8 When the length of each linked list is greater than 8 and the number of elements is greater than or equal to 64, it will be adjusted to a red-black tree to improve execution efficiency for the purpose
  4. When the length of the linked list is less than 6, adjust to the linked list
  5. Before jdk1.8, the linked list was inserted at the head, and then at the end.

HashSet和HashMap

The bottom layer of HashSet is actually HashMap, which uses the key part of HashMap

HashTable

Thread safety, slow operation efficiency, null is not allowed as key or value

Subclass Properties

A subclass of HashTable requires both key and value to be String, which is usually used to read configuration files.

Save in the stream, or load from the stream

TreeMap

Implemented the SortedMap interface (a sub-interface of Map), which can automatically sort keys

The bottom layer is a red-black tree. The left node is smaller than the right node

Same as TreeSet, it is necessary to formulate comparison rules, that is, to implement the Comparable interface

public class Person implements Comparable<Person> 

And rewrite the compareTo method

@Override
public int compareTo(Person o) {
    
    
    int n1 = this.getName().compareTo(o.getName());
    int n2 = this.getStudentnum()-o.getStudentnum();
    return n1==0?n2:n1;
}

Guess you like

Origin blog.csdn.net/weixin_43903813/article/details/112298901