标签:value double extends 耗资源 流转 java 常用 combiner 处理过程
Java8新增了Stream,IntStream,LongStream,DoubleStream等流式API,这些API代表多个支持串行和并行聚集操作的元素。Stream是一个通用的流接口,而IntStream,LongStream,DoubleStream则代表元素类型为int,long,double的流。
Stream提供了大量的方法进行聚集操作,这些方法可以是“中间的(intermediate)”,也可以是“末端的(terminal)”。
Stream介绍:
先说下Stream的优势:它是java对集合操作的优化,相较于迭代器,使用Stream的速度非常快,并且它支持并行方式处理集合中的数据,默认情况能充分利用cpu的资源。同时支持函数式编程,代码非常简洁。
Stream是一种用来计算数据的流,它本身并没有存储数据。你可以认为它是对数据源的一个映射或者视图。
它的工作流程是:获取数据源->进行一次或多次逻辑转换操作->进行归约操作形成新的流(最后可以将流转换成集合)。
Stream的特性
例子一:
Stream<String> stream1 = (Stream<String>) Stream.<String>builder().add("a").add("b").build();
stream1.forEach(s -> System.out.println("str:" + s));
例子二:
Stream<String> stream2 = Stream.of(new String[]{"c", "d"});
stream2.forEach(s -> System.out.println("str:" + s));
例子三:
String[] strArray = new String[]{"a", "b", "c"};
Stream<String> stream3 = Arrays.stream(strArray);
stream3.forEach(s -> System.out.println("str:" + s));
例子四:
String[] strArray = new String[]{"a", "b", "c"};
List<String> list = Arrays.asList(strArray);
Stream<String> stream4 = list.stream();
stream4.forEach(s -> System.out.println("str:" + s));
流的操作类型分2种:中间操作与末端操作
中间操作就是对容器的处理过程,包括:排序(sorted...),筛选(filter,limit,distinct...),映射(map,flatMap...)等
示例代码:
List<String> aslist = Arrays.asList("1", "2", "5", "2", "3", "3");
/**
* filter
*/
aslist.stream().filter(s -> (Integer.parseInt(s) > 2)).forEach(s -> System.out.println("str:" + s));
//Stream filter method
Stream<T> filter(Predicate<? super T> predicate);
运行结果:
str:5
str:3
str:3
示例代码:
List<String> aslist = Arrays.asList("1", "2", "5", "232", "3", "13");
/**
* mapToInt
*/
aslist.stream().mapToInt(s -> Integer.parseInt(s)).forEach(i -> System.out.println("int:" + i));
/**
* map
*/
aslist.stream().map(s -> s.length()).forEach(s -> System.out.println("len:" + s));
//Stream map method
<R> Stream<R> map(Function<? super T, ? extends R> mapper);
//Stream mapToInt method
IntStream mapToInt(ToIntFunction<? super T> mapper);
运行结果:
int:1
int:2
int:5
int:232
int:3
int:13
len:1
len:1
len:1
len:3
len:1
len:2
示例代码:
/**
* peek
*/
aslist.stream().peek(new Consumer<String>() {
@Override
public void accept(String s) {
//操作
}
}).forEach(s -> System.out.println("peek:" + s));
//Stream peek method
Stream<T> peek(Consumer<? super T> action);
示例代码:
List<String> aslist = Arrays.asList("1", "2", "5", "3", "3", "2");
/**
* distinct[equals()返回true]
*/
aslist.stream().distinct().forEach(s -> System.out.println("str:" + s));
//Stream distinct method
Stream<T> distinct();
运行结果:
str:1
str:2
str:5
str:3
示例代码:
/**
* sorted default
*/
aslist.stream().sorted().forEach(s -> System.out.println("sort1:" + s));
/**
* sorted comparator
*/
aslist.stream().sorted(((o1, o2) -> Integer.parseInt(o2) - Integer.parseInt(o1))).forEach(s -> System.out.println("sort2:" + s));
//Stream sorted method
Stream<T> sorted();
//Stream sorted method
Stream<T> sorted(Comparator<? super T> comparator);
运行结果:
sort1:1
sort1:2
sort1:2
sort1:3
sort1:3
sort1:5
sort2:5
sort2:3
sort2:3
sort2:2
sort2:2
sort2:1
List<String> aslist = Arrays.asList("1", "6", "5", "3", "3", "2");
/**
* limit
*/
aslist.stream().limit(2).forEach(s -> System.out.println("limit:" + s));
//Stream limit method
Stream<T> limit(long maxSize);
运行结果:
limit:1
limit:6
末端方法就是对流的最终操作。当对某个Stream执行末端方法后,该流将会被“消耗”且不可再用。包括sum(),count(),average()等聚合函数。
示例代码:
List<String> aslist = Arrays.asList("1", "6", "5", "3", "3", "2");
/**
* forEach
*/
aslist.stream().forEach(s -> System.out.println("forEach:" + s));
//Stream forEach method
void forEach(Consumer<? super T> action);
运行结果:
forEach:1
forEach:6
forEach:5
forEach:3
forEach:3
forEach:2
List<String> aslist = Arrays.asList("1", "6", "5", "3", "3", "2");
/**
* toArray
*/
Object[] objArray = aslist.stream().toArray();
Arrays.asList(objArray).forEach(s -> System.out.println("toArray:" + s));
//Stream toArray method
Object[] toArray();
运行结果:
toArray:1
toArray:6
toArray:5
toArray:3
toArray:3
toArray:2
示例代码:
List<String> aslist = Arrays.asList("1", "6", "5", "3", "3", "2");
/**
* max && min && count
*/
System.out.println("max:" + aslist.stream().max(((o1, o2) -> Integer.parseInt(o1) - Integer.parseInt(o2))).get());
System.out.println("mix:" + aslist.stream().min(((o1, o2) -> Integer.parseInt(o1) - Integer.parseInt(o2))).get());
System.out.println("count:" + aslist.stream().count());
//Stream max method
Optional<T> max(Comparator<? super T> comparator);
//Stream min method
Optional<T> min(Comparator<? super T> comparator);
//Stream count method
long count();
运行结果:
max:6
mix:1
count:6
List<String> aslist = Arrays.asList("1", "6", "5", "3", "3", "2");
/**
* anyMatch
*/
System.out.println("anyMatch:" + aslist.stream().anyMatch(s -> s.equals("5")));
/**
* allMatch
*/
System.out.println("allMatch:" + aslist.stream().allMatch(s -> s.equals("1")));
/**
* noneMatch
*/
System.out.println("noneMatch:" + aslist.stream().noneMatch(s -> s.equals("a")));
//Stream anyMatch method
boolean anyMatch(Predicate<? super T> predicate);
//Stream allMatchmethod
boolean allMatch(Predicate<? super T> predicate);
//Stream noneMatchmethod
boolean noneMatch(Predicate<? super T> predicate);
运行结果:
anyMatch:true
allMatch:false
noneMatch:true
List<String> aslist = Arrays.asList("1", "6", "5", "3", "3", "2");
/**
* findFirst && findAny
*/
System.out.println("findFirst:" + aslist.stream().findFirst().get());
System.out.println("findAny:" + aslist.stream().findAny().get());
//Stream findFirst method
Optional<T> findFirst();
//Stream findAny method
Optional<T> findAny();
Optional<T> reduce(BinaryOperator<T> accumulator)
//比如,我们找出数组中长度最大的一个数:
public void test() {
String address = "中山北路南京大学仙林校区";
List<String> aList = new ArrayList<>();
aList.add("南京");
aList.add("大学");
aList.add("仙林校区");
aList.add("仙林大学城");
aList.add("中山北路");
Optional<String> a =aList.stream()
.reduce((s1, s2) -> s1.length()>=s2.length() ? s1 : s2);
System.out.println(a.get());//仙林大学城
}
T reduce(T identity, BinaryOperator<T> accumulator)
public void test() {
String address = "中山北路南京大学仙林校区";
List<String> aList = new ArrayList<>();
aList.add("南京");
aList.add("大学");
aList.add("仙林校区");
aList.add("仙林大学城");
aList.add("中山北路");
String t="value:";
String a =aList.stream()
.reduce(t, new BinaryOperator<String>() {
@Override
public String apply(String s, String s2) {
return s.concat(s2);
}
});
System.out.println(a);//value:南京大学仙林校区仙林大学城中山北路
}
<R> R collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner);
还好,考虑到我们日常使用,java8提供了一个收集器(Collectors),它是专门为collect方法量身打造的接口:我们常常使用collect将流转换成List,Map或Set:
Stream<String> stream = Stream.of("I", "love", "you", "too");
List<String> list = stream.collect(Collectors.toList());
String address = "中山北路南京大学仙林校区";
List<String> aList = new ArrayList<>();
aList.add("南京");
aList.add("大学");
aList.add("仙林校区");
aList.add("仙林大学城");
aList.add("中山北路");
String t="value:";
Map<String, Integer> maps = aList.stream().collect(Collectors.toMap(Function.identity(), String::length));
System.out.println(maps);//{中山北路=4, 大学=2, 仙林大学城=5, 仙林校区=4, 南京=2}
String address = "中山北路南京大学仙林校区";
List<String> aList = new ArrayList<>();
aList.add("南京");
aList.add("大学");
aList.add("仙林校区");
aList.add("仙林大学城");
aList.add("中山北路");
String t="value:";
Map<Integer, List<String>> maps = aList.stream().collect(Collectors.groupingBy(String::length));
System.out.println(maps);//{2=[南京, 大学], 4=[仙林校区, 中山北路], 5=[仙林大学城]}
IntStream是特殊的Stream,但有一些操作符是IntStream独有的。
以下代码需要单独运行:java.lang.IllegalStateException: stream has already been operated upon or closed
int[] ints = new int[]{1, 3, 5, 7, 9, 2, 4, 6, 8};
IntStream intStream = IntStream.of(ints);
System.out.println("max:" + intStream.max().getAsInt());
System.out.println("min:" + intStream.min().getAsInt());
System.out.println("avg:" + intStream.average().getAsDouble());
System.out.println("sum:" + intStream.sum());
System.out.println("count:" + intStream.count());
参考连接:https://www.cnblogs.com/xjx199403/p/10697430.html
标签:value double extends 耗资源 流转 java 常用 combiner 处理过程
原文地址:https://www.cnblogs.com/everyingo/p/12942714.html