check if all Objects of a stream meet a rule [duplicate] - java

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));
}

Related

convert iterator to lambda [duplicate]

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.

Java 8: How Lambda expression impacts [duplicate]

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.

Finding element in LinkedList with lambda [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I do have a simple Object which can be identified by a String. These identifiers are unique.
public class SomeObject
{
private String identifier;
public String getIdentifier() {
return this.identifier;
}
}
I now have another class which holds a List of these simple Objects and has a method to return the object that matches the identifier provided if it exists in the list.
public class SomeBiggerClass
{
LinkedList<SomeObject> allObjects;
public SomeObject getObject(String identifier)
{
if (this.allObjects.stream().anyMatch(object -> object.getIdentifier() == identifier)) {
return this.allObjects.stream().filter(object -> object.getIdentifier() == identifier).findAny().get();
}
else {
return null;
}
}
}
However even if the Strings exactly match it will return false for the anyMatch() part and will throw an exception for the second lambda. I would be alble to do this in c# and linq, however I'm kinda lost with these java lambdas. Any ideas on how to do this would be apreciated.
You need to use .equals(...) instead of == in order to compare strings.

Java: Converting Integer to String. Comparing with == equals operator [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
public static void main(String[] args) {
Integer i = new Integer(4);
System.out.println(i.toString());
if (i.toString() == i.toString()) {
System.out.println("true how");
} else {
System.out.println("false how");
}
}
While executing above code, I am getting output as "false how".
Can you explain how Jvm treats this object?
toString() creates a new string object every time and your code is actually checking if both references are the same, which is never the case so it runs the else case. If you try
i.toString().equals(i.toString())
you'll get the desired output.
You must compare objects with equals() method.
i.toString().equals(i.toString())

Is there an "in" Python keyword equivalent in Java? [duplicate]

This question already has answers here:
How do I determine whether an array contains a particular value in Java?
(30 answers)
Closed 10 years ago.
What is the most simple/efficient way of implementing the following Python code in Java?
if "foo" in ["foo", "bar"]:
print "found."
If you have your data in array use this
String[] strings = {"foo","bar"};
for (String s : strings) {
if (s.equals("foo")) {
System.out.println("found");
break;
}
}

Categories