Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Sorting HashMap by key

Chris JonesJun 15 2011 — edited Jun 15 2011
I would like to sort my hashmap by key so that it prints out letters first and then numbers i.e a,b,c,d,1,2,3,10,100.

Is there a smart way to do this?



	public static void main(String[] args) throws IOException {
		HashMap<String, Integer> values = new HashMap<String, Integer>();
		values.put("A",1);
		values.put("B",2);
		values.put("C",3);
		values.put("D",4);
		values.put("1",5);
		values.put("2",6);
		values.put("3",7);
		values.put("10",8);
		values.put("100",9);

		SortedSet<String> sortedset= new TreeSet<String>(values.keySet());
		Iterator<String> keys = sortedset.iterator();
		while(keys.hasNext()){
			System.out.println(values.get(keys.next()));		
                                     }
		
	}
Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 13 2011
Added on Jun 15 2011
3 comments
360 views