The Artima Developer Community
Sponsored Link

Java Buzz Forum
How to Sort Map in Java with Example

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Javin Paul

Posts: 1090
Nickname: javinpaul
Registered: Jan, 2012

Javin Paul is Java Programmer working on Finance domain.
How to Sort Map in Java with Example Posted: Apr 29, 2014 7:13 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Javin Paul.
Original Post: How to Sort Map in Java with Example
Feed Title: Java67
Feed URL: http://www.java67.com/feeds/posts/default?alt=rss
Feed Description: Java and technology tutorials, tips, questions for all programmers.
Latest Java Buzz Posts
Latest Java Buzz Posts by Javin Paul
Latest Posts From Java67

Advertisement
So you have a Map in your Java program and you want to process its data in sorted order. Since Map doesn't guarantee any order for its keys and values, you always end up with unsorted keys and Map. If you really need a sorted Map then think about using TreeMap, which keeps all keys in a sorted order. This could be either natural order of keys (defined by Comparable) or a custom order (defined by Comparator), which you can provide, while creating instance of TreeMap. If you don't have your data in a sorted Map then only option remains is to get the keys, sort them and then process data in that order. Since keys are unique in Map, it returns a Set of keys, which means you cannot sort them by using Collections.sort() method, which accept a List. So what to do? Well, we can convert our Set into List as shown in this example, then sort them using sort() method in any order and process them accordingly. Now which approach is better, either using TreeMap or keeping mappings in an unsorted general purpose Map implementation like HashMap or Hashtable? well both have their advantages and disadvantages, but if requirement is always keep keys in sorted order then use TreeMap, though insertions will be slow but keys will always remain in sorted order. On the other hand, if requirement is just to process data in a particular order, which also varies depending upon which method is using Map, then use general purpose Map e.g. HashMap and only sort when needed. Insertion will be much faster, but it will require additional time to sort keys before processing. In next section, we will see example of both approach of sorting Map in Java.
Read more ยป

Read: How to Sort Map in Java with Example

Topic: Do more with less: Lambda expressions in Java 8 Previous Topic   Next Topic Topic: HashMap performance improvements in Java 8

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use