java collections framework

Collection is a collection of objects. Collection has two sub-interfaces, List and Set
, which are unordered, unique and non-repeatable. Commonly used implementation classes are HashSet, TreeSet, and sometimes used for duplicate value filtering. Elements with a value of null are allowed, but there can be at most one null element.
Lists are ordered and can be repeated. Commonly used implementation classes include ArrayList and LinkedList. The data storage method and query method (array subscript) are similar to arrays for easy search.
Maps appear as keys and values. Commonly used implementation classes are HashMap, TreeMap.

List can get the value by subscript (1,2..), the value can be repeated

, and Set can only get the value through the cursor, and the value is

ArrayList, Vector, LinkedList is the implementation class of List

ArrayList is thread-unsafe Yes, Vector is thread-safe. The bottom layer of these two classes is implemented by an array.

LinkedList is thread-unsafe, and the bottom layer is an  

array implemented by a linked list, which stores elements continuously in memory.
The elements in the linked list are not stored sequentially in memory, but are linked together by pointers stored in the elements.


The most commonly used are ArrayList, HashSet, HashMap, Array.

Map usage
  Map<String,Object>  map = new HashMap<String,Object>();
  //adding data
  map.put("key","value");
[size=small]Value method[/size]
  //The first type: secondary value (traverse key and value through Map.keySet)
  for (String key : map.keySet()) {
   System.out.println("key= "+ key + " and value= " + map.get(key));
  }

  //The second: entrySet-iterator (traversing key and value through Map.entrySet using iterator)
  Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
  while (it.hasNext()) {
   Map.Entry<String, String> entry = it.next();
   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
  }
  
  //The third type: entrySet (recommended when Map.entrySet traverses key and value with large capacity)
  System.out.println("traverse key and value through Map.entrySet");
  for (Map.Entry<String, String> entry : map.entrySet()) {
   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
  }

  //The fourth type: .values() (traverse all values ​​through Map.values(), but cannot traverse keys)
  for (String v : map.values()) {
   System.out.println("value= " + v);
  }

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326988288&siteId=291194637