java8新特性Stream流分组排序过滤多条件去重
private static List<User> list = new ArrayList<User>();
public static void main(String[] args) {
list = Arrays.asList(
new User(1, "a", 10),
new User(4, "d", 19),
new User(5, "e", 13),
new User(2, "b", 14),
new User(3, "a", 10),
new User(6, "f", 16)
);
long start = System.currentTimeMillis();
order();
println(String.format("耗时[%s]毫秒", (System.currentTimeMillis() - start)));
}
/** * 多条件去重
* @param list
*/
public static void order() {
list.stream().llectingAndThen(
Comparatorparing(o -> o.getAge() + ";" + o.getName()))), ArrayList::new)).forEach(u -> println(u));
}
public static void group() {
Map<Integer, List<User>> collect = list.stream().upingBy(User::getAge));
System.out.println(collect);
java stream}
/** * filter过滤
* @param list
*/
public static void filterAge() {
list.stream().filter(u -> u.getAge() == 10).forEach(u -> println(u));
}
/
** * sorted排序
*/
public static void stord() {
list.stream().sorted(Comparatorparing(u-> u.getAge())).forEach(u -> println(u));
}
/** * limit⽅法限制最多返回多少元素
*/
public static void limit() {
list.stream().limit(2).forEach(u -> println(u));
}
/** * 不要前多n个元素,n⼤于满⾜条件的元素个数就返回空的流
*/
public static void skip() {
list.stream().skip(2).forEach(u -> println(u));
}
// 最⼤值最⼩值
public static void statistics() {
Optional<User> min = list.stream().min(Comparatorparing(User::getUserId));
println(min);
Optional<User> max = list.stream().max(Comparatorparing(User::getUserId));
println(max);
}
/
/ 统计
public static void summarizingInt(){
IntSummaryStatistics statistics = list.stream().collect(Collectors.summarizingInt(User::getAge));
double average = Average();
long count = Count();
int max = Max();
int min = Min();
long sum = Sum();
println(average);
println(count);
println(min);
println(sum);
println(max);
}
/** * 转set
*/
public static void toSet() {
Set<User> collect = list.stream().Set());
Iterator<User> iterator = collect.iterator();
while(iterator.hasNext()) {
System.out.().getUserId());
}
}
/** * 转map
*/
public static void toMap() {
Map<Integer, User> collect = list.stream().Map(User::getUserId, u -> u));
for (Integer in : collect.keySet()) {
User u = (in);//得到每个key多对⽤value的值
println(u);
}
}
/** *map
*/
public static void map() {
list.stream().map(User::getUserId).forEach(userId -> println(userId));
list.stream().mapToInt(User::getAge).forEach(userId -> println(userId));
list.stream().mapToDouble(User::getUserId).forEach(userId -> println(userId));
list.stream().mapToLong(User::getUserId).forEach(userId -> println(userId));
}
/** * 查与匹配
* allMatch⽅法与anyMatch差不多,表⽰所有的元素都满⾜才返回true。noneMatch⽅法表⽰没有元素满⾜ */
public static void anyMatch() {
boolean anyMatch = list.stream().anyMatch(u -> u.getAge() == 100);
boolean allMatch = list.stream().allMatch(u -> u.getUserId() == 10);
boolean noneMatch = list.stream().noneMatch(u -> u.getUserId() == 10);
println(anyMatch);
println(allMatch);
println(noneMatch);
}
/** * reduce操作
*/
public static void reduce() {
Optional<Integer> sum = list.stream().map(User::getAge).reduce(Integer::sum);
Optional<Integer> max = list.stream().map(User::getAge).reduce(Integer::max);
Optional<Integer> min = list.stream().map(User::getAge).reduce(Integer::min);
println(sum);
println(max);
println(min);
}
/** * 公共输出
* @param c
*/
public static void println(Object c) {
System.out.String());
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论