001. An overview of the
A Collection is a Java container. It is located under the java.util package and consists of Collection(interface), Map(interface), and Collection utility classes.
Set structure diagram
003. The List interface
0031. The characteristics of
- An ordered set that allows repeating elements
0032. Two main implementation classes:
- ArrayList: The underlying array-based memory address is contiguous
- LinkedList: the underlying LinkedList is based on a bidirectional LinkedList, with discontinuous memory addresses
0033.ArrayList
- Elements can be one or more NULL;
- The default initial capacity is 10. When the array size is insufficient, the capacity is increased by 1.5 times.
List<String> list = new ArrayList<>();
list.add("hua");
list.add("ming");
list.add("fan");
list.add(null);
list.add("hua"); list.add(null); System.out.println(list); // output [hua, Ming, fan, null, hua, null]Copy the code
0034.LinkedList
- The element can be one or more NULL
- Implement both List and Deque interfaces
0035.ArrayList and LinkedList
Void add(E element) Add element void Add (int I,E element) Add element E get(int I) Get element Eset(int I,E element) Replace the element E remove(int I) Remove the element int size() Number of elements Boolean isEmpty() There are no elements in the collection, returntrue
Copy the code
0036. Three ways to traverse
Public static void main(String[] args) {List<Integer> List = new ArrayList<>(); public static void main(String[] args) {List<Integer> List = new ArrayList<>();for(int i=0; i<10; i++){ list.add(i); } System.out.println(list); Iterator<Integer> it = list.iterator();while(it.hasNext()){
System.out.print(it.next() +""); } System.out.println(); // Traversal: enhancedforcyclefor(Integer n:list){
System.out.print(n+""); } System.out.println(); / / traverse:for
for(int i=0; i<list.size(); i++){ System.out.print(list.get(i)+""); }}Copy the code
0037. Application Scenarios
- ArrayList is better for query operations;
- LinkedList is better for insert and delete operations;
004. The Set interface
0041. The characteristics of
- An unordered collection with no repeating elements allowed
0042. The implementation class
- HashSet: The underlying implementation is based on HashMap
- TreeSet: The underlying implementation is based on binary trees
0043.HashSet
- Only one NULL element is allowed
Set<String> sh = new HashSet<>();
sh.add("fan");
sh.add(null);
sh.add("hua");
sh.add("ming");
sh.add("hua"); // Filter out sh.add(null); System.out.println(sh); //[null, fan, hua, ming]Copy the code
005. The Map interface
0051. The characteristics of
- A collection of mapped relationships
- Data is stored as key-value pairs
0052. Two main implementation classes
- HashMap: Underlying based on array + list (JDK7 and before), array + list + red-black tree (JDK8)
- HashTable: underlying HashTable based
0053.HashMap
- Keys are unique, values can be more than unique
- Null keys and null values are allowed
- The Entry objects are sorted out of order
- The default initial capacity is 16 and the default load factor is 0.75
0054. Common methods of HashMap
V PUT (Object key,Object value) Add V GET (Object key) Query V Remove (Object key) Remove int size() lengthCopy the code
Code 0055.
Map<Integer,String> map = new HashMap<>();
map.put(1, "wang");
map.put(2, "li");
map.put(3, "hua");
map.put(4, null);
map.put(5, null);
map.put(2, "fan"); // override the value map.put(null, null) for the same key; // The key value can be empty system.out.println (map); //{null=null, 1=wang, 2=fan, 3=hua, 4=null, 5=null}Copy the code
0056. Traversal mode
// Method 1: iteratefor(Integer i:map.keySet()){
System.out.println("key:"+i+" values:"+map.get(i)); } // Method 2: Iterator<Entry<Integer,String>> list = map.entryset ().iterator();while(list.hasNext()){
Entry<Integer,String>entry = list.next();
System.out.println("key:"+entry.getKey()+" values:"+entry.getValue());
}
Copy the code
006. The utility class
- Collections: Collections helper classes provide sorting, searching, and thread-safe operations on collection elements.
- Comparable, Comparator: Generally used to compare objects to achieve a sort.