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.
Related
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:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I'm making something but I can't seem to make it work. I get a nullpointerexception, but I can't figure out why.
public class Bus implements Serializable {
ArrayList<Reiziger> reizigers;
public String add(Reiziger reiziger) {
reizigers.add(reiziger);
return "lijst";
}
}
The nullpointer happens at line "reizigers.add(reiziger);" in the add method of Bus.java
You should initialize ArrayList<Reiziger> reizigers before use it:
List<Reiziger> reizigers = new ArrayList<>();
Also, note that List interface type is used for reizigers collections instead of ArrayList implementation. Read here the reason
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:
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.