- Java – The type java.util.Comparator cannot be resolved
- Best Solution
- Related Solutions
- Comparator cannot be resolved to a type
- Comparator cannot be resolved to a type, what should i do
- 1. Import the Comparator library
- 2. Incorrect Java Path
- Using iajc with java 1.8 JDK — Comparator cannot be resolved to a type
- Comparator won’t work with Arrays.sort
- Trying to solve «The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments»
- The type java.util.Comparator cannot be resolved. It is indirectly referenced from required .class files
- Questions : The type java.util.Comparator cannot be resolved. It is indirectly referenced from required .class files
- Answers 1 : of The type java.util.Comparator cannot be resolved. It is indirectly referenced from required .class files
Java – The type java.util.Comparator cannot be resolved
I recently installed Java 8. While executing an application, I got above mentioned error can you please help me.
Best Solution
Assuming you are using some IDE, like Eclipse. When you are using jdk 1.8 with IDE, you need to update your IDE to support version 1.8.
It does not matter you are using new jdk’s feature or not, but compiler has to load new JRE files in order to compile your project.
Related Solutions
Java – What are the differences between a HashMap and a Hashtable in Java
There are several differences between HashMap and Hashtable in Java:
- Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.
- Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.
- One of HashMap’s subclasses is LinkedHashMap , so in the event that you’d want predictable iteration order (which is insertion order by default), you could easily swap out the HashMap for a LinkedHashMap . This wouldn’t be as easy if you were using Hashtable .
Since synchronization is not an issue for you, I’d recommend HashMap . If synchronization becomes an issue, you may also look at ConcurrentHashMap .
Java – the difference between public, protected, package-private and private in Java
The official tutorial may be of some use to you.
Class | Package | Subclass (same pkg) | Subclass (diff pkg) | World | |
---|---|---|---|---|---|
public | + | + | + | + | + |
protected | + | + | + | + | |
no modifier | + | + | + | ||
private | + |
+ : accessible
blank : not accessible
Comparator cannot be resolved to a type
You can’t apply a comparator of Patients to objects that might not be Patients. So the fix was to remove a file which wasn’t needed, but which hadn’t caused a noticeable problem up to that point, since we’d been using AJ 1.6 for many years previously.
Comparator cannot be resolved to a type, what should i do
Have you imported the comparator API?
If not, Is your project on the build path? Maybe you have to configure your build path by making the src as source folder so that VsCode will prompt you to import Comparator.
1. Import the Comparator library
Add this to the top of your file:
2. Incorrect Java Path
VS Code may be looking at a jre instead of a JDK.
- hit ctrl + shift + p to bring up the command palette.
- Type >Java: Configure Java Runtime and hit enter
- Change the path to point to your JDK location. The default location is C:\Program Files\Java\jdkX.X.X_XXX
Java — Two Maven Errors — ‘Type Cannot Be Resolved’, I have two errors on an existing mvm project I am trying to build and not sure how to get these resolved. First the main one: [ERROR] Failed to execute goal org.codehaus.mojo:aspectj-maven-plugin:1.5:compile (default) on project ws: Compiler errors: The type java.util.Comparator cannot be resolved. It is indirectly …
Using iajc with java 1.8 JDK — Comparator cannot be resolved to a type
After some poking around, and a little bit of serendipity, I finally managed to find what was keeping the fixes I’d tried up to that point from solving the problem.
It came down to the existence of a copy of aspectjtools.jar within my %ANT_HOME%\lib folder. This JAR had been placed there many, many ( many !) years ago. It happened to be the AspectJ 1.6 version of the JAR and its existance there caused ant to use that version even though our build.xml file had been modified at some point to specify the path to AspectJ so an existence of the JAR under the ant lib was not needed to determine the version to use(and which I’d recently changed to point at AspectJ 1.8 instead, though the presense of the file made my change to build.xml ignored). Even though I thought I was now using AJ 1.8 with Java 1.8, I was not, so all the fundamental problems of incompatibility between older AJ and Java 1.8 remained.
Once I removed that JAR from the ant lib folder, my ant builds worked as expected without errors!
So the fix was to remove a file which wasn’t needed, but which hadn’t caused a noticeable problem up to that point, since we’d been using AJ 1.6 for many years previously. The update to the newer version didn’t really take effect because of the stray file’s existence. No file ==> build.xml config takes effect ==> compile works!
Attempting to override existing comparator for, What this means is that you’d have to provide an initial capacity to the queue, but you also get to craft your own custom comparator, which is what you want, without having to modify or touch the implementation of PriorityQueue. Code sampleComparator
Comparator won’t work with Arrays.sort
An ArrayList is different from a Java array; since you’re using a List, Arrays.sort won’t help you here.
Consider Collections.sort instead.
Ignoring your actual problem with Arrays.sort vs. Collections.sort (that has been beautifully answered), it might be a good idea to implement a Comparator instead of casting your Objects in the compare method:
public class CalorieComparator implements Comparator < @Override public int compare(Edible o1, Edible o2) < if (o1.getCalories() >o2.getCalories()) < return 1; >else if (o1.getCalories() < o2.getCalories()) < return -1; >else < return 0; >> >
Arrays.sort takes an array as its first argument. To sort a collection such as a list, use Collections.sort .
Collection.sort(elist, new CalorieComparator());
Also, note that your method won’t compile because you aren’t returning a List .
Comparator cannot be resolved to a type, what should i do, Import the Comparator library. 2. Incorrect Java Path. VS Code may be looking at a jre instead of a JDK. hit ctrl + shift + p to bring up the command palette. Change the path to point to your JDK location. The default location is C:\Program Files\Java\jdkX.X.X_XXX.
Trying to solve «The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments»
The problem is that the list you’re sorting has been declared as a list that can contain arbitrary objects. You can’t apply a comparator of Patients to objects that might not be Patients.
To solve it, declare your list as a list of Patients:
List list = new ArrayList<>(waitingList)
Java — cannot be resolved to a type?, The problem lies in the fact that «T» in the context of the compiler could be a type like any other. Using letters T U V are conventions that we use for generics, but you could just as easily write List
The type java.util.Comparator cannot be resolved. It is indirectly referenced from required .class files
Questions : The type java.util.Comparator cannot be resolved. It is indirectly referenced from required .class files
I am using Eclipse IDE, and I encountered this error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The type java.util.Comparator cannot be resolved. It is indirectly referenced from required .class files at project2.FeatureList.(FeatureList.java:1) at project2.EveryPlaceHasAName.main(EveryPlaceHasAName.java:81)
package project2; //line error referenced by "at project2.FeatureList.(FeatureList.java:1)" import java.util.ArrayList; import java.util.Collections; public class FeatureList extends ArrayList < /** * Creates an empty FeatureList. */ public FeatureList() < >. >
package project2; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.ArrayList; public class EveryPlaceHasAName < public static void main( String[] args ) < . FeatureList list = new FeatureList(); //line error referenced by "at project2.EveryPlaceHasAName.main(EveryPlaceHasAName.java:81)" . >>
This is the only error that is being displayed. Please help, I don’t understand why it is not compiling. I’m fairly new to programming, so please explain how to solve it in a simple way.
Answers 1 : of The type java.util.Comparator cannot be resolved. It is indirectly referenced from required .class files
In your eclipse’s preferences window, type jre in the filter box. The JRE(s) you have told eclipse about are listed here. Whichever one is enabled is broken. The answer will be in this dialog box; doublecheck that path, re-configure it, or point it at a proper JDK, etc.