lambda

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cheidou123/article/details/89492428
      Optional<Long> time = deviceSearchResp.getBindCardInfoDoList().stream()
          .map(bindCardInfoDo -> LocalDateTime.parse(bindCardInfoDo.getCreatedAt(), FULL_FORMATTER)
          .atZone(ZoneId.systemDefault()).toInstant().getEpochSecond())
          .min(Long::compareTo);

可以得出最小时间的long类型

     Optional<LocalDateTime> time = deviceSearchResp.getBindCardInfoDoList().stream()
          .map(bindCardInfoDo -> LocalDateTime.parse(bindCardInfoDo.getCreatedAt(), FULL_FORMATTER))
          .min(LocalDateTime::compareTo);

可以获得最小时间LocalDateTime类型

package lambda;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author bincai, bincai@mobvoi.com
 * @date Apr 26 , 2019
 */
public class lambda1 {

  public static void main(String[] args) {
    filter();
    map();
    match();
    find();
    MaxMin();
    sum();
    sort();
    flatMap();
    peek();
  }

  // 过滤方法.
  public static void filter() {
    List<Integer> list = new ArrayList<>();
    list.add(1);
    list.add(2);
    list.add(3);
    list.add(4);
    list.add(5);
    List<Integer> list2 = list.stream().filter(u -> u > 3).collect(Collectors.toList());
    System.out.println(list.toString());  // [1, 2, 3, 4, 5]
    System.out.println(list2.toString()); // [4, 5]
    List<Object> list3 = new ArrayList<>();
    list3.add(1);
    list3.add(null);
    list3.add(3);
    list3.add(null);
    list3.add(5);
    List<Object> list4 = list3.stream().filter(Objects::nonNull).collect(Collectors.toList());
    System.out.println(list3.toString());  // [1, null, 3, null, 5]
    System.out.println(list4.toString());  // [1, 3, 5]
  }

  // map方法.
  public static void map() {
    Person person1 = new Person();
    person1.age = 30;
    person1.name = "person1";
    Person person2 = new Person();
    person2.age = 40;
    person2.name = "person2";
    Person person3 = new Person();
    person3.age = 50;
    person3.name = "person3";
    List<Person> personList = new ArrayList<>();
    personList.add(person1);
    personList.add(person2);
    personList.add(person3);
    Optional<Integer> age = personList.stream().map(person -> person.age).max(Integer::compareTo);
    System.out.println(age.get()); // 50
  }

  // map方法.
  public static void match() {
    Person person1 = new Person();
    person1.age = 30;
    person1.name = "person1";
    person1.love = "dou";
    Person person2 = new Person();
    person2.age = 40;
    person2.name = "person2";
    person2.love = "dou";
    Person person3 = new Person();
    person3.age = 50;
    person3.name = "person3";
    person3.love = "dou";
    Person person4 = new Person();
    person4.age = 50;
    person4.name = "person4";
    person4.love = "dou";
    List<Person> personList = new ArrayList<>();
    personList.add(person1);
    personList.add(person2);
    personList.add(person3);
    personList.add(person4);
    Integer num = 60;
    String love = "dou";
    // 1.noneMatch 如果全都没有匹配,返回true,否则返回false.
    System.out
        .println(personList.stream().map(Person::getAge).noneMatch(age -> age.equals(50))); //false
    System.out
        .println(personList.stream().map(Person::getAge).noneMatch(num::equals));           //true
    // 2.anyMatch 如果有匹配,返回true,否则返回false.
    System.out
        .println(personList.stream().map(Person::getAge).anyMatch(age -> age.equals(50))); //true
    System.out
        .println(personList.stream().map(Person::getAge).anyMatch(num::equals));           //false
    // 3.allMatch 如果全都匹配,返回true,否则返回false.
    System.out
        .println(personList.stream().map(Person::getLove).allMatch(love::equals));         //true
    System.out
        .println(personList.stream().map(Person::getLove).allMatch(num::equals));          //false
  }

  public static void find() {
    Person person1 = new Person();
    person1.age = 30;
    person1.name = "person1";
    person1.love = "dou";
    Person person2 = new Person();
    person2.age = 40;
    person2.name = "person2";
    person2.love = "dou";
    Person person3 = new Person();
    person3.age = 50;
    person3.name = "person3";
    person3.love = "dou";
    Person person4 = new Person();
    person4.age = 50;
    person4.name = "person4";
    person4.love = "dou";
    List<Person> personList = new ArrayList<>();
    personList.add(person1);
    personList.add(person2);
    personList.add(person3);
    personList.add(person4);
    // findFirst. 取第一个
    System.out.println(personList.stream().findFirst().get().getName()); //person1
    // findAny. 任意取一个
    System.out.println(personList.stream().findAny().get().getAge());    //30
  }

  public static void MaxMin() {
    Person person1 = new Person();
    person1.age = 30;
    person1.name = "person1";
    person1.love = "dou";
    Person person2 = new Person();
    person2.age = 40;
    person2.name = "person2";
    person2.love = "dou";
    Person person3 = new Person();
    person3.age = 50;
    person3.name = "person3";
    person3.love = "dou";
    Person person4 = new Person();
    person4.age = 50;
    person4.name = "person4";
    person4.love = "dou";
    List<Person> personList = new ArrayList<>();
    personList.add(person1);
    personList.add(person2);
    personList.add(person3);
    personList.add(person4);
    // max. 取最大值
    System.out.println(personList.stream().map(person -> person.age).max(Integer::compareTo).get());
    // min. 取最小值
    System.out.println(personList.stream().map(person -> person.age).min(Integer::compareTo).get());
  }

  public static void sum() {
    Person person1 = new Person();
    person1.age = 30;
    person1.name = "person1";
    person1.love = "dou";
    Person person2 = new Person();
    person2.age = 40;
    person2.name = "person2";
    person2.love = "dou";
    Person person3 = new Person();
    person3.age = 50;
    person3.name = "person3";
    person3.love = "dou";
    Person person4 = new Person();
    person4.age = 50;
    person4.name = "person4";
    person4.love = "dou";
    List<Person> personList = new ArrayList<>();
    personList.add(person1);
    personList.add(person2);
    personList.add(person3);
    personList.add(person4);
    // 求和,必须是对应类型
    System.out.println(personList.stream().mapToInt(person -> person.age).sum()); //170
  }

  private static void sort() {
    Person person1 = new Person();
    person1.age = 60;
    person1.name = "person1";
    person1.love = "dou";
    Person person2 = new Person();
    person2.age = 40;
    person2.name = "person2";
    person2.love = "dou";
    Person person3 = new Person();
    person3.age = 50;
    person3.name = "person3";
    person3.love = "dou";
    Person person4 = new Person();
    person4.age = 50;
    person4.name = "person4";
    person4.love = "dou";
    List<Person> personList = new ArrayList<>();
    personList.add(person1);
    personList.add(person2);
    personList.add(person3);
    personList.add(person4);
    personList = personList.stream()
        .sorted(Comparator.comparing(Person::getAge).reversed())
        .limit(4)
        .collect(Collectors.toList());
    personList.forEach(person -> {
      System.out.print(person.getName() + ",");  //person1,person3,person4,person2,
    });
    System.out.println("----------");
  }

  private static void flatMap() {
    List<String> hobbyList = new ArrayList<>();
    hobbyList.add("abc");
    Person person1 = new Person();
    person1.age = 60;
    person1.name = "person1";
    person1.love = "dou";
    person1.hobby = hobbyList;
    Person person2 = new Person();
    person2.age = 40;
    person2.name = "person2";
    person2.love = "dou";
    person2.hobby = hobbyList;
    Person person3 = new Person();
    person3.age = 50;
    person3.name = "person3";
    person3.love = "dou";
    person3.hobby = hobbyList;
    Person person4 = new Person();
    person4.age = 50;
    person4.name = "person4";
    person4.love = "dou";
    person4.hobby = hobbyList;
    List<Person> personList = new ArrayList<>();
    personList.add(person1);
    personList.add(person2);
    personList.add(person3);
    personList.add(person4);
    List<String> list = personList.stream()
        .flatMap(person -> person.getHobby().stream())
        .collect(Collectors.toList());
    list.forEach(person -> {
      System.out.print(person + ",");  //abc,abc,abc,abc,
    });
  }

  public static void peek(){
    String ex = "出门问问#智能手表";
    List<String> list = new ArrayList<>();
    Stream.of(ex.split("#")).peek(e->System.out.println(e)).
        forEach(
            e ->list.add(e)
        );
    System.out.println(list);
    // 出门问问
    // 智能手表
    // [出门问问, 智能手表]
  }

  public static void count() {
    List<String> list = new ArrayList<>();
    list.add("123");
    list.add("124");
    list.add("234");
    System.out.println(list.stream().filter(e -> e.startsWith("1"))
        .count());   //2
  }

  public static void reduce() {
    List<String> stringCollection = new ArrayList<>();
    stringCollection.add("ddd2");
    stringCollection.add("aaa2");
    stringCollection.add("bbb1");
    stringCollection.add("aaa1");
    stringCollection.add("bbb3");
    stringCollection.add("ccc");
    stringCollection.add("bbb2");
    stringCollection.add("ddd1");
    Optional<String> reduced =
        stringCollection
            .stream()
            .sorted()
            .reduce((s1, s2) -> s1 + "#" + s2);
    reduced.ifPresent(System.out::println);
// "aaa1#aaa2#bbb1#bbb2#bbb3#ccc#ddd1#ddd2"
  }

  public static void orElse() {
    List<String> list = new ArrayList<>();
    System.out.println(list.stream().findAny().orElse(null));  // null
    list.add("123");
    System.out.println(list.stream().findAny().orElse(null));  // 123
    list.add("123");
    System.out.println(list.stream().findAny());  // Optional[123]

}

class Person {

  public int age;

  public String name;

  public String love;

  public List<String> hobby;

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getLove() {
    return love;
  }

  public void setLove(String love) {
    this.love = love;
  }

  public List<String> getHobby() {
    return hobby;
  }

  public void setHobby(List<String> hobby) {
    this.hobby = hobby;
  }
}

hashmap 相关

package study;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

/***
 * @author bincai
 * @email 1355869831@qq.com
 */
public class Test {
    public static void main(String[] args) {
        // 首先我们要知道对hashmap排序并重新返回一个hashmap并么有意义.
        Map<String,Integer> map = new HashMap<>();
        map.put("test",4);
        map.put("tes",3);
        map.put("te",5);
        System.out.println(map.toString());
        // 升序排.
        Map<String,Integer> map2 = map.entrySet().stream()
                .sorted(Map.Entry.comparingByValue()).collect(Collectors.toMap(
                       Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
        System.out.println(map2.toString());
        // 降序排.
        Map<String,Integer> map3 = map.entrySet().stream()
                        .sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).collect(Collectors.toMap(
                Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
        System.out.println(map3.toString());
        // 找出最大的元素.
        Map<String,Integer> map4 = map.entrySet().stream()
                .sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).limit(1).collect(Collectors.toMap(
                        Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
        System.out.println(map4.toString());
      }
}

输出:

{te=5, tes=3, test=4}
{tes=3, test=4, te=5}
{te=5, test=4, tes=3}
{te=5}
展开阅读全文

没有更多推荐了,返回首页