Java basic Map interface + Collections tool class, collections tool class

Source: Internet
Author: User
Tags map class

Java basic Map interface + Collections tool class, collections tool class

1. In Map, we mainly talk about two interfaces: HashMap and LinkedHashMap.

(1) how to obtain the sorted hashmap

Let's talk about the add, delete, modify, and query functions of Map:

/** Add Map set */Map <String, String> map = new HashMap <String, String> (); map. put ("Monday", "Monday"); map. put ("Saturday", "Sunday"); System. out. println (map);/** obtain the value through creation */String string = map. get ("Saturday"); System. out. println (string);/** modify value through creation */map. put ("Monday", "Mon"); System. out. println (map);/** Delete value through creation */String remove = map. remove ("Saturday"); System. out. println (remove); System. out. println (map );}

 

The above is the addition, deletion, modification, and query method of Map.

 

2. Map Traversal

1. First: Use the keySet () method to obtain all the data Set created and stored in the Set <> Set.

(1) enhanced

(2) iterator

Map <String, Character> map = new LinkedHashMap <String, Character> ();
Map. put ("1", '1 ');
Map. put ("2", '2 ');
Map. put ("3", '3 ');
Map. put ("4", '4 ');
Map. put ("5", '5 ');
Set <String> set = map. keySet ();

(1) enhanced

For (String string: set ){
System. out. println (string + "" + map. get (string ));
}

(2) iterator

Iterator <String> iterator = set. iterator ();
While (iterator. hasNext ()){
String str = iterator. next ();
Integer integer = map. get (str );
System. out. println (str + "" + integer );
}

The second method uses Map. Entry <K, V> to provide a nested interface during Map class design: Entry. The Entry encapsulates the correspondence between key-value pairs into objects.

GetKey () method: Get the key in the Entry object
GetValue () method: Get the value in the Entry object
EntrySet () method: This method is used to return all key-value pairs (Entry) objects in the Map Set and return them in the form of a Set.

(1) enhanced

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) iterator

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 ());
}

 

3. Nesting of Map

We pass in the reference type Person in the nested Map. here we need to rewrite the equlas and HashCode methods.

Private static void fun6 () {Map <Person, String> map1 = new LinkedHashMap <Person, String> (); map1.put (new Person ("Huang Xiaoming", 40 ), "Beijing"); map1.put (new Person ("AnlayBay", 36), "Beijing"); Map <Person, String> map2 = new LinkedHashMap <> (); map2.put (new Person ("", 40), "Liangshan"); map2.put (new Person ("Lin Chong", 40), "leopard Header"); Map <Person, string>, String> map = new LinkedHashMap <> (); map. put (map1, "group 1"); 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 void fun5 () {Map <Person, String> map1 = new LinkedHashMap <Person, string> (); map1.put (new Person ("Huang Xiaoming", 40), "Beijing"); map1.put (new Person ("AnlayBay", 36), "Beijing "); map <Person, String> map2 = new LinkedHashMap <> (); map2.put (new Person ("", 40), "Liangshan "); map2.put (new Person ("Lin Chong", 40), "leopard Header"); Map <Person, String>, String> map = new LinkedHashMap <> (); map. put (map1, "group 1"); map. put (map2, "group 2"); 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 will add a set of tool classes

Collections

Public static void main (String [] args) {ArrayList <String> arrayList = new ArrayList <> (); arrayList. add ("a"); arrayList. add ("s"); arrayList. add ("g"); arrayList. add ("d"); // sort Collections. sort (arrayList); System. out. println (arrayList); // flip Collections. reverse (arrayList); System. out. println (arrayList); // disrupt the order of Collections. shuffle (arrayList); System. out. println (arrayList); // List for Binary Search <Integer> integers = new ArrayList <> (); Collections. addAll (integers, 1, 2, 3, 4, 5, 7, 8); System. out. println (integers); int binarySearch = Collections. binarySearch (integers, 6); System. out. println (binarySearch); // disrupt shuffle Collections. shuffle (integers); System. out. println (integers );}

Today, most of them are presented in code and I will not introduce them much. The set Module is nothing more than addition, deletion, modification, query, and traversal, in the course of my study, I have typed every set of methods for ten times to deepen my impression.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.