1

I'm pretty confused... I know that I can store an ArrayList of Lists on a List of Lists;

I have an object like this:

SemanticTuple< List<String>, List<List<String>> > object;

I can do this:

List<List<String>> list = new ArrayList<>();
object = new SemanticTuple<>("name", Arrays.asList(header.split(headerSplitter)), list);

But I can't do this:

object = new SemanticTuple<>("name", Arrays.asList(header.split(headerSplitter)), new ArrayList<>());

Why does it can't recognize the type there?

update

public class SemanticTuple <HASH, DATA> implements Serializable {

private String name;
private HASH hash;
private DATA Data;

public SemanticTuple() {
}

public SemanticTuple(String name, HASH hash, DATA Data) {
    this.name = name;
    this.hash = hash;
    this.Data = Data;
}
...

Object is declared as generic from another class:

public class MeteorologicTask extends Task < LineNumberReader, 
                                         SemanticTuple< List<String>, List<List<String>> >,
                                         SemanticTuple< List<String>, List<List<String>> > >{
...

from...

public abstract class Task <RESOURCE, INPUT, OUTPUT> implements Callable<OUTPUT>{

protected RESOURCE resource;
protected INPUT input;
protected OUTPUT output;
protected Integer taskID;

public Task() {
}

public Task(RESOURCE resource, Integer taskID) {
    this.resource = resource;
    this.taskID = taskID;
}
...

An image:

enter image description here

12
  • Try new ArrayList<List<String>>()... I think you have to define the type of object. In the first example it is implicitely done Commented Oct 25, 2015 at 21:11
  • @YassinHajaj No, still wrong type. Commented Oct 25, 2015 at 21:14
  • lets wait for an answer than :) Commented Oct 25, 2015 at 21:16
  • 3
    There isn't enough information to answer this. We need to know a) how SemanticTuple is declared, b) how the constructor for SemanticTuple is declared, c) how object is declared, d) what version of Java you are using (Java 8 has much better type inference). Commented Oct 25, 2015 at 21:17
  • 1
    Yes...this seems to be fine with a rough example on Java 8. I suspect you're using Java 7, right? (My rough example won't compile on Java 7.) Commented Oct 25, 2015 at 21:18

2 Answers 2

4

You are trying to create a new ArrayList without specifying the element type. Have you tried specifying it explicitly?

SemanticTuple<List<String>, List<List<String>>> object = 
    new SemanticTuple<List<String>, List<List<String>>>(
        "name", 
        Arrays.asList(header.split(headerSplitter)), 
        new ArrayList<List<String>>()
    );

Edit: Explicitly declaring the generic type at the constructor call for SemanticTuple does seem to work in Java 7.

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

6 Comments

error: incompatible types: SemanticTuple<List<String>,ArrayList<List<String>>> cannot be converted to SemanticTuple<List<String>,List<List<String>>>
Updated. Could you try again?
Aparently my plataform is java 8 as my plataform is named jdk 1.8 and path is: /usr/lib/jvm/java-8-oracle
Did you try adding the explicit type arguments to the constructor call as well? (new SemanticTuple<List<String>, List<List<String>>>(…)
Ah, this solve it! Thanks :) but why all this dependency?
|
0

In the first case, the type of the contained element is inferred elaborating the left-hand side of the assignment, i.e. List<String>. So, when you pass list as a parameter, its static type is unambiguously assigned. In the second instruction, the compiler is very likely asserting it has no way to infer the contained type, which has to be decided at compile-time.

4 Comments

This isn't true since in Java 8, this behavior works as one would expect the inference to. It may be the case that you're right in this scenario in general, but could you explain it a bit clearer?
Java 8 cannot understand ambiguous statements and Java 18 won't either. The code is not specifying the type of the elements contained in the list and SemanticTuple is generic, so there is no inference that can be done at compile-time. This is not the case of generalized target-type inference.
... But it works fine in Java 8, at least be rough example did. If you're going to argue this point, you'll need to cite some sources.
The OP has been modified and the declaration of object has been added. Now the statement is not ambiguous anymore and it compiles with Java 8 as well as with Java 7. This fact confirms we are not talking about "generalized target-type inference", which did not exist in Java 7.

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.