Manipulating HashMap using lambda expressions

Source: Internet
Author: User

JAVA8, if you want to sort the words according to the value of the map, it's more cumbersome to see how to HashMap, sort by value

Now it's much easier to use lambda expressions.

<span style= "FONT-SIZE:14PX;" >Map<String,Integer> map=new hashmap<> (), Map.put ("Derek", "Map.put" ("Dad", "Wuyi"), Map.put ("Mom", 46); List<map.entry<string, integer>> list=new arraylist<> (); List.addall (Map.entrySet ());// Collections.sort (List,new comparator<map.entry<string, integer>> () {//public int compare (Map.Entry< String, integer> m1,map.entry<string, integer> m2) {//return m1.getvalue ()-m2.getvalue ();//}//}); Collections.sort (list, (M1, M2)->m1.getvalue ()-m2.getvalue ()); List.foreach (System.out.println (M.getkey ( ));</span>

The commented out part is the previous way, now only need a short line of code can be done, traversal is also a lot of convenience.

The functional programming that lambda expressions bring to Java is really sharp, and with the enhanced type inference in Java8, the code becomes simple and understandable ~

Manipulating HashMap using lambda expressions

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.