Solved

Enforce order in SortedMap

Posted on 2010-11-17
2
220 Views
Last Modified: 2012-05-10
Suppose I have a SortedMap:
SortedMap <String, String> map = new TreeMap<String, String>();

Open in new window

and then I add some values:
   map.put("Mary", "Orange");
   map.put("Bob", "Banana");
   map.put("Jack", "Apple");

Open in new window

It seems that by default, it puts them in alphabetical order.
How would I cause it to put it in order that they are added to the list?


0
Comment
Question by:allelopath
  • 2
2 Comments
 
LVL 92

Expert Comment

by:objects
ID: 34159879
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 34159890
> How would I cause it to put it in order that they are added to the list?

sorry didn't see that. in which case you're not actaully sorting them
use a LinkedHashMap instead
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now