Java 1.7 - generic error syntax - java

I get from github java project. I have java 1.7 version
there is code like this:
protected Set<Tag> tags = null;
private final Map<Tag, String> results;
protected AbstractAction() {
this.tags = new HashSet<>();
this.results = new HashMap<>();
}
I added it to eclipse, but there is error on new HashSet<>();
The error in eclipse is :
Multiple markers at this line
- Cannot instantiate the type HashSet
- Syntax error on token "<", ? expected after this token
- Type mismatch: cannot convert from HashSet to Set
- Syntax error on token "<", ? expected after this token
How do you think I can resolve it ?
Thank you.

The type inference feature was introduced in Java 7, and your code compiles correctly using the Java 7 JDK. Make sure you have configured the Java version level in the Eclipse project for Java 7 and not some earlier version.

Related

"incompatible types" in foreach with jdk 1.7 - no error with 1.6

I have code similar to the following which compiles with jdk 1.6.0_22 but not with jdk 1.7.0_79:
for(Entry<A, B> entry: aBean.getData().entrySet()) { }
getData() returns a Map<A, B>. With 1.7 the following compile error occurs, suggesting that the generics of the returned Set are erased:
[compile] /path/to/file.java:170: error: incompatible types
[compile] for (Entry<A, B> entry: aBean.getData().entrySet()) {
[compile] ^
[compile] required: Entry<A, B>
[compile] found: Object
The error disappears when I use a local variable to hold the Set:
Set<Entry<A, B>> mySet = aBean.getData().entrySet();
for(Entry<A, B> entry: mySet) { } //works
Given that this compiles under 1.6, am I correct in assuming this is a compile Bug? If not, what am I doing wrong?
Have you already tested this with Java 8? If the issue still exists with the latest – and only supported – Java version, I suggest you create a bug report.
Otherwise use the solution you already provided in your question.

Convert a stream into a list

I want to convert a stream into a list. To do so I have tried this code :
try(Stream<String> stream = Files.lines(Paths.get(args[0]))) {
List<String> lines = stream.collect(Collectors.toList());
}
But then the compiler tell me that :
Type mismatch: cannot convert from List<Object> to List<String>
Also I've tried this way :
try(Stream<String> stream = Files.lines(Paths.get(args[0]))) {
List<String> lines = Stream.of(stream).collect(Collectors.toList());
}
But then I have this error :
This static method of interface Stream can only be accessed as Stream.of
which happen every time, whenever I use the Stream.of methods.
Can someone help me out, please?
It's seems that you use java compiler language level lower than 8.
Try compiling from command line with
javac -source 8
Also check your language level project setting in IDE.
For example, output from IDEA 2016 hides info about used language level, but provide info about javac version
Information:Using javac 1.8.0_101 to compile java sources
Information:java: Errors occurred while compiling module 'demo_04'
Information:8/1/16 2:13 PM - Compilation completed with 1 error and 0 warnings in 1s 403ms
/tmp/1/src/JavaStreamExample.java
Error:(16, 49) java: incompatible types: java.util.List<java.lang.Object> cannot be converted to java.util.List<java.lang.String>

javac incompatible types error in eclipse

While compiling the code I get an unexpected error, which never occurred before, it says that I cannot convert from int to an Object...
Code:
maxBundles = max;
bundleProgressBar.setMaximum(max);
bundleProgressLabel.setText("Updating Components...");
// Tell JS that the state is Installing.
Object[] arr = { 1 };
error:
`103: error: incompatible types
[javac] Object[] arr = { 1 };`
I know this is a problem with eclipse, because it worked before, so my question is what can I change to resolve it...
What you're trying to do is called autoboxing - the process of converting a primitive (int in this case) to its Object represantation (Integer in this case), automatically. More on autoboxing and unboxing here.
Autoboxing was introduced in Java 1.5. So check that your project's compiler compliance level is set to 1.5 or above (Project properties -> Java compiler).

Change to compiler 1.7 when creating a List produced errors throughout the entire project

I am using Eclipse Juno and Java.
I was trying to create a new list:
List myList = new ArrayList();
This had an error and the resolution on it was along the lines of change compiler to 1.7 which I accepted. The errror on this list creation line was corrected however I now have many errors thoughout the whole project on lines that were previously working. Some examples are:
class GetAccountAndCubsHandler<T> implements AsyncCallback<List<AccountAndCubs>>
Multiple markers at this line
- The hierarchy of the type GetAccountAndCubsHandler is inconsistent
- List cannot he resolved to a type
public class AccountCreationView extends Composite {
Multiple markers at this line
- The hierarchy of the type AccountCreationView is inconsistent
- Breakpoint:AccountCreationView
#SuppressWarnings("unused")
private String accountId;
First line - Multiple markers at this line
- SuppressWarnings cannot be resolved to a type
- The attribute valie is undefined for the annotation type SuppressWarnings
Second line - String cannot be resolved to a type
As you can imagine having my whole project adversely affected in this way is very disconcerting so any advice on how to recover would be greatly appreciated.
Regards,
Glyn
"String cannot be resolved to a type"
Ensure that a valid JRE or JDK is specified in your build-path.
Since you're using Eclipse, right-click on your project, then Properties → Java Build Path.
Perhaps you don't have a Java 7 JRE configured in the Installed JREs preference panel.

JDK compiler error

Before anyone screams about EOL'ed JDK, I'd like to point out that my question is not about how to compile the following. There is a real question here and it's not about JDK 1.5 being EOL'ed...
The following under JDK 1.5, up to 1.5.0_22 (the last one I could find) produces on my system a compiler error:
private Object[] boozinga() {
boolean b = Math.abs(42) > 0;
Object[] res = new Object[1];
res[0] = b ? new int[1] : new String[1];
return res;
}
Changing the Math.abs(42) > 0 to true allows compilation.
Changing the ternary "assignment" to an if/else allows compilation.
Using JDK 1.6 allows compilation.
So I was wondering: is there something not legal in the above code under Java 1.5 and that is allowed under Java 1.6?
Does it crash for those of you that are under Java 1.5 too?
The crash says something like this:
An exception has occured in the
compiler (1.5.0_22). Please file a bug
at the Java Developer Connection
(http://java.sun.com/webapps/bugreport)
after checking the Bug Parade for
duplicates. Include your program and
the following diagnostic in your
report. Thank you.
I take it filling a bug report for an EOL'ed JDK is an exercice in futility but still, I'd still like to know if the above is valid Java 1.5 code or not.
I think it is legal. The evidence is that JDK 1.6.0_21 compiles it with options -source 1.5 -target 1.5. Can't you use JDK 1.6 with these options to compile and JRE 1.5 to run?
It crashes for me, too (JDK 1.5.0_12). It crashes for me even with:
public Object boozinga() {
boolean b = true;
Object res = b ? new int[1] : new String[1];
return res;
}
The difficulty for the compiler is that the type of b ? new int[1] : new String[1] is java.lang.Object & java.io.Serializable & java.lang.Cloneable.
The problem here is that the compiler has trouble to decide the type of the expression b ? new int[1] : new String[1]. I had something like this before (with 1.1.8 or 1.2, I think - but with a real error message, not a compiler crash), and then simply used a cast to help the compiler here.
res[0] = b ? (Object)new int[1] : new String[1];
I didn't look what the language specification says about this - but the compiler should never crash with an exception, it should give a real error message.

Categories

Resources