[Java] summary of Java collection

More articles welcome the official account of "three Heng Lan". It can be seen from the picture that there ar...
Collection collection
Map collection

More articles welcome the official account of "three Heng Lan".

It can be seen from the picture that there are two main types of Java collections. One is the Collection interface, which stores a single element; The other is the Map interface, which stores key value pair elements.

Collection collection

Common interfaces include List, Set and Queue

  • List: the stored elements are ordered and repeatable. If there are requirements for sequence, use the modified interface
  • Set: the stored elements are unordered, unique and cannot be repeated. Can be used for de duplication
  • Queue: the stored elements are ordered and repeatable.

List

  • ArrayList: the Object [] array is used to store data. The search function is convenient and the thread is unsafe
  • Vector: the Object [] array is used to store data, and vector is used for thread safety
  • LinkedList: the two-way linked list data structure is used to store data. Inserting and deleting data is more efficient and non thread safe than ArrayList

Set

  • HashSet: storing data structures and data operations are mainly realized through HashMap, and threads are unsafe. The HashSet judges that the value is unique. First obtain the hash value through the hashCode() function to judge whether it is equal or not. If it is equal, then call the equals method to judge.

  • LinkedHashSet: inherits the HashSet. The difference from HashSet is that the elements of HashSet are unordered, while LinkedHashSet uses linked list to maintain the order of elements. When accessing elements, it will access the elements of the set in the order of insertion

    public class Test { public static void main(String[] args) { Student student1 = new Student("student1"); Student student2 = new Student("student2"); Student student3 = new Student("student3"); HashSet<Student> hashSet = new HashSet<>(); hashSet.add(student1); hashSet.add(student2); hashSet.add(student3); LinkedHashSet<Student> linkedHashSet = new LinkedHashSet<>(); linkedHashSet.add(student1); linkedHashSet.add(student2); linkedHashSet.add(student3); printSet(hashSet); printSet(linkedHashSet); } public static void printSet(Set<Student> set) { System.out.println(set.getClass()); for (Iterator iterator = set.iterator(); iterator.hasNext();) { Student student = (Student) iterator.next(); System.out.println(student.name); } } }

    Printed results:

    class java.util.HashSet student3 student1 student2 class java.util.LinkedHashSet student1 student2 student3
  • TreeSet: implements the SortSet interface. It can be seen that the elements of the set are in sorting status. The objects placed in TreeSet need to implement the Comparable interface and override its compareTo() method. There are natural sorting and custom sorting. Natural sorting is used by default. If the elements are not custom objects, they will be sorted in ascending order.

    public class Test { public static void main(String[] args) { Student student1 = new Student("student1"); Student student2 = new Student("student2"); Student student3 = new Student("student3"); // Natural sorting TreeSet<Student> treeSet = new TreeSet<>(); treeSet.add(student1); treeSet.add(student3); treeSet.add(student2); printSet(treeSet); // Custom sorting TreeSet<Student> treeSet2 = new TreeSet<>(new StudentComparator()); treeSet2.add(student1); treeSet2.add(student3); treeSet2.add(student2); printSet(treeSet2); } public static void printSet(Set<Student> set) { System.out.println(set.getClass()); for (Iterator iterator = set.iterator(); iterator.hasNext();) { Student student = (Student) iterator.next(); System.out.println(student.name); } } } class Student implements Comparable{ public Student(String name) { super(); this.name = name; } String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int compareTo(Object o) { // Ascending arrangement return this.name.compareTo(((Student)o).getName()); } } class StudentComparator implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { // Descending order return o2.getName().compareTo(o1.getName()); } }

    Printed results:

    class java.util.TreeSet student1 student2 student3 class java.util.TreeSet student3 student2 student1

Map collection

  • HashMap: non thread safe; null keys and values can be stored. There can only be one key and countless values
  • HashTable: thread safety; null key and value are not allowed. Because of the problem of thread safety, HashTable is slower than HashMap, and concurrent HashMap can be used if thread safety is considered. HashTable inherits Dictionary, which has been abandoned, so HashTable, as a subclass, has gradually been put in the cold, so use it with caution
  • LinkedHashMap: similar to the relationship between HashSet and LinkedHashMap.
  • TreeMap: TreeMap implements NavigableMap and SortedMap interfaces more than HashMap. Literally, you know that HashMap has more key sorting and search capabilities than HashMap. It is mainly used for sorting

A small summary of Java collections. I will learn the specific usage of each collection in detail later.

Snail speed learning, slow growth-

More articles welcome the official account of "three Heng Lan".

22 October 2021, 09:35 | Views: 7035

Add new comment

For adding a comment, please log in
or create account

0 comments