Sorting a HahMap seems a bit of a sizifus work, quoting javaDoc
This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time
Plus, how do you want your map to be sorted, by key or by value? Anyhow, java.util package usualy has answers you are looking for. Try finding something in Collections class or even, if you'll do some type transformations, in the Arrays class.
Hope I haven't overstepped any Beginning Java form rules by providing this info.
Certain List implementation usually provide a constructor using a Collection (a Set is a collection too). Therefore you could create a List from the Set of keys and sort them, as well as create a List from the Set of values and sort them. But try reading the tutorial first:
Censorship is the younger of two shameful sisters, the older one bears the name inquisition. -- Johann Nepomuk Nestroy
I stored objects in ArrayList. I moved my objects to HashSet(which doest allow duplicates)
to remove duplicates. Will it reduce Performance.
For this why cant i use HashSet Directly.(what is the need of arraylist)
The adding will never occur. You are checking for each element inside ret if it's not inside ret. Of course it is, so the guard returns false. This is a good thing, because otherwise the add method would have caused a ConcurrentModificationException. You can't modify a collection while iterating over it (either with an Iterator or with the for-each loop). There may be a few Collection implementations that do allow it but most don't. ArrayList most certainly doesn't.
As for a possible solution:
1) use Collections.sort to sort the list
2) iterate over the loop (using an explicit Iterator!), removing each element (with Iterator.remove(), that's the only way to remove elements while iterating) that equals its predecessor.