I know that The Treeset Sort the input automatically but don't accept duplicates.Is there any class Collection in java that allows Duplicate values or objects and Sort the given Input
Asked
Active
Viewed 160 times
1
-
1possible duplicate of [Java list that automatically sorts elements as I add them](http://stackoverflow.com/questions/7097974/java-list-that-automatically-sorts-elements-as-i-add-them) – Alex K. Jun 15 '12 at 11:35
-
You can have a custom Comparator which never return 0. – Peter Lawrey Jun 15 '12 at 11:39
2 Answers
0
User List Implementation and sort them using Collection.sort()
List<String> list=new ArrayList<String>();
list.add("A");
list.add("C");
list.add("A");
list.add("B");
list.add("A");
System.out.println(list);
Collections.sort(list);
System.out.println(list);
But if you are using you object in collection then Implement Comparable
interface and override compare(Object obj,Object obj1)
method.
Otherwise you can write your Comparator
then pass it to sort
method.

amicngh
- 7,831
- 3
- 35
- 54