常用流操作
2018-05-18 本文已影响4人
大华夏
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.Set;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest {
/**
* 对集合元素进行筛选
*/
@Test
public void testStreamFilter() {
System.out.println("testStreamFilter");
List<Integer> vals = Stream.of(10, 20, 60, 900, 70, 120, 130, 160).collect(Collectors.toList());
Stream<Integer> stream = vals.stream().filter((a) -> {
return a > 100;
});
stream.forEachOrdered((a) -> System.out.println(a));
}
/**
* 从流对象转换成列表
*/
@Test
public void testStreamCollect() {
// System.out.println("testStreamCollect");
List<String> collected = Stream.of("a", "b", "c").collect(Collectors.toList());
assertEquals(Arrays.asList("a", "b", "c"), collected);
}
/**
* 在原有列表的基础上运用些规则,变成另一个列表
*/
@Test
public void testStreamMap() {
System.out.println("testStreamMap");
List<Integer> vas = Stream.of(1, 3, 4, 5, 6, 7).collect(Collectors.toList());
List<Integer> vas2 = vas.stream().map((a) -> {
return a + 10;
}).collect(Collectors.toList());
System.out.println(vas);
System.out.println(vas2);
}
/**
* 汇集多个列表的流对象到一个流对象
*/
@Test
public void testStreamFlatMap() {
System.out.println("testStreamFlatMap");
List<Integer> vals = Stream.of(Arrays.asList(1, 2, 4), Arrays.asList(56, 78, 97))
.flatMap(numbers -> numbers.stream()).filter((a) -> {
System.out.println("hello");
System.out.println(a);
return true;
}).collect(Collectors.toList());
System.out.println(vals);
}
/**
* 求最大最小值
*/
@Test
public void testStreamMinOrMax() {
System.out.println("testStreamMinOrMax");
List<Integer> vals = Stream.of(10, 20, 60, 900, 70, 120, 130, 160).collect(Collectors.toList());
int min = vals.stream().min(Comparator.comparing((a) -> a)).get();
int max = vals.stream().max(Comparator.comparing((a) -> a)).get();
System.out.println(min);
System.out.println(max);
}
class ValWrap {
public int intVal;
public long longVal;
public ValWrap() {
}
public ValWrap(int intVal) {
this.intVal = intVal;
}
public ValWrap(long longVal) {
this.longVal = longVal;
}
}
private List<List<ValWrap>> intValsList = Stream
.of(Stream.of(new ValWrap(3), new ValWrap(56), new ValWrap(30)).collect(Collectors.toList()),
Stream.of(new ValWrap(2), new ValWrap(34), new ValWrap(52)).collect(Collectors.toList()),
Stream.of(new ValWrap(43), new ValWrap(14), new ValWrap(23)).collect(Collectors.toList()))
.collect(Collectors.toList());
@Test
public void testStreamReduce() {
int sum = Stream.of(1, 2, 3).reduce(0, ((acc, element) -> acc + element));
System.out.println("testStreamReduce: " + sum);
assertEquals(6, sum);
}
/**
* 利用Stream函数进行链式操作
*/
@Test
public void testStream() {
Set<Integer> vals = intValsList.stream().flatMap((List<ValWrap> valWraps) -> valWraps.stream())
.filter(varWrap -> varWrap.intVal > 30).map(varWrap -> varWrap.intVal).collect(Collectors.toSet());
System.out.println("testStream: " + vals);
int sum = vals.stream().reduce(0, (acc,el)->acc+el);
System.out.println("sum: "+sum);
}
}