This question already has answers here:
Anonymous inner class using an interface in Java
(1 answer)
Multiple inheritance for an anonymous class
(6 answers)
Closed 5 years ago.
So here is code which implements a min-heap :
PriorityQueue<String> minHeap = new PriorityQueue<>(k,new Comparator<String>(){
public int compare(String s1, String s2){
return Integer.compare(s1.length(), s2.length());
}
});
I am confused about the compare function. It appears we are overriding the compare function in Comparator but how? Where can I learn about overriding methods upon object instantiation in java?
Related
This question already has answers here:
Remove object from ArrayList with some Object property
(5 answers)
Remove objects from an ArrayList based on a given criteria
(10 answers)
How to remove specific object from ArrayList in Java?
(17 answers)
Closed 1 year ago.
I have a class with its own hashCode() method. I am adding this class to a HashSet. How can I remove an item by its hashCode, without knowing the object itself?
For example, if I have the following code
HashSet<Data> set = new HashSet<>();
set.add(new Data(10, 5));
...
class Data {
public int importantVal;
public int notImportantVal;
//... constructor ...
#Override
public int hashCode() {
return importantVal;
}
}
and I knew the importantVal of a Data object, but not the object itself. How would I remove it? set.remove(10) does not work.
Best solution I can think of is to also override equals() to return if importantVal is the same, and then do set.remove(new Data(10, anyPlaceholderValue))
This question already has answers here:
Performance difference between Java 8 lambdas and anonymous inner classes
(2 answers)
Does a lambda expression create an object on the heap every time it's executed?
(3 answers)
Closed 7 years ago.
I was using java 8 new feature 'Lambda expression' its cool feature to use. I think it helps only developer to simplify the coding. Does it have any performance impact on my application.
private void sortCities(List<String> cities){ //Conventional way
Collections.sort(cities, new Comparator<String>() {
#Override
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
});
}
private void sortCities(List<String> cities){ //Using Lambda Expression
Collections.sort(cities, (s1, s2) -> s1.compareTo(s2));
}
No, it does not. In your particular case (without capturing arguments) lambda will be actually more performant as the comparator will be created only once and reused, while with anonymous class it will be created each time you call the sortCities method.
This question already has answers here:
Sort ArrayList of custom Objects by property
(29 answers)
Closed 8 years ago.
I would like to sort a List/Set of Class given a personnal order (like A.class < B.class < C.class for instance). But Class doesn't implements Comparable, and I obviously can't customize the class Class without rewriting the whole Java language, so what are my options?
Thanks a lot !
You can use a custom Comparator. Just implement that interface and you can use it with Collections.sort() and sorted sets (pass the comparator as constructor)
Comparator<Class<?>> c=new Comparator<Class<?>>{
#Override
int compare(Class<?> a, Class<?> b) {
// your comparison logic
}
};
List<Class<?>> list= ...
Collections.sort(list, c);
Set<Class<?>> set=new TreeSet<>(c);
This question already has answers here:
Does Java have "properties" that work the same way properties work in C#?
(5 answers)
Closed 9 years ago.
Is there an equivalent of the C# property call in java?
protected int foo2{ get; set; }
instead of doing this all the time:
private int foo2;
public void setfoo2 (int value) {foo2 = value;}
public int getfoo2 () {return foo2;}
nope.
they are different languages. different ways of doing things. BTW c# too does the same thing in the background. what c# gives is what we call syntactic sugar. it gives a shorthand writing.
This question already has answers here:
Sorting a list of points with Java [duplicate]
(4 answers)
How do I use Comparator to define a custom sort order?
(9 answers)
Closed 9 years ago.
I have an objects class that holds the properties title, director, genre, rating. I have created an arraylist and have filled it with instances of this base class
ArrayList<Movie> movieCatalog
I am wanting to sort this ArrayList in alphabetical order by the title property and change their positions within the ArrayList. From my research I understand I need to use a Comparator class but I am confused about how to do this.
You can create a custom comparator and than call the Collections.sort(movieCatalog,comparator); method.
E.g.
public static final Comparator<movieCatalog> movieComparator = new Comparator<movieCatalog>() {
public int compare(movieCatalog a1, movieCatalog a2) {
return a1.name.compareTo(a2.name);
}
};
Collections.sort(movieCatakig,movieComparator);
Your Movie class needs to implement Comparable<Movie>. Then you need to implement the compareTo method, which in this case can just call the String class compareTo method.
int compareTo(Movie other) {
return this.title.compareTo(other.title);
}
After that, if movies is an ArrayList of movies, you can simply do
Collections.sort(movies);