Guest User

Untitled

a guest
Jun 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.function.Predicate;
  3. import java.util.function.Function;
  4. import java.util.function.Supplier;
  5. import java.util.function.Consumer;
  6. class Person {
  7. String firstName;
  8. String lastName;
  9.  
  10. Person() {}
  11.  
  12. Person(String firstName, String lastName) {
  13. this.firstName = firstName;
  14. this.lastName = lastName;
  15. }
  16. }
  17. public class HelloWorld{
  18.  
  19. public static void main(String []args){
  20. // boolean-valued functions of one argument.contains various default //methods for composing predicates to complex logical terms (and, or, negate)
  21. Predicate<String> predicate = (s) -> s.length() > 0;
  22. //The generic String is what it will take on RHS
  23. predicate.test("foo"); // true
  24. predicate.negate().test("foo"); // false
  25. String str = "testString";
  26. boolean b = Objects.nonNull(str);
  27. Predicate<Boolean> nonNull = Objects::nonNull;
  28. //System.out.println(nonNull.test(true));
  29. //System.out.println(nonNull.negate().test(false));
  30. Predicate<Boolean> isNull = Objects::isNull;
  31. //System.out.println(isNull.test(true));
  32. //System.out.println(isNull.negate().test(false));
  33. Predicate<String> isEmpty = String::isEmpty;
  34. Predicate<String> isNotEmpty = isEmpty.negate();
  35.  
  36. //Suppliers produce a result of a given generic type. Unlike Functions//Suppliers don't accept arguments.
  37.  
  38. Supplier<Person> personSupplier = Person::new;
  39. //System.out.println(personSupplier.get()); // new Person
  40. Comparator<Person> comparator = (p1, p2) -> p1.firstName.compareTo(p2.firstName);
  41.  
  42. Person p1 = new Person("John", "Doe");
  43. Person p2 = new Person("Alice", "Wonderland");
  44. //Comparator has added new default methods
  45. comparator.compare(p1, p2); // > 0
  46. comparator.reversed().compare(p1, p2); // < 0
  47. //Functions accept one argument and produce a result. Default methods can be //used to chain multiple functions together (compose, andThen).
  48. Function<String, Integer> toInteger = Integer::valueOf;
  49. Function<String, String> backToString = toInteger.andThen(String::valueOf);
  50. backToString.apply("123"); // "123"
  51.  
  52.  
  53.  
  54. //Consumers represents operations to be performed on a single input argument.
  55.  
  56. Consumer<Person> greeter = (p) -> System.out.println("Hello, " + p.firstName);
  57. greeter.accept(new Person("Luke", "Skywalker"));
  58.  
  59.  
  60. /*
  61. A java.util.Stream represents a sequence of elements on which one or more operations can be performed. Stream operations are either intermediate or termina
  62. */
  63. List<String> stringCollection = new ArrayList<>();
  64. stringCollection.add("ddd2");
  65. stringCollection.add("aaa2");
  66. stringCollection.add("bbb1");
  67. stringCollection.add("aaa1");
  68. stringCollection.add("bbb3");
  69. stringCollection.add("ccc");
  70. stringCollection.add("bbb2");
  71. stringCollection.add("ddd1");
  72.  
  73. //Filter accepts a *predicate* to filter all elements of the stream. This operation is intermediate
  74.  
  75. //ForEach accepts a *consumer* to be executed for each element in the filtered stream. ForEach is a terminal operation.
  76. stringCollection
  77. .stream()
  78. .filter((s) -> s.startsWith("a"))
  79. .forEach(System.out::println);
  80.  
  81. // "aaa2", "aaa1"
  82.  
  83. /*
  84. Sorted is an intermediate operation which returns a sorted view of the streamThe elements are sorted in natural order unless you pass a custom Comparator.
  85. */
  86. stringCollection
  87. .stream()
  88. .sorted()
  89. .filter((s) -> s.startsWith("a"))
  90. .forEach(System.out::println);
  91.  
  92. // "aaa1", "aaa2"
  93.  
  94. /*
  95. map converts each element into another object via the given function and can also use map to transform each object into another type. The generic type of the resulting stream depends on the generic type of the function you pass to map.
  96. */
  97. stringCollection
  98. .stream()
  99. .map(String::toUpperCase)
  100. .sorted((c, d) -> c.compareTo(d))
  101. .forEach(System.out::println);
  102.  
  103. // "DDD2", "DDD1", "CCC", "BBB3", "BBB2", "AAA2", "AAA1"
  104.  
  105.  
  106. /*
  107. Match ops check whether a certain predicate matches the stream. All of those operations are terminal and return a boolean result.
  108. */
  109. boolean anyStartsWithA =
  110. stringCollection
  111. .stream()
  112. .anyMatch((s) -> s.startsWith("a"));
  113.  
  114. System.out.println(anyStartsWithA); // true
  115.  
  116. //Similarly are noneMatch,allMatch
  117.  
  118. //Reduce
  119. //terminal operation performs a reduction on the elements of the stream with//the given function. The result is an Optional holding the reduced value.
  120. Optional<String> reduced =
  121. stringCollection
  122. .stream()
  123. .sorted()
  124. .reduce((s1, s2) -> s1 + "#" + s2);
  125.  
  126. reduced.ifPresent(System.out::println);
  127. // "aaa1#aaa2#bbb1#bbb2#bbb3#ccc#ddd1#ddd2"
  128.  
  129.  
  130. }
  131. }
Add Comment
Please, Sign In to add comment