The Java Collection Framework provides a set of well-performing, easy-to-use interfaces and classes that are located in the Java.util package
First, collections and arrays
array: (the base data type can be stored) is a container for storing objects, but the length of the array is fixed and not suitable for use when the number of objects is unknown. (reproduced in other blogs https://www.cnblogs.com/leeplogs/p/5891861.html)
Collection: (objects can only be stored, and object types may be different) with variable lengths, which can be used in most cases. (reproduced in other blogs https://www.cnblogs.com/leeplogs/p/5891861.html)
Collection interface stores a set of non-unique, unordered objects
The list interface stores a set of objects that are not unique, ordered (in order of insertion)
The set interface stores a unique, unordered set of objects
ArrayList: implements a variable-length array that allocates contiguous space in memory. Traversing elements and random-access elements is relatively efficient
LinkedList: use linked list storage. High efficiency when inserting and deleting elements
  Span style= "font-size:18px" >vector and arraylis Similarities and differences
implementation principle, function is the same, can be interoperable
main differences
vector thread safety, ArrayList heavy Speed light thread non-secure
Similarities and differences of hshtable and HashMap
The principle of implementation, the same function, can be interoperable
Main differences
Hashtable inheriting Dictionary class HashMap implementing the Map interface
Hashtable thread safety,Hashmap thread non-secure
Hashmao null values are not allowed,Hashtable allows null values
It is best to use ArrayList and HashMap in practical development.
Collections is equivalent to a tool class that provides a series of methods such as sorting and so on java.util.Collections package
1 PackageTEXT;2 3 Importjava.util.ArrayList;4 Importjava.util.Collections;5 ImportJava.util.Random;6 7 Public classText {8 //Sort9 Public Static voidstringlist () {TenArraylist<integer> stringlist =NewArraylist<>(); OneRandom random =NewRandom (); A intLi; - for(inti = 0; I < 10; i++) { - Do { theLi = Random.nextint (10); -} while(Stringlist.contains (LI)); - Stringlist.add (LI); -System.out.println ("Add successfully" +Li); + } -System.out.println ("\ n" ++ "==================== sort before =======================" + "\ n"); A for(inti = 0; I < stringlist.size (); i++) { at System.out.println (Stringlist.get (i)); - } -System.out.println ("\ n" -+ "==================== sort after =======================" + "\ n"); - Collections.sort (stringlist); - for(Integer integer:stringlist) { in System.out.println (integer); - } to } + - /** the * @paramargs * */ $ Public Static voidMain (string[] args) {Panax Notoginseng { - { the text.stringlist (); + } A } the } +}
Some of the more common methods in the following collections
Map coutries=new HashMap ();
add put (object key name, object value)
coutries.put (", " "");
get value by key
string conter= (String) coutries.get ("Fill in the key value");
displays the number of items in the collection
coutries.size ();
determine if a key name exists (returns a bool value)
Boolean Bool=coutries.containskeys ("Key Name")
determine if a value exists (returns a bool value)
Boolean bool=coutries.containsvalue ("value")
To delete a collection object
coutries.remove ("Key Name");
Key Set
Coutries.keyset ();
Value Set
coutries.values ();
Emptying the Coutries collection
Coutries.clear ();
Iterators are used only for single-column collection double-row collections to use iterators to replace a two-column collection with a singleton set
PackageDay03map;Importjava.util.Collection;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.Map;ImportJava.util.Map.Entry;ImportJava.util.Set;/**March 10, 2018*/ Public classDay01 { Public Static voidMain (string[] args) {Map<string, string> map =NewHashmap<string, string>(); Map.put ("CBC", "China Construction Bank"); Map.put ("ABC", "Agricultural Bank of China"); Map.put ("ICBC", "ICBC"); //double-column collection traversal mode cannot use for subscript because he has no subscript.//mode one: for: Traversing value through the Keys collectionSet<string> set =Map.keyset (); for(String key:set) {System.out.println (key); //Key has, how is value obtained? System.out.println (Map.get (key)); } System.out.println ("======================="); //mode two: Traverse value directlyCollection<string> Collection =map.values (); for(String value:collection) {System.out.println (value); } System.out.println ("=========================="); //method Three: Key and valueset<entry<string, string>> entryset =Map.entryset (); for(Entry<string, string>Entry:entryset) {System.out.println (Entry.getkey ()); System.out.println (Entry.getvalue ()); } //scenario four: Using iterators to traverseSystem.out.println ("==========================="); Set<String> KeySet =Map.keyset (); Iterator<String> iterator =Keyset.iterator (); //iterators require manual iterations and need to use while while(Iterator.hasnext ()) {String key=Iterator.next (); SYSTEM.OUT.PRINTLN (key); System.out.println (Map.get (key)); } System.out.println ("The Last Supper =============."); //scenario Five: Using iterators to traverse key+value while traversingiterator<entry<string, string>> Iterator2 =Map.entryset (). iterator (); //you have to use a while loop to see iterator. while(Iterator2.hasnext ()) {Entry<string, string> entry =Iterator2.next (); System.out.println (Entry.getkey ()); System.out.println (Entry.getvalue ()); } }}The set class inherits the Conllection class, which is a collection class. The Set implementation class has three
The HashSet class accesses objects in the collection according to the hashing algorithm, and accesses it faster.
1.duplicate data is not present in set.
2.empty data can appear in set.
3.the data in set is unordered.
Linkedhashset
There is a big difference between this and hashset that Linkedhashset is orderly. Linkedhashset performance is better than hashset when iterating through all the elements in the set, but the performance is slightly inferior to hashset at insert time.
TreeSetis characterized by:1. Cannot write empty data2. The data written is ordered. 3. Do not write duplicate dataSummarize the collection
Defines a singleton collection by default to initialize 10 locations, but the number of elements is 0;
ArrayList The expansion principle is (by 3/2) +1 is the original capacity of 0.5 times times +1
The principle of Vector expansion is one times the original
HashMap: The default initial capacity of 16 expansion principle is one times the original
HashSet: The initial capacity is also 16 and its expansion is also the same as the original
The collection framework in JAVA