• collection 与stream与lambd表达式的结合使用


    Arraylist除了常用的add,get,set,remove,contains,size,indexof,sublist这些常用的增删改方法,还有已下

    1、void java.util.ArrayList.forEach(Consumer<? super String> action)
    Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. Unless otherwise specified by the implementing class, actions are performed in the order of iteration (if an iteration order is specified). Exceptions thrown by the action are relayed to the caller
     
    对list中的元素,遍历执行action
    2、void java.util.ArrayList.sort(Comparator<? super String> c)

    Sorts this list according to the order induced by the specified Comparator

    按照comparator的compare指定的排序规则。排序集合中内容

    New Comparator<String>(){

    @Override  //重写compare方法的实现(正数,0,负数分别对应大于,等于,小于)

              public int compare(String s1, String s2) { 

                  return (s1.compareTo(s2)); //按字典顺序比较两个字符串character,字典顺序越往后的index越大。Eg: “a”.compareTo(“c”):-2; “a”.compareTo(“ab”):-1; “d”.compareTo(“ab”): 3;

                  }}

    3、Stream<String> java.util.Collection.stream()
    Returns a sequential Stream with this collection as its source
    4、Stream<String> java.util.stream.Stream.filter(Predicate<? super String> predicate)

    Returns a stream consisting of the elements of this stream that match the given predicate.

    过滤出符合这些predicate条件的集合

    Eg: Stream.filter((p) -> (p.getSalary() > 1400))  就是过滤出这些工资大于1400的数据

    sorted(Comparator<? super T> comparator)

    min(Comparator<? super T> comparator)

    max(Comparator<? super T> comparator);

    limit(long maxSize);

     

    截止以上我们对集合以及stream的一些常用用法有了个大概的了解。下面我们再来学习下,如何结合lambda表达式来使用这些常用的集合以及stream的方法。

    1、首先了解下什么是lambda表达式

    Lambda表达式的语法

    基本语法:

     (parameters) -> expression

    (parameters) ->{ statements; }

     

    下面是Java lambda表达式的简单例子:

       // 1. 不需要参数,返回值为 5 

    ( ) -> 5 

       // 2. 接收一个参数(数字类型),返回其2倍的值 

    x -> 2 * x 

       // 3. 接受2个参数(数字),并返回他们的差值 

    (x, y) -> x – y 

       // 4. 接收2个int型整数,返回他们的和 

    (int x, int y) -> x + y 

       // 5. 接受一个 string 对象,并在控制台打印,不返回任何值(看起来像是返回void) 

    (String s) -> System.out.print(s)

     

    2、可以使用lambda表达式,代替实现ConsumerComparatorPredicate等对象

    1)  lambda表达式,代替实现Consumer

    arraylist.foreach(p->p+2)

     

    2)  lambda表达式,代替实现Comparator

    Array.sort(arraylist, New Comparator<String>(){ public int compare(String s1, String s2) {                return (s1.compareTo(s2));        }}

    )

    替换成

    Array.sort(arraylist,(s1,s2)->(s1.compareTo(s2)));

         3) lambda表达式,代替实现Predicate

    phpProgrammers.stream().filter((p) -> (p.getSalary() > 1400)) 

  • 相关阅读:
    教你怎样做个有“钱”途的測试project师
    使用具体解释及源代码解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter
    html-上左右布局方式---ShinePans
    POJ1502 MPI Maelstrom Dijkstra
    Angry IP Scanner 获取设备的IP
    【Struts2学习笔记(12)】Struts2国际化
    使用SQL Profile及SQL Tuning Advisor固定运行计划
    sage开发url替换字符串
    柯塔娜大合唱,互联网安全观
    vim水平摆放全部窗体的三个方法
  • 原文地址:https://www.cnblogs.com/happyliuyi/p/10966419.html
Copyright © 2020-2023  润新知