Java Collection, java Collection class details
Collection
Collection is an interface that allows a set to contain duplicate elements or prohibit repeated elements. You can force or limit the order. The following describes some common basic methods in Collection.
1 Collection <String> hashSet = new HashSet <> (); 2 hashSet. add ("hello Collection"); // add element 3 Collection <String> arrays = Arrays. asList ("a", "B", "c"); 4 hashSet. addAll (arrays); // Add a collection of 5 systems. out. println (hashSet. size (); // obtain the number of elements in the set. 6 System. out. println (hashSet. contains ("B"); // whether the set contains an element 7 System. out. println (hashSet. containsAll (arrays); // whether the set contains a group of elements 8 Object [] elments = hashSet. toArray (); // The array 9 String [] strings = hashSet. toArray (new String [0]); // converts 10 hashsets to a specified element. remove ("a"); // delete a single element 11 hashSet. removeAll (arrays); // deletes a group of elements.
Set
Set cannot have two references pointing to the same object or null, even. equals (B) = true does not work because it is a Set composed of Non-repeated objects. If an existing element is added to the Set, no exception is thrown, but a Boolean value false is returned.
The SortedSet interface of the inherited Set further expands the Set method.
1 SortedSet <String> treeSet = new TreeSet <> (); 2 treeSet. add ("a"); 3 treeSet. add ("B"); 4 treeSet. add ("c"); 5 System. out. println (treeSet. first (); // obtain the first element 6 System in the set. out. println (treeSet. last (); // get the last element in the set 7 // return all elements except the first element 8 SortedSet <String> tail = treeSet. tailSet (treeSet. first () + '\ 0'); 9 System. out. println (tail); 10 // returns all elements except the last one. 11 SortedSet <String> head = treeSet. headSet (treeSet. last (); 12 System. out. println (head); 13 // intercept all the elements from the nth element to the M element. 14 SortedSet <String> middle = treeSet. subSet (treeSet. first () + '\ 0', treeSet. last (); 15 System. out. println (middle );
List
Unlike a Set, a List is an ordered object Set that can contain duplicate elements. The size of a List varies with the number of elements, unlike an array, list also inherits the Collection method and extends some methods.
1 java. util. list <String> list = Arrays. asList ("Hello", "Java"); 2 java. util. list <String> arrayList = new ArrayList <> (list); 3 System. out. println (arrayList. get (0); // obtain the array element 4 arrayList through the index. set (0, "Hi"); // set the element 5 System of the set through the index. out. println (arrayList. get (0); 6 arrayList. add (1, "Bob"); 7 System. out. println (arrayList); // Insert the element 8 System through the index. out. println (arrayList. indexOf ("Bob"); // query the location of an element 9 System. out. println (arrayList. lastIndexOf ("Java"); // reverse query the position of an element 10 arrayList. retainAll (list); // Delete the 11 System elements that are different from the list. out. println (arrayList); 12 arrayList. clear (); // clear all elements 13 System. out. println (arrayList );
Map
Map is a series of key-value pairs. A key corresponds to a value. Map can be considered as a Set composed of Set and Collection. The Map key is like a Set and does not allow repeated elements, let's take a look at some common Map methods.
1 Map <Integer, String> map = new HashMap <> (); 2 String [] words = {"a", "B", "cc "}; 3 for (int I = 0; I <words. length; I ++) {4 map. put (I, words [I]); // fill Map 5} 6 System by using the put method. out. println (map); 7 System. out. println (map. get (1); // obtain the 8 System value corresponding to 1. out. println (map. containsKey (5); // determines whether a key 9 System is contained. out. println (map. containsValue ("B"); // determines whether a value is included. 10 // process the key and value of the ing relationship as a Set. 11 Set <Integer> keys = map. keySet (); 12 System. out. println (keys); 13 Collection <String> values = map. values (); 14 System. out. println (values); 15 for (Map. entry <Integer, String> elment: map. entrySet () {16 System. out. println (elment); 17} 18 map. remove (1); // Delete the key-Value Pair 19 System. out. println (map );
Common skills in Collection
You can perform some operations on the collection through the static method of the Collections class.
1 java. util. list <Integer> numbers = Arrays. asList (12, 5, 6, 8, 11, 4); 2 Collections. sort (numbers); // sort 3 System. out. println (numbers); 4 Collections. reverse (numbers); // reverse the 5 System. out. println (numbers); 6 Collections. shuffle (numbers); // disrupt order 7 System. out. println (numbers); 8 // maximum value, minimum value 9 Collections. max (numbers); 10 Collections. min (numbers );
Prevents concurrent access to a set
1 java.util.List<String> list=Collections.synchronizedList(new ArrayList<String>());2 Map<Integer, String> map=Collections.synchronizedMap(new HashMap<Integer,String>());
Read-only set
java.util.List<String> words=Collections.unmodifiableList(new ArrayList<String>());