There are following way to Check If All Values in a Object Are Greater Than Given Value from java 8?
- Create a HelloObject class with attribute firstAttributeValue and secondAttributeValue of int type.
- Create a list of HelloObject with argument type constructor.
- Now get the Stream data from List using listObjects.stream() method.
- Use allMatch() method to check if r.getFirstAttributeValue() > 0 for all elements then it return Boolean true or false.
import java.util.Arrays;
import java.util.List;
class HelloObject {
int firstAttributeValue;
int secondAttributeValue;
public HelloObject(int firstAttributeValue, int secondAttributeValue) {
super();
this.firstAttributeValue = firstAttributeValue;
this.secondAttributeValue = secondAttributeValue;
}
public int getFirstAttributeValue() {
return firstAttributeValue;
}
public void setFirstAttributeValue(int firstAttributeValue) {
this.firstAttributeValue = firstAttributeValue;
}
public int getSecondAttributeValue() {
return secondAttributeValue;
}
public void setSecondAttributeValue(int secondAttributeValue) {
this.secondAttributeValue = secondAttributeValue;
}
@Override
public String toString() {
return "HelloObject [firstAttributeValue=" + firstAttributeValue + ", secondAttributeValue="
+ secondAttributeValue + "]";
}
}
public class MainProgram {
public static void main(String[] args) {
List<HelloObject> listObjects = Arrays.asList(new HelloObject(1, 2), new HelloObject(2, 3),
new HelloObject(5, 6), new HelloObject(7, 8));
boolean status = listObjects.stream().allMatch(r -> r.getFirstAttributeValue() > 0);
System.out.println("All attribute value is greater than given Value :- " + status);
}
}
Output :-
All attribute value is greater than given Value :- true