Elham
Elham

Reputation: 1

How to cast ArrayList<LinkedHashMap<String, Comparable>> to Comparable?

I want to add y to the x:

x.put("y", (Comparable) y);

y is defined as:

ArrayList<LinkedHashMap<String, Comparable>> y = new ArrayList<LinkedHashMap<String, Comparable>>();

Output example of

y =[{c=32,a=1, b=2 }, {c=33,a=4, b=5 }]

x is defined as:

LinkedHashMap<String, Comparable> x = new LinkedHashMap<String, Comparable>();

I am getting this error: java.util.ArrayList cannot be cast to java.lang.Comparable where i am trying to : x.put("y", (Comparable) y); and if I remove the comparable it shows that error exist in the eclipse. Any suggestion?

/Elham

Upvotes: 0

Views: 446

Answers (2)

Elham
Elham

Reputation: 1

Well, Thanks for your answers. This problem is solved by replacing x and y with the following: ArrayList> y = new ArrayList<>(); Map x = new HashMap<>();

And then just use: x.put("y", y);

Upvotes: 0

Gilad
Gilad

Reputation: 841

If this is what you want, you can create a class likes so

private static class CompArrayList extends ArrayList<LinkedHashMap<String, Comparable>> implements Comparable
{

    @Override
    public int compareTo(Object o)
    {
        return 0;
    }
}

define whatever you want in the compareTo() method and define y using this class

CompArrayList y = new CompArrayList ()

now y is Comparable and you can add it to your map

Upvotes: 1

Related Questions