Theja
Theja

Reputation: 762

class that accepts duplicates and sorts input automatically

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

Upvotes: 1

Views: 171

Answers (2)

amicngh
amicngh

Reputation: 7899

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.

Upvotes: 0

Jason
Jason

Reputation: 11363

List, along with Collection.sort() would fit your needs.

Upvotes: 1

Related Questions