Arijit Dasgupta
Arijit Dasgupta

Reputation: 325

How to add ArrayList elements in TreeSet or TreeMap

As we all know, we need to implement Comparable interface and add compareTo() when we work with TreeSet. Not doing so will throw ClassCastException. Now I have a TreeSet and I need to add ArrayLists as elements of TreeSet.

if we write:

ArrayList al = new ArrayList();
ArrayList al2 = new ArrayList();
ArrayList al3 = new ArrayList();
TreeSet ts = new TreeSet();
ts.add(al);
ts.add(al2);
ts.add(al3);

It throws ClassCastException.

Question: How can I add ArrayList istances (not their elements) to a TreeSet or TreeMap?

Upvotes: 2

Views: 9328

Answers (3)

Lorenzo Barbagli
Lorenzo Barbagli

Reputation: 1281

1) As said before, you are adding ArrayList to TreeSet, not an element inside ArrayList.

al.add(__something__);
ts.add(al.get(__something-to-get-your-element-from-al__));

2) It's not recommended to use raw types, as Lists and Sets are generic types:

List<String> al = new ArrayList();
Set<String> ts = new TreeSet();
al.add("hello");
al.add("hello2");
ts.add(al.get(0));
ts.add(al.get(1));

or:

ts.addAll(al); //which will add to your TreeSet all the elements that are in the ArrayList.

EDIT: 3) probably you want to add ArrayList to TreeSet, then you have to declare the set like this:

List<String> al = new ArrayList();
//add elements to al
Set<List<String>> ts = new TreeSet();
ts.add(al);

Upvotes: 0

A_Di-Matteo
A_Di-Matteo

Reputation: 27812

If you really need to add array list (as an instance) and not its elements, you should use another constructor rather than the empty one, consider the constructor taking as a parameter a Comparator.

TreeSet(Comparator<? super E> comparator)

Constructs a new, empty tree set, sorted according to the specified comparator.

You can define upfront a Comparator for your purpose with the meaning you actually want with regards to the concerned arraylists.

Then add the array list instances, which will properly compared according to your comparator.


As an example, you can define a Comparator which would compare array lists based on their size (sample simple comparison):

public class MyArrayListComparator implements java.util.Comparator<ArrayList> {

    public int compare(ArrayList al1, ArrayList al2) {
        if (al1.size() > al2.size())
            return 1;
        if (al1.size() < al2.size())
            return -1;
        return 0;
    }
}

Then in your code:

    ArrayList al = new ArrayList();
    ArrayList al2 = new ArrayList();
    ArrayList al3 = new ArrayList();
    TreeSet ts = new TreeSet(new MyArrayListComparator());
    ts.add(al);
    ts.add(al2);
    ts.add(al3);

Note the

TreeSet ts = new TreeSet(new MyArrayListComparator());

This is actually a good example of the difference between Comparable and Comparator:

  • Comparable is implemented by the class you want to use, with a specific behavior and you cannot change it or add it
  • Comparator is an external implementation you can add (if supported by the concerned consumer) with the behavior you want

Check also this SO Q/A for further details on Comparable vs Comparator.

Upvotes: 5

Trace Carrasco
Trace Carrasco

Reputation: 92

Currently you are adding ArrayList objects and not the elements within them. As described in another answer, using the addAll will work because ultimately it will go through each ArrayList and add the individual elements.

Upvotes: 0

Related Questions