0

I am storing an arrayList as my keys in a TreeMap but I am getting this exception

java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Comparable

I copy the content of array to ArrayList and try to store that arrayList as my keys in the Map My Code is :

TreeMap< ArrayList<Integer> , Integer > bandsMap = new TreeMap< ArrayList<Integer> , Integer >(); 
ArrayList< Integer > erfcn = new ArrayList< Integer >();

for (int index = 0; index < frequencies.length; index++)   
    erfcn.add(frequencies[index]);

    bandsMap.put( erfcn , band_number);

 for (Integer value : bandsMap.values()) {
    System.out.println("Value = " + value + "\n");
 }

Any Idea ? Thanks

8
  • 2
    java.util.ArrayList cannot be cast to java.lang.Comparable. List doesn't extends Comparable, then there are not defined comparing operation through compareTo(T o) method on it. You can't use as map key. Commented Nov 8, 2013 at 12:23
  • 1
    TreeMap compares keys before adding them. How do you think it's going to compare two instances of ArrayList? What is the criteria that determines that a list is smaller/bigger/equal to another? Commented Nov 8, 2013 at 12:23
  • Do you want to construct the building? Commented Nov 8, 2013 at 12:24
  • Do you really need a List<Integer> for the keys? Can you think of a possible workaround? Commented Nov 8, 2013 at 12:24
  • 1
    A Multimap stores collections of values. Collections of keys is not a normal thing to do - you really should rethink your design. Commented Nov 8, 2013 at 12:29

6 Answers 6

5

A tree map maintains its keys in sorted order. The ArrayList class does not define any ordering, so it cannot be used as a key directly. You can supply an external comparator to impose an order though, but you must define an ordering that makes sense to you:

TreeMap<ArrayList<Integer>, Integer> bandsMap = new TreeMap<>(
    new Comparator<ArrayList<Integer>>() {
        public int compare(ArrayList<Integer> lst1, ArrayList<Integer> lst2) {
            // return 1 if lst1 > lst2, 0 if equal, -1 if lst1 < lst2
        }
    });

Alternatively, if you don't have to maintain the keys in any particular order, use a HashMap instead.

Sign up to request clarification or add additional context in comments.

Comments

2

ArrayLists don't implement Comparable, so you need to use an unsorted map, like HashMap, or tell the TreeMap how to sort your ArrayLists by using this constructor.

Comments

1

The error itself indicates the problem. ArrayList class does not implement java.lang.Comparable interface and the TreeMap is expecting the key to implement comparable interface. Hence it is causing the exception.

Refer ArrayList documentation.

As we cannot go modify ArrayList, you can use external comparator to make ArrayList work as a key to the TreeMap. You just need to override compare() method in it.

Comments

0

You can't compare two lists, you have to either change list to some other structure, or make your own list using Comparable interface. Good solution is also to wrap up list in new class which implements Comparable and implement just this method from interface.

Check out this

public class Fruit implements Comparable<Fruit>{
     public int compareTo(Fruit compareFruit) {
    //your code here 
     }  
}

and this link. Hope it helps.

Comments

0

If you really want to use ArrayList as key in TreeMap, then you need to write Comparator for it and pass is using constructor

Using List as key in Tree is not a good idea, please review your design.

Comments

0

Given what the other answers have said about Lists not implementing Comparable, you could create your own class to act as TreeMap keys, extending ArrayList and implementing Comparable:

class KeyList extends ArrayList<Integer> implements Comparable<ArrayList<Integer>> {

  public int compareTo(ArrayList<Integer> list) {
    //decide how to compare ArrayLists, then implement it here 
    return 0;
  }

}

Then you can create your TreeMap:

new TreeMap<KeyList, Integer>();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.