Lot of examples on Collectors here https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class CSV { public static void main(String[] args) { List<String> names = Arrays.asList("a", "b", "c"); System.out.println(names.stream().map(String::toUpperCase).collect(Collectors.toList())); System.out.println(names.stream().map(String::toUpperCase).collect(Collectors.joining(","))); } }
interface Util { public static int numberOfCores() { return Runtime.getRuntime().availableProcessors(); } } public class IFWithStaticMethod { public static void main(String[] args) { System.out.println(Util.numberOfCores()); } }
interface Fly { default void takeoff() { System.out.println("Fly:takeoff"); getState(); } void getState(); } public class IFDefault implements Fly { public static void main(String[] args) { IFDefault default1 = new IFDefault(); default1.takeoff(); } @Override public void getState() { System.out.println("IFDefault:getState"); } }
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.Comparator; enum GENDER { MALE, FEMALE } class Person { @Override public String toString() { return "Person [name=" + name + ", gender=" + gender + ", age=" + age + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public GENDER getGender() { return gender; } public void setGender(GENDER gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } String name; GENDER gender; int age; public Person(String name, GENDER gender, int age) { super(); this.name = name; this.gender = gender; this.age = age; } } public class Sorting { public static List<Person> getPersons() { return Arrays.asList(new Person("Sara", GENDER.FEMALE, 20), new Person("Sara", GENDER.FEMALE, 22), new Person("Bob", GENDER.MALE, 20), new Person("Paula", GENDER.FEMALE, 32), new Person("Paul", GENDER.MALE, 32), new Person("Jack", GENDER.MALE, 2), new Person("Jack", GENDER.MALE, 72), new Person("Jill", GENDER.FEMALE, 12)); } public static void main(String[] args) { System.out.println(getPersons().stream().sorted(Comparator.comparing(Person::getName)).collect(Collectors.toList())); } }
import java.util.function.Predicate; public class TestPredicate { public static void main(String[] args) { Predicate<Integer> isOdd = (param) -> param % 2 == 0; Predicate<Integer> isOddAndGreaterThan5 = isOdd.and((param) -> param.intValue() > 5); System.out.println(isOdd.test(2)); System.out.println(isOdd.test(3)); System.out.println(isOddAndGreaterThan5.test(3)); System.out.println(isOdd.test(6)); } }
No comments:
Post a Comment