In 1.Map we mainly talk about two interfaces HashMap and Linkedhashmap
(1) Where Linkedhashmap is ordered how to deposit how to take out
Let's talk about the map additions and deletions to check the function:
/** Add the Map collection*/Map<string, string> map =NewHashmap<string, string>(); Map.put ("Monday", "Monday"); Map.put ("Saturday", "Sunday"); SYSTEM.OUT.PRINTLN (map); /** Get values by building*/String String= Map.get ("Saturday")); System.out.println (string); /** by building modified values*/Map.put ("Monday", "Mon"); SYSTEM.OUT.PRINTLN (map); /** Delete Values by building*/String Remove= Map.Remove ("Saturday")); System.out.println (remove); SYSTEM.OUT.PRINTLN (map); }
The above is the map of the deletion and modification method
Traversal of 2.Map
1. The first type: Get all of the collection's built-in set<> collection through the Keyset () method
(1) Enhanced for
(2) iterators
map<string, character> map =new linkedhashmap<string, character> ();
Map.put ("One", ' 1 ');
Map.put ("Two", ' 2 ');
Map.put ("Three", ' 3 ');
Map.put ("Four", ' 4 ');
Map.put ("Five", ' 5 ');
set<string> set = Map.keyset ();
(1) Enhanced for
for (String String:set) {
System.out.println (string+ "" +map.get (string));
}
(2) iterators
iterator<string> Iterator = Set.iterator ();
while (Iterator.hasnext ()) {
String Str=iterator.next ();
Integer integer = map.get (str);
System.out.println (str+ "" +integer);
}
The second is the design of the Map class through Map.entry<k,v>, which provides a nested interface: Entry. Entry encapsulates the corresponding relationship of a key-value pair into an object.
GetKey () Method: Gets the key in the entry object
GetValue () Method: Gets the value in the entry object
EntrySet () Method: Used to return all the key-value pairs (Entry) objects in the Map collection, returned as a set collection.
(1) Enhanced for
set<entry<string, character>> entries = Map.entryset ();
For (entry<string, character> entry:entries) {
String str = Entry.getkey ();
Character Character = Entry.getvalue ();
System.out.println (str+ "" +character);
}
(2) iterators
set<map.entry<string, string>> entries = Map.entryset ();
iterator<map.entry<string, string>> Iterator = Entries.iterator ();
while (Iterator.hasnext ()) {
entry<string, string> Entry = Iterator.next ();
System.out.println (Entry.getkey () + "" +entry.getvalue ());
}
Nesting of 3.Map
We pass in a reference type in a nested map person here remember to rewrite the Equlas and Hashcode methods
Private Static voidfun6 () {Map<person, string> map1 =NewLinkedhashmap<person, string>(); Map1.put (NewPerson ("Huang Xiaoming", 40), "Beijing"); Map1.put (NewPerson ("Anlaybay", 36), "Beijing"); Map<person, string> map2 =NewLinkedhashmap<>(); Map2.put (NewPerson ("Song Jiang", 40), "Liangshan"); Map2.put (NewPerson ("Lin", 40), "Leopard Head"); Map<map<person,string>, string> Map =NewLinkedhashmap<>(); Map.put (MAP1,"First Group"); Map.put (MAP2,"Second Group"); for(Entry<map<person,string>, string>Zhu:map.entrySet ()) {String value=Zhu.getvalue (); for(Entry<person, string>Entry:zhu.getKey (). EntrySet ()) {Person Key=Entry.getkey (); String value2=Entry.getvalue (); System.out.println (Value+".." +key+ ".." +value2); } } } /*** Iterator Traversal*/ Private Static voidfun5 () {Map<person, string> map1 =NewLinkedhashmap<person, string>(); Map1.put (NewPerson ("Huang Xiaoming", 40), "Beijing"); Map1.put (NewPerson ("Anlaybay", 36), "Beijing"); Map<person, string> map2 =NewLinkedhashmap<>(); Map2.put (NewPerson ("Song Jiang", 40), "Liangshan"); Map2.put (NewPerson ("Lin", 40), "Leopard Head"); Map<map<person,string>, string> Map =NewLinkedhashmap<>(); Map.put (MAP1,"First Group"); Map.put (MAP2,"Second Group"); Set<Map<Person,String>> set =Map.keyset (); Iterator<map<person, string>> iterator =Set.iterator (); while(Iterator.hasnext ()) {Map<person, string> next =Iterator.next (); String String=Map.get (next); Set<Person> persons =Next.keyset (); Iterator<Person> Iterator2 =Persons.iterator (); while(Iterator2.hasnext ()) {person Next2=Iterator2.next (); String string2=Next.get (NEXT2); System.out.println (string2+ "" +next2+ "" +string); } } }
Here I add a collection of tool classes
Collections
Public Static voidMain (string[] args) {ArrayList<String> arrayList =NewArraylist<>(); Arraylist.add (A); Arraylist.add ("S"); Arraylist.add ("G"); Arraylist.add ("D"); //SortCollections.sort (arrayList); System.out.println (arrayList); //FlipCollections.reverse (arrayList); System.out.println (arrayList); //Scramble Ordercollections.shuffle (arrayList); System.out.println (arrayList); //Two-point searchlist<integer> integers =NewArraylist<>(); Collections.addall (Integers,1,2,3,4,5,7,8); System.out.println (integers); intBinarySearch = Collections.binarysearch (integers, 6); System.out.println (BinarySearch); //Disrupt Shufflecollections.shuffle (integers); System.out.println (integers); }
Today most of the code shows, do not introduce, the collection of this module is nothing more than add and remove change and check and traverse, I in the process of learning basic every set of methods I will knock 10 times to deepen the impression
Java Base Map Interface +collections