jdk8的stream流式计算的操作

jdk8之后增加了流式计算,现在根据学习了流式计算,并把过程记录下来:

  Person.java的bean

package com.zhang.collectordemo;

/**
* @program: utilsdemo
* @description: pojo的测试类
* @author: zhangyu
* @create: 2019-06-18 13:51
*/
public class Person {
private Integer id;
private String name;

public Person(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
1) 将list变成map

package com.zhang.collectordemo;

/**
* @program: utilsdemo
* @description: 测试map
* @author: zhangyu
* @create: 2019-06-18 14:08
*/

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class TestMapDemo {

@Test
public void fun() {
List<Person> list = new ArrayList();
list.add(new Person(1, "zhangsan"));
list.add(new Person(2, "lisi"));
list.add(new Person(3, "wangwu"));
// jdk8将链表变成map的方法
Map<Integer, Person> personMap = list.stream().collect(Collectors.toMap(Person::getId, Function.identity()));
// Map<Integer, Person> personMap = list.stream().collect(Collectors.toMap(Person::getId, e->e));
System.out.println(personMap.get(1));
// 从map中添加数据
System.out.println(personMap.get(2).getName());
Map<Integer, String> map = list.stream().collect(Collectors.toMap(Person::getId, Person::getName));
map.put(4, "zhangyu");
System.out.println(map);
}
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 2) 通过流计算重复出现次数:

@Test
public void fun() {
List<String> items = Arrays.asList("apple", "apple", "banana", "apple", "orange", "banana", "papaya");
Map<String, Long> result = items.stream().collect(Collectors.groupingBy(e -> e, Collectors.counting()));
System.out.println(result);
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

3)测试对元素进行转换: 

@Test
public void fun2() {
List<Integer> s = Arrays.asList(1, 2, 3, 4);
// s.map(x->x*2).forEach(System.out::print);
s.stream().mapToInt(x -> x).forEach(System.out::print);
System.out.println();
s.stream().mapToDouble(x -> x).forEach(System.out::println);
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

4) 测试获取链表中最大值,并且当链表为null时候,设置一个默认最大值。

@Test
public void fun4() {
List<Integer> s = Arrays.asList(-1, -2, -3, -4);
// s.map(x->x*2).forEach(System.out::print);
// s.stream().mapToInt(x -> x).forEach(System.out::print);
// System.out.println();
// double变成int可以用Double的intValue的类
int max = s.stream().mapToInt(x -> x).max().orElse(0);
System.out.println(max);
}
5)测试filter和groupby的操作

package com.zhang.collectordemo;

/**
* @program: utilsdemo
* @description: 测试map
* @author: zhangyu
* @create: 2019-06-18 14:08
*/

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TestMapDemo {

@Test
public void fun() {
List<Person> list = new ArrayList();
list.add(new Person(1, "zhangsan"));
list.add(new Person(2, "lisi"));
list.add(new Person(3, "lisi"));
list.add(new Person(3, "wangwu"));
list.add(new Person(4, null));
// jdk8 将链表变成map的方法
/* Map<String, Person> personMap = list.stream().filter(e -> (e.getName() != null)).collect(Collectors.toMap(Person::getName, e -> e));
System.out.println(personMap);
System.out.println(personMap.get(null));
// 从map中添加数据
// System.out.println(personMap.get(2).getName());
// Map<Integer, String> map = list.stream().collect(Collectors.toMap(Person::getId, Person::getName));
// map.put(4, "zhangyu");
// System.out.println(map);
List<Person> newList = list.stream().filter(e -> (e.getName() != null)).collect(Collectors.toList());
System.out.println(newList);*/
Map<String, List<Person>> map = list.stream().filter(e -> (e.getName() != null)).collect(Collectors.groupingBy(Person::getName));
for (String name : map.keySet()) {
// System.out.println(map.get(name).get(0).getName());
for (Person person : map.get(name)) {
System.out.println(person.getName() + "---" + person.getId());
}
}
}
}

 
---------------------

posted on 2019-07-21 03:41  激流勇进1  阅读(293)  评论(0编辑  收藏  举报