When I am passing an ArrayList to TreeSet constructor, I am getting the following error:
Exception in thread "main" java.lang.ClassCastException: [I cannot be cast to java.lang.Comparable
at java.util.TreeMap.compare(TreeMap.java:1294)
at java.util.TreeMap.put(TreeMap.java:538)
at java.util.TreeSet.add(TreeSet.java:255)
at java.util.AbstractCollection.addAll(AbstractCollection.java:344)
at java.util.TreeSet.addAll(TreeSet.java:312)
at java.util.TreeSet.<init>(TreeSet.java:160)
at jay.week1.MaxPairwiseProduct.getMaxPairwiseProduct(MaxPairwiseProduct.java:8)
at jay.week1.MaxPairwiseProduct.main(MaxPairwiseProduct.java:17)
I am getting the above error at this line :
TreeSet<Integer> set = new TreeSet(Arrays.asList(numbers));
This is the full program:
import java.util.*;
public class MaxPairwiseProduct {
static int getMaxPairwiseProduct(int[] numbers) {
TreeSet<Integer> set = new TreeSet(Arrays.asList(numbers));
int max1 = set.pollLast();
int max2 = set.pollLast();
int result = max1 * max2;
return result;
}
public static void main(String[] args) {
int[] numbers = {1, 2, 3};
System.out.println(getMaxPairwiseProduct(numbers));
}
}
What is it that I am doing wrong?