0

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

sidgate
  • 14,650
  • 11
  • 68
  • 119
Elham
  • 1

2 Answers2

1

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

Gilad
  • 833
  • 1
  • 8
  • 13
0

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);

Elham
  • 1