Mainly divided into collection and map,collection. Gets the saved object reference using the index, which we can think of as an array-like class, while map stores the object as a key-value pair, and each object has a key value corresponding to it when it is stored in the map. Collection is the top-level interface of the collection class, and its directly inherited interfaces have set and list. Collections is a tool class/helper class for the collection Class , which provides a series of static methods for sorting, searching, and line Cheng of elements in the collection.
Map: A bunch of key-value pairs of data that require keys in each key-value pair to not be duplicated
The map type container is a key-value pair to save the object, and sometimes we call it a dictionary, creating a container of the map type can use any class that implements the map interface.
Map map = new HashMap ();
To add data to a map you can use the Put method, which accepts two parameters: the key and the value.
Map.put ("Key", 10);
You can use the Get method if you want to get a value for a key in the map.
Map.get ("key");
HashMap
Common Operations
Add: Put ()
Remove: Remove () Clear ()
Modified: Put ()
Search: Get ()
Get element Key set: KeySet ()
Get element value set: VALUES ()
Iteration
Foreah
Iterator
Determine if key exists: ContainsKey ()
Determine if the value exists: Containsvalue ()
List: A stack of ordered, repeatable data that can be retrieved using subscript
The list collection is very similar to the use of arrays, which are used to store large amounts of data, with two points in different places:
1, the length of the array must be determined before use, once determined can not be changed. The list collection is variable in length and does not need to be defined.
2, the array must hold the same type of data, the list collection can hold different types of data.
1 Public classlistarray{2 Public Static voidMain (string[] args) {3System.out.println ("Please enter the name of the learner and end with over");4Scanner Scanner =NewScanner (system.in);5ArrayList list =NewArrayList ();6 Do{7String name =Scanner.next ();8 if(Name.equalsignorecase ("over")) Break;9 List.add (name);Ten} while(true); One System.out.println (list); A } -}
The list collection holds reference types that can be obtained with coercion type conversions.
JDK 1.5 ago
List.add (New Integer (10));
int i = (Integer) list.get (0). Intvalue ();
After JDK 1.5, the automatic boxing unpacking feature is available in Java, using
List.add (10);
int i = (ingeter) list.get (0);
Common operations:
List Collection
2.1, ArrayList: Based on the implementation of the array, the retrieval is faster, the increase and deletion is slow, high efficiency.
2.2, LinkedList: Based on linked list implementation, retrieval is slow, add and delete quickly.
2.3, Vector: Thread-safe collection, low efficiency.
2.4. Common operation
Added: Add ()
Remove: Remove () Clear ()
Modify: Set ()
Search: Get ()
Iteration:
for (subscript)
Foreach
Iterator
Judging existence: Contains ()
Converts a collection to an array: ToArray ()
The list collection overrides the ToString method to output the elements in the collection sequentially.
1 Importjava.util.ArrayList;2 Importjava.util.Collection;3 ImportJava.util.Iterator;4 ImportJava.util.Scanner;5 6 Public classListarray {7 Public Static voidMain (string[] args) {8System.out.println ("Please enter the name of the class school and end With over");9Scanner Scanner =NewScanner (system.in);TenArrayList list =NewArrayList (); One Do { AString name =Scanner.next (); - if(Name.equalsignorecase ("Over")) - Break; the List.add (name); -} while(true); - System.out.println (list); + } - +}
List Collection Common methods
1 Importjava.util.ArrayList;4 ImportJava.util.Scanner;5 6 ImportJavax.swing.text.html.HTMLDocument.Iterator;7 8 Public classDemo {9 Public StaticScanner Scanner =NewScanner (system.in);Ten One Public Static voidMain (string[] args) { AArrayList ListA =NewArrayList (); -ArrayList Listb =NewArrayList (); -System.out.println ("Please enter the name of a participant, enter end with over"); the InputName (ListA); -System.out.println ("Please enter the name of Student B, enter the end of over"); - InputName (LISTB); - +System.out.println ("Please enter the name of the learner you want to find"); -String name =Scanner.next (); + intpos =Lista.indexof (name); A if(pos = =-1) { atSystem.out.println ("Not Found"); -}Else { -System.out.println ("Found, location is" +POS); - } -System.out.println ("Please enter the name of the academy to be deleted"); -String Dename =Scanner.next (); in if(Lista.remove (dename)) { -System.out.println ("Delete succeeded"); to}Else { +System.out.println ("No student"); - } the } * $ Public Static voidinputname (ArrayList list) {Panax Notoginseng Do { -String name =Scanner.next (); the if(Name.equalsignorecase ("Over")) + Break; A List.add (name); the} while(true); + } -}
Set set: A bunch of unordered and not repetitive data
There are many usages of the set set and list set, but the elements in the set set are unordered, the elements cannot be duplicated, and the common classes in the set set are HashSet
Common operations
Added: Add ()
Remove: Remove () Clear ()
Modified: Add ()
Iteration:
Foreach
Iterator
Judging existence: Contains ()
Returns the number of elements in the collection: size ()
The list collection can be indexed with a for loop. But set must be done through iterators.
1 ImportJava.util.HashSet;2 ImportJava.util.Iterator;3 4 Public classSetiterator {5 Public Static voidMain (string[] args) {6HashSet set =NewHashSet ();7Set.add ("a");8Set.add ("B");9Set.add ("C");TenSet.add ("D"); OneIterator Iterator =set.iterator (); A while(Iterator.hasnext ()) { -String str =(String) Iterator.next (); - System.out.println (str); the - } - } -}
Java Collections and classifications