zoukankan      html  css  js  c++  java
  • 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)) 

  • 相关阅读:
    操作系统:DOS
    袁氏-人物:袁淑
    移植linux-2.6.32.2到qq2440
    qq2440启动linux后出现错误提示request_module: runaway loop modprobe binfmt-464c
    qq2440启动linux后插入u盘出现usb 1-1: device descriptor read/64, error -110,usb 1-1: device not accepting address 8, error -110
    ubuntu14.04 64bit安装teamviewer
    vi 的使用
    添加了环境变量,然而交叉编译器还是无法运行的解决方案
    linux下route命令使用实战
    ubuntu14.04纯命令行下连接有线网和无线网
  • 原文地址:https://www.cnblogs.com/happyliuyi/p/10966419.html
Copyright © 2011-2022 走看看