Based on the typo, and your clarification that you are using hashmap, the order of the keys retrieval will not be consistent with the insert order. Use LinkedHashMap for that. This is using you do external sorting and then insert sorted entries into the map.
Based on the typo, and your clarification that you are using hashmap, the order of the keys retrieval will not be consistent with the insert order. Use LinkedHashMap for that. This is using you do external sorting and then insert sorted entries into the map.
If you want the entries to be sorted while they are being inserted into the Map, use TreeMap. You can either use a custom comparator or make your key object implement Comparable interface.
Or TreeMap for "natural order". – Thilo Sep 14 at 10:42 or TreeMap with a Comparator for any order you like. – EJP Sep 14 at 10:51 @EJP : yes, already mentioned in the answer (custom comparator for any ordering or Comparable for natural ordering).
Maybe I needed to make it explicit that custom comparator is usually used for atypical ordering and comparable for natural ordering. – Scorpion Sep 14 at 10:56.
HashMap uses key.hashValue() to sort the values. Use TreeMap instead.
No. It doesn't do anything of the sort. – EJP Sep 14 at 10:39 is there anyway to extract the key values from a sorted hashmap and store them as string?
– Umang Sep 14 at 10:40 @Umang yes, you did it yourself in your question. – EJP Sep 14 at 10:43 I know, but the order of the keys is not the same. I want the same order as it was after sorting.
– Umang Sep 14 at 10:45 @Umang So sort it. But that doesn't sort the HashMap, it sorts a copy of the Collection returned by keys(). – EJP Sep 14 at 10:50.
I have a sorted hashmap based on values. I have sorted a hashmap based on values No you haven't. A HashMap isn't sorted at all.
You can get the values(), as a Collection, and you can sort that any way you like, but it doesn't sort the HashMap itself. But this method didn't work. It stores the keys in random fashion.It isn't defined to do anything differently, especially as you haven't sorted the HashMap at all.
You need to clarify what you're talking about here. If you want to sort the values, do the above. If you want to sort the keys, do the above with keys() instead of values()'.
If you want the Map itself sorted on keys, use a TreeMap. If you want the Map itself sorted on values, bad luck, you can't.
I know a hashmap isnt sorted on its own. I meant that I created a hashmap and then sorted it. – Umang Sep 14 at 10:44 you can see the question for the link to how sorting was done – Umang Sep 14 at 10:44 @Umang That link doesn't demonstrate sorting a HashMap at all.It shows code that returns a List that has been sorted.
Nothing whatsoever has been done to the HashMap. – EJP Sep 14 at 10:47 the link says on the top - "This following code outputs the elements of the map sorted by value." And its actually sorting based on values!
I can see that! – Umang Sep 14 at 10:49 @Umang and so it does, because it processed the sorted List. The HashMap itself has not been sorted.
– EJP Sep 14 at 10:51.
Try this: public static void main(String args) { Map hm = new TreeMap(); hm. Put("AAA", "typeAAA"); hm. Put("BBB", "typeBBB"); hm.
Put("ABB", "TypeABB"); String keys = hm.keySet(). ToArray(new String0); for (String key : keys) { System.out. Println("key: " + key); } } The output would be: key: AAA key: ABB key: BBB.
It would seem that you want the order of the keys of a HashMap to be the same as your sorted list of keys. This is simply not possible. A HashMaps keys are determined by the hash table algorithms; e.g. A complicated process that depends on the keys' hash values and sequence of insertions and deletions.
The closest you will get is to create a LinkedHashMap, and populate it by inserting entries from the old HashMap in the order of the sorted keys. If you then iterate over the LinkedHashMap's keys, you will get them back in the order in which they were inserted. But this is a heavy-weight solution, and it breaks down if you subsequently have to add more entries to the "sorted" map.
It may be better to just use a TreeMap. I don't want to make changes to the hashmap. I just want to get an array with the keys in the order of sorted values.
In that case, you simply need to extract the HashMap's keys into an array and sort it. The code has been given in other answers. On the other hand, if you want to do something so that the map's keys always come out in the sorted order (which you seem to be saying in other comments), you are changing the map.
I don't want to make changes to the hashmap. I just want to get an array with the keys in the order of sorted values. – Umang Sep 14 at 11:23 thanks.
Will look into it. – Umang Sep 14 at 11:26.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.