beh1 beh1 - 3 years ago 138
Java Question

Java code that compiles on one machine but not another

I'm having an issue with some code that I wrote. It compiles fine on my Mac (using "Java 8 Update 121 build 1.8.0", Nano text editor, and just the Java command line compiler javac and java), but it for some reason won't compile on another machine.

On the other machine, the issue stems from the following lines:

public class SomeClass {
...
public static HashMap<String, ArrayList<HashSet<String>>> conversationRecord = new HashMap<>();
...
public static someMethod() {
...
//This produces errors
conversationRecord.put(uuid, new ArrayList<>());
conversationRecord.get(uuid).add(0, new HashSet<>());
conversationRecord.get(uuid).add(1, new HashSet<>());
}//end method
}//end class


The error reads the following:

java:279: error: no suitable method found for put(String,ArrayList<Object>)
conversationRecord.put(uuid, new ArrayList<>());
^
method HashMap.put(String,ArrayList<HashSet<String>>) is not applicable
(actual argument ArrayList<Object> cannot be converted to ArrayList<HashSet<String>> by method invocation conversion)
method AbstractMap.put(String,ArrayList<HashSet<String>>) is not applicable
(actual argument ArrayList<Object> cannot be converted to ArrayList<HashSet<String>>


The above code compiles on my Mac 100% of the time, but won't compile on another machine at all.

Any one have any insight as to why this is happening? (Aside: I don't know anything about the other person's setup besides they are compiling and running Java programs)

Answer Source

It seems like about type inference situation .the jdk8 allow following code:

conversationRecord.put(uuid, new ArrayList<>());

But on earlier version,even jdk7,above code is not allow.so you should check another machine is jdk8 or latter.

Of course,you can change your code like this:

 ArrayList<HashSet<String>> temp=new ArrayList<>();
 conversationRecord.put(uuid, temp);

For more information about type inference .

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download