Flexible access to key and value in map (recommended for the suffering brothers and sisters who are beginners in Java)

Source: Internet
Author: User

1.map and Map.entery differences

I think a lot of people misunderstand these two classes, or not very flexible application, let me briefly explain my understanding (for reference only).

→map: No matter, learn Java, whether it is a bird or an old cow have knocked to knock, is a set of key-value pairs of classes, belonging to the thread is not safe, about the MAP thread security can be implemented in addition, will be discussed later.

For example:map<string,object> Map = new hashmap<string,object> ();

Map.put ("1", 1);//partition 1

Map.put ("2", 2);//partition 1

Key and value are just our own defined objects, and key is the only one that does not repeat, value can be repeated, that's all.

  

→map.entry is a Map derived class, more powerful, can be obtained through Map.enteyset (), is an instantiated or after the object set,

For example:map.entery<string,object> Map = Map.entryset ();

Map.put ("1", 1);//partition 1

Map.put ("2", 2);//partition 2

is carried by a set container, can iterate over, and provide Getkey () and GetValue (), SetValue () and other commonly used methods;

★example 1    

public class TestMap {

public static void Main (string[] args) {

map<string, object> map = new hashmap<string,object> ();

Map.put ("1", "one");

Map.put ("2", "both");

Map.put ("3", "three");

Map.put ("4", "four");

Map.put ("5", "Five");

SYSTEM.OUT.PRINTLN (Testmap.mapconvertstringone (map));

}

public static String Mapconvertstringone (map<string,object> mapemp) {

StringBuilder returnstring = new StringBuilder ();

Set<map.entry<string, object>> set = Mapemp.entryset ();

map.entry<string, object> Entry = null;

Returnstring.append ("{");

For (iterator<map.entry<string, object>> Iterator2 = Set.iterator (); Iterator2.hasnext ();) {

Entry = Iterator2.next ();

Returnstring.append (Entry.getkey ());

Returnstring.append (":");

Returnstring.append (Entry.getvalue ());

if (Iterator2.hasnext ()) {

Returnstring.append (",");

}

}

Returnstring.append ("}");

return returnstring.tostring ();

}

}

Run Result: {3:three,2:two,1:one,5:five,4:four}//can see unordered arrangement

★example 2

@SuppressWarnings ("Rawtypes")
private static String Mapconvertstringtwo (map<string,object> mapemp) {
StringBuilder returnstring = new StringBuilder ();
set<string> KeySet = Mapemp.keyset ();
Returnstring.append ("{");
for (Iterator Iterator = Keyset.iterator (); Iterator.hasnext ();) {
String key = (string) iterator.next ();
Returnstring.append (key);
Returnstring.append (":");
Returnstring.append (Mapemp.get (key));
if (Iterator.hasnext ()) {

Returnstring.append (",");
}
}
Returnstring.append ("}");
return returnstring.tostring ();
}
Run Result: {3:three,2:two,1:one,5:five,4:four}//can see unordered arrangement

I think that if there is no special treatment of the second example more practical, simple and easy to understand are common use, and on the performance of HashMap performance relative Hashtable better, in the development of basic are practical hashmap. The small examples described above are expected to help you.

  

    

Flexible access to key and value in map (recommended for the suffering brothers and sisters who are beginners in Java)

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.