Common Scenario Code Examples of Lambda in java

  • 2021-07-18 08:03:30
  • OfStack

In this paper, we share the specific codes of Lambda commonly used scenarios in java for your reference. The specific contents are as follows


public class test18 {
  /**
   * lambda Common scenarios for expressions 
   */
  @Test
  public void test() {
    List<String> list_one = new ArrayList<>();
    list_one.add("NIKE");
    list_one.add("Addidas");
    /**
     *  Abbreviation for anonymous inner classes  ()->
     */
    new Thread(new Runnable() {
      @Override
      public void run() {
        System.out.println("do nothing");
      }
    }).start();
    new Thread(() -> System.out.println("do nothing")).start();
 
    // Used in set comparators 
    Collections.sort(list_one, new Comparator<String>() {
      @Override
      public int compare(String o1, String o2) {
        return o1.compareTo(o2);
      }
    });
    Collections.sort(list_one, (o1, o2) -> o1.compareTo(o2));// Positive order 
    Collections.sort(list_one, (o1, o2) -> -o1.compareTo(o2));// Reverse order 
 
    // Used to traverse a collection   Or manipulate elements in the collection 
    list_one.forEach(System.out::println);
    List result = new ArrayList();
    list_one.stream().forEach(item -> result.add(item));
 
 
    System.out.println("--boundary--");
 
    // Through customization filter Method   Or   Set .stream().filter(Predicate<>)
    filter(list_one, n -> n.startsWith("N"));
    filter(list_one, n -> true);
    filter(list_one, n -> false);
    filter(list_one, n -> n.length() > 5);
 
    Predicate<String> predicate = n -> n.startsWith("N");
    list_one.stream().filter(predicate).forEach(System.out::print);
    List<Integer> list3 = new ArrayList<>();
    list3.add(10);
    list3.add(8);
    list3.add(3);
    list3.add(15);
    list3.add(20);
    Predicate<Integer> predicate1 = n -> Integer.valueOf(n) > 10;
 
    // Used for map reduce Medium   Here first filter Delete data   Then execute map Operation 
    Double aDouble = list3.stream().filter(predicate1).map(vo -> vo * .12 + vo).reduce((sum, vo) -> sum + vo).get();
    System.out.println(aDouble);
 
    //stream() After that, you can pass distinct() Weight removal   It can also be passed through stream().collect(Collectors.toSet()) Weight removal  
    //Collector.joining(delimiter) Connecting into strings by delimiters 
    List<String> list4 = new ArrayList<>();
    list4.add("Hello");
    list4.add("Boy");
    list4.add("How");
    list4.add("Are");
    list4.add("You");
    list4.add("You");
    list4.stream().distinct().collect(Collectors.toList()).forEach(System.out::print);
    System.out.println(list4.stream().map(x -> x.toUpperCase()).collect(Collectors.joining(", ")));
 
    /**
     *  Pass mapTOInt() mapToDouble() mapToLong()  And then summarStatistics()  Available  IntSummaryStatistics  An instance of this class 
     *  And then call its getSum() getAverage() getCount() getMax() getMin() Method 
     */
    List<Integer> list5 = Arrays.asList(1, 3, 4, 5, 6, 7, 10, 23);
    IntSummaryStatistics intSummaryStatistics = list5.stream().mapToInt(x -> x).summaryStatistics();
    System.out.println(intSummaryStatistics.getSum());
    System.out.println(intSummaryStatistics.getAverage());
    System.out.println(intSummaryStatistics.getCount());
    System.out.println(intSummaryStatistics.getMax());
    System.out.println(intSummaryStatistics.getMin());
  }
 
  public void filter(List<String> names, Predicate<String> condition) {
    names.stream().filter(name -> condition.test(name)).forEach(vo -> System.out.print(vo + " "));
    System.out.println();
  }
}
 

Related articles: