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.
Related
This question already has answers here:
How do I replace an anonymous class with a lambda in Java?
(5 answers)
Closed 1 year ago.
My linter advises me to refactor this using lambda expressions, but I can't figure out how to do it for this piece of code
return new Iterable<Message>() {
#Override
public Iterator<Message> iterator() {
return new UnprocessedIterator(message);
}
};
Something like return () -> new UnprocessedIterator(message); should work.
This question already has answers here:
how to check if all elements of java collection match some condition?
(3 answers)
Is it possible to check whether all Java 8 stream elements satify one of given predicates?
(3 answers)
Closed 3 years ago.
I want to check if all objects of a stream meet a rule, and returns True only if all of them meets the rule,
but I have a compilation error: Role cannot be applied to lambda parameter
public static Predicate<Hostel> areAllTrue() {
return req -> req.getRole().stream(r -> isTrue(r));
}
private static boolean isTrue(HostelRole hostelRole) {
}
Use the terminal operation allMatch:
public static Predicate<Hostel> areAllTrue() {
return req -> req.getRole().stream().allMatch(r -> isTrue(r));
}
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?
This question already has answers here:
Implementing toString on Java enums
(4 answers)
Closed 8 years ago.
I'm new to Java and I'm learning the language fundamentals.
Can someone explain to me how the toString method is called when there is no function call to it? I think it has something to do with the actual enumerator words on the second line such as:
KALAMATA("Kalamata"), LIGURIO("Ligurio") ...
The whole purpose for this enum class is so the ENUM values don't print to screen in all upper case characters.
Can someone please explain me how toString method is used in this class? Like when is it called? How is it called?
public enum OliveName {
KALAMATA("Kalamata"),LIGURIO("Ligurio"),PICHOLINE("Picholine"),GOLDEN("Golden");
private String nameAsString;
//for enum classes, the constructor must be private
private OliveName(String nameAsString) {
this.nameAsString = nameAsString;
}
#Override
public String toString() {
return this.nameAsString;
}
}
Pretty much like any object.
OliveName oliveName = OliveName.KALAMATA;
System.out.println(oliveName.toString());
or
System.out.println(oliveName);
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.