The Java Collection Framework provides a set of well-performing, easy-to-use interfaces and classes that are located in the Java.util package.
Collection interface mainly has list, set and other implementation classes, map interface is mainly HashMap, treemap and other implementation classes. Collections provides a variety of algorithm implementations for sorting, traversing, and so on.
The following is the use of Set collections: Store unique, unordered objects. Derived class HashSet.
Set set=New HashSet (); Set.add ("Friday"); Set.add (199); Set.add (23.55); Set.add (100f);
Output using the Foreach method:
for (Object object:set) { System.out.println (object); }
It should be noted that the set interface does not have a GET method, so the output cannot be traversed with a for loop.
Using iterators to take values
Iterator its = set.iterator (); while (Its.hasnext ()) { // Enter the element in the loop output collection Object obj = its.next (); System.out.println (obj); }
The result of the program operation is:
23.55 Friday 100.0199-------------------23.55 Friday 100.0199
TreeSet implementation of sorting functions
Set set=New TreeSet (); Set.add ("2EE"); Set.add ("Jsk"); Set.add ("SDA"); Set.add ("WQL"); Set.add ("SDJ"); for (Object object:set) { System.out.println (object); }
Map interface
The map interface stores a set of key-value objects that provide a mapping of key to value.
Sample Requirements Description:
Create a collection of poems, iterate over the name of each poem, author, content,
Analysis:
Create a "poem" Class poem, with attributes such as: Name, author, content
Using HashMap to store the information of a poem, the name of the poem as key, and the content of the poem as value.
Solve:
First create the poem class, which contains the private property: name, author, content, and generate a corresponding get, set method, which generates the full parameter constructor and the parameterless constructor.
To assign a value to an array of objects in a test class:
Poem poem1=New Poem ("Dawn", "Meng Hao", "Spring sleep not dawn, everywhere smell birds." The night comes the wind and rain, the flowers fall to know how much. "); Poem poem2=new Poem ("Deer Chai", "Wang Wei", "The empty mountain is missing people, but Wenren language rang.") Return shadow into the Deep forest, re-shone on the moss. "); Poem poem3=new Poem ("Acacia", "Wang Wei", "Red bean born in Southern, wed hair a few sticks." May the Lord more pick, this thing most acacia. ");
Then create the HashMap function under Map,
Map<string, poem> map=new hashmap<string, poem> ();
Then add the data using Map.put:
Map.put ("Dawn", poem1); Map.put ("Deer Chai", poem2); Map.put ("Acacia", poem3);
Finally, an iterator is used to output:
Set keys== keys.iterator (); while (Its.hasnext ()) { string key=(String) its.next (); Poem Poem=map.get (key); System.out.println (Poem.getname () +poem.getauthor () +poem.getwenzhang ()); }
The result of the program operation is:
Acacia Wang Wei Red bean born south, Wed hair a few sticks. May the Lord more pick, this thing most acacia. Dawn Meng Hao Spring sleep not xiao, everywhere smell cry bird. The night comes the wind and rain, the flowers fall to know how much. The Deer Chai Wangwi the empty mountain, but the Wenren language rang. Return shadow into the Deep forest, re-shone on the moss.
Linedlist Operating Data
You can use LinkedList to improve efficiency when inserting and deleting operations frequently.
The following are common methods of operation:
void AddFirst (Object o) adds an element to the header of the list
void AddLast (Object o) adds an element at the end of the list
Object GetFirst () returns the first element in a list
Object GetLast () returns the last element in the list
Object Removefirst () deletes and returns the first element in a list
Object removelast () deletes and returns the last element in the list
The map interface specifically handles the storage of key-value mapping data, and can operate on values based on keys. The most common implementation class is HashMap.
The following are common methods of operation
Object put (Object key, Object val) is stored in a "key-value pair" manner
Object get (Object key) returns the associated value according to the key, or null if the specified key does not exist
Object remove (object key) deletes the "key-value pair" that is mapped by the specified key
int size () returns the number of elements
Set KeySet () returns a collection of keys
Collection values () returns a collection of value
Boolean ContainsKey (Object key) returns True if there is a "key-value pair" that is mapped by the specified key
Similarities and differences of Hashtable and HashMap
Hashtable inherits the Dictionary class, HashMap implements the map interface.
Hashtable thread safety, HashMap thread non-secure
Hashtable does not allow null values, HASHMAP allows null values.
What the Java Collection Framework contains