Summary of the Relationship among Collection List Set Map in Java

  • 2021-06-29 10:59:33
  • OfStack

Beginner java, individual contacts are a little confused, so summarize their relationship

1. Relationships

Collection

--List: Stored in a specific order

--ArrayList, LinkList, Vector

--Set: Cannot contain duplicate elements

--HashSet, TreeSet

Map

--HashMap, HashTable, TreeMap

2. Explain separately

Collection:Collection is a parent interface, List and Set are inherited from their subinterfaces, Collection is the most basic collection interface. Java SDK does not provide classes directly inherited from Collection, but provides classes inherited from other subinterfaces, such as List What Set.The Collection class used supports one Iterator() Method traversal.

The List:List interface is ordered and inserts elements precisely into the specified location. Unlike the following Set interface, the List interface allows the same elements

ArrayList: Implements a variable-size array that allows all elements to be synchronized, that is, there is no synchronization method

LinkList: Allows null elements, usually operating at the beginning or end, so they are often used as stacks (stack), queues (queue), and two-way queues (deque)

Vector: Similar to ArrayList, but Vector is synchronous, Stack inherits from Vector

Set: is an Collection interface that does not contain duplicate elements

HashSet: There can be no duplicate elements, the bottom layer is implemented using HashMap

Map: This interface implements the mapping of Key to Value, one Map cannot contain the same Key, and each Key can only map one Value

HashTable: An Key-Value hash table has been implemented, each non-null element can be used as Key or Value, HashTable is synchronous

HashMap: Unlike HashTable, it is asynchronous and allows the null element to exist

3. Array and Arrays Collection and Collections

Array: is therefore the most efficient way to randomly access a list of objects, but the element types must be the same and the capacity cannot be changed.

Arrays: This static class operates specifically on array, providing static methods such as search, sort, copy, and so on

An interface under Collection:Java.util that is the parent of various collection structures

Collections: A dedicated static class under Java.util that contains static methods for various collection operations, including search, sort, thread security, and so on.

4. Summary

With the List interface, LinkList should be used for fast insertion and deletion, and ArrayList for random access elements.

Single-threaded use asynchronous classes, multi-threaded use synchronous classes

Note the operation on HashTable to override the equals and hashCode methods as objects of Key

HashMap is used for fast lookup in various Maps

The longest used are ArrayList, HashSet, HashMap, Array

Below is an example of finding the longest string in an element string array


package Collection;
import java.util.Arrays; 
import java.util.Collections; 
import java.util.Comparator; 
import java.util.List; 
public class MasString {
 public static void main(String args []){
 String str[] = new String[]{"zhans", "lis", "wangwt", "quliu"};
 List<String> list = Arrays.asList(str);
 String max = Collections.max(list, new strSort()); // Collections Use of static functions 
 System.out.println("max: "+max);
 }
}
class strSort implements Comparator<String>{
<span style="white-space:pre"> </span>// 1 Comparator redefinition 
 public int compare(String s1, String s2){
 if(s1.length() < s2.length()){
  return -1;
 }
 if(s1.length() < s2.length()){
  return s1.compareTo(s2);
 }
 else return 1;
 }
}

Related articles: