0

I have the following code

        List<Integer> startnodes = ImmutableList.of(397251,519504,539122,539123,539124,539125);
        List<Integer> endnodes = ImmutableList.of(539126,539127,539142,539143,539144,539145);
        List<String> rp = ImmutableList.of("Knows","Knows","Knows","Knows2","Knows2","Knows2");
        Map<String,Value> parameters =
                  ImmutableMap.of("rels",ImmutableList.of(startnodes,endnodes,rp));

The compiler throws the following error at the last line.

Type mismatch: cannot convert from ImmutableMap<String,ImmutableList<List<? 
extends Object&Comparable<?>&Serializable>>> to Map<String,Value>

My main confusion is that the value for the key here is a heterogenous list, so what should be the type of the key to satisfy the compiler?. I am relatively new to Java and any suggestions are welcome. Thanks!

Kirin
  • 25
  • 5
  • 2
    What is `Value`? Java doesn't have .net like value types. – Oleg Nov 06 '17 at 08:10
  • @Oleg is right, you need to do something like... "Map> parameters = ...." – Jesus Peralta Nov 06 '17 at 08:16
  • Apologize. I am not clear on what the error message means and hence the confusion. My main confusion is that the value for the key here is a heterogenous list, so what should be the type of the key to satisfy the compiler? – Kirin Nov 06 '17 at 08:22
  • replace `Map` with `Map>` – Lino Nov 06 '17 at 08:24
  • Thanks, but that doesn't work as rp here is a string list. I get the following error with the above suggestion. Type mismatch: cannot convert from ImmutableMap&Serializable>>> to Map> – Kirin Nov 06 '17 at 08:30

2 Answers2

2

ImmutableList.of(startnodes,endnodes,rp) needs to infer a type that will satisfy both List<Integer> and List<String> because generics in Java are invariant the only type that satisfies it is List<?>. So you can assign it to:

List<List<?>> list = ImmutableList.of(startnodes,endnodes,rp);

And your Map needs to be defined as:

Map<String,List<List<?>>> parameters = ImmutableMap.of("rels",ImmutableList.of(startnodes,endnodes,rp));
Oleg
  • 6,124
  • 2
  • 23
  • 40
1

You could make your lists of the same type by using ImmutableList.<Object>of(397251,519504,....). This way you'd get a homogeneous ImmutableList<ImmutableList<Object>>, which is possibly slighly less confusing than using wildcards.

However, this all shows that a List is not the proper solution to your problem. What about creating a tiny class? Using Lombok, you need just

@lombok.Value public class Something {
    private final ImmutableList<Integer> startNodes;
    private final ImmutableList<Integer> endNodes;
    private final ImmutableList<String> rp;
}

Without Lombok, you'd need to write quite some boilerplate, but that possibly still worth it, as dealing with heterogeneous Lists is pain.

maaartinus
  • 44,714
  • 32
  • 161
  • 320