-Filter the data that matches the conditions. -For the argument, write a conditional expression in which boolean is the return value.
Object name.stream (). Filter (dummy argument-> conditional expression);
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("apple");
list.add("orange");
list.add("banana");
list.stream()
.filter(i -> i != "apple");//from list"Except for apple is extracted"
}
-Primitive types that do not implement Comparatable can only be sorted in ascending order -For types that implement Comparatable (String, Integer, etc.), you can specify ascending and descending order with Comparator. In the case of ascending order, Comparator.naturalOrder () can be omitted.
Object name.stream (). Sorted (ascending or descending);
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("lemon");
list.add("orange");
list.add("apple");
list.stream().sorted(Comparator.naturalOrder());//ascending order
list.stream().sorted(Comparator.reverseOrder());//descending order
}
map() ยท Convert elements in an object
Object name.stream (). Map (dummy argument-> conversion process);
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("apple");
list.add("orange");
list.add("banana");
List<String> ret = list.stream().map(s -> s.toUpperCase());//Convert from lowercase to uppercase
}
Recommended Posts