import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
@Data
@AllArgsConstructor
@Builder
public class Fruit {
private Integer id;
private String name;
private Double money;
private Integer num;
}
测试用例
import org.junit.Before;
import org.junit.Test;
import java.util.*;
import java.util.stream.Collectors;
public class FruitTest {
List<Fruit> fruitList = null;
@Before
public void setup() {
fruitList = new ArrayList<>();
fruitList.add(new Fruit(1, "Apple1", 4.5, 11));
fruitList.add(new Fruit(1, "Apple2", 3.1, 2));
fruitList.add(new Fruit(2, "Banana", 4.89, 8));
fruitList.add(new Fruit(3, "Pear", 10.99, 9));
}
@Test
public void testGroupBy() {
System.out.println("testGroupBy()");
Map<Integer, List<Fruit>> map = fruitList.stream()
.collect(Collectors.groupingBy(Fruit::getId));
System.out.println(map);
}
/**
* 当id存在重复,如果不处理,就会抛出异常。下面通过(k1, k2) -> k1) 处理。
*/
@Test
public void testListToMap() {
System.out.println("testListToMap()");
Map<Integer, Fruit> map = fruitList.stream()
.collect(Collectors.toMap(Fruit::getId, a -> a, (k1, k2) -> k1));
System.out.println(map);
}
@Test
public void testFilter() {
System.out.println("testFilter()");
List<Fruit> ls = fruitList.stream()
.filter(a -> a.getName().equals("香蕉"))
.collect(Collectors.toList());
System.out.println(ls);
}
@Test
public void testMaxMin() {
System.out.println("testSum()");
Optional<Fruit> max = fruitList.stream().max(Comparator.comparingDouble(Fruit::getMoney));
max.ifPresent(System.out::println);
Optional<Fruit> min = fruitList.stream().min(Comparator.comparingDouble(Fruit::getMoney));
min.ifPresent(System.out::println);
}
/**
* 列表直接排序,会直接修改到原列表
*/
@Test
public void testSortChangeSelf() {
System.out.println(fruitList);
fruitList.sort(Comparator.comparingDouble(Fruit::getMoney));
System.out.println(fruitList);
}
/**
* 使用Stream+sort排序后,生成新的列表,不影响原列表
*/
@Test
public void testSortWithStream() {
System.out.println(fruitList);
// 使用价格排序
List<Fruit> ls = fruitList.stream()
.sorted(Comparator.comparingDouble(Fruit::getMoney))
.collect(Collectors.toList());
System.out.println(ls);
// 使用名称升序排序
ls = fruitList.stream()
.sorted(Comparator.comparing(Fruit::getName))
.collect(Collectors.toList());
System.out.println(ls);
// 使用名称降序排序
ls = fruitList.stream()
.sorted(Comparator.comparing(Fruit::getName).reversed())
.collect(Collectors.toList());
System.out.println(ls);
}
/**
* 集合去重(基本类型)
*/
@Test
public void testDistinct1() {
//简单字符串的去重
List<String> list = Arrays.asList("111", "222", "333", "111", "222");
list.stream().distinct().forEach(System.out::println);
}
/**
* 集合limit,返回前几个元素
*/
@Test
public void testLimit() {
List<String> list = Arrays.asList("333", "222", "111");
list.stream().limit(2).forEach(System.out::println);
}
/**
* 集合skip,删除前n个元素
*/
@Test
public void testSkip() {
List<String> list = Arrays.asList("333", "222", "111");
list.stream().skip(2).forEach(System.out::println);
}
/**
* 集合reduce,将集合中每个元素聚合成一条数据
*/
@Test
public void testReduce() {
String s = fruitList.stream()
.map(Fruit::getName)
.reduce("水果", (a, b) -> a + b);
System.out.println(s);
}
/**
* 总和1
*/
@Test
public void testSum() {
// 计算水果的总数量,使用sum函数
int sum = fruitList.stream().mapToInt(Fruit::getNum).sum();
System.out.println(sum);
// 计算水果的总数量,使用reduce函数
Optional<Integer> sum1 = fruitList.stream().map(Fruit::getNum).reduce((a, b) -> a + b);
sum1.ifPresent(System.out::println);
}
}