java list 去重 相同的相加_java8 list求最大值、最小值、平均值、求和、中位数、属性排序、去重...

import org.junit.Test;

import java.text.SimpleDateFormat;

import java.util.*;

import java.util.stream.Collectors;

import static java.util.Comparator.comparingLong;

import static java.util.stream.Collectors.*;

/**

* @Author:

* @Date: 2018/12/12 13:08

* @Description:

*/

public class test {

public static void main(String[] args) {

List list = new ArrayList<>();

list.add(new User(21L, "张三"));

list.add(new User(25L, "李四"));

list.add(new User(22L, "王五"));

list.add(new User(19L, "赵柳"));

list.add(new User(32L, "王5"));

list.add(new User(29L, "王6"));

list.add(new User(21L, "王7"));

// 对象根据年龄属性升序排序

List newList = list.stream().sorted(Comparator.comparing(User::getAge)).collect(toList());

// list遍历

newList.forEach(System.out::println);

// 平均数

double asDouble = list.stream().mapToLong(User::getAge).average().getAsDouble();

System.out.println("average:" + asDouble);

double avg = list.stream().collect(Collectors.averagingLong(User::getAge));

System.out.println("average:" + avg);

// 最大值

long asLong = list.stream().mapToLong(User::getAge).max().getAsLong();

System.out.println("max:" + asLong);

// 最小值

long asLong1 = list.stream().mapToLong(User::getAge).min().getAsLong();

System.out.println("min:" + asLong1);

// 求和

long sum1 = list.stream().mapToLong(User::getAge).sum();

System.out.println("sum:" + sum1);

// 提取对象属性生成list

List ids = list.stream().map(User::getAge).collect(toList());

System.out.println(ids);

// list升序排序

Collections.sort(ids);

System.out.println(ids);

// 生成中位数

Long j;

if (ids.size() % 2 == 0) {

j = (ids.get(ids.size() / 2 - 1) + ids.get(ids.size() / 2)) / 2;

System.out.println("中位数为" + j);

} else {

j = ids.get(ids.size() / 2);

System.out.println("中位数为" + j);

}

// list倒序排序

ids.sort(Comparator.reverseOrder());

System.out.println(ids);

// 去重

List users = list.stream().collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparingLong(User::getAge))), ArrayList::new));

System.out.println("去重:"+users);

/**

* List -> Map

* 需要注意的是:toMap 如果集合对象有重复的key,会报错Duplicate key ....

* apple1,apple12的id都为1。可以用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2

*/

Map userMap = list.stream().collect(Collectors.toMap(User::getAge, a -> a, (k1, k2) -> k1));

System.out.println(userMap);

//过滤出符合条件的数据

List filterList = list.stream().filter(a -> a.getName().equals("李四")).collect(toList());

System.out.println("filterList:" + filterList);

List list2 = Arrays.asList(1, 2, 3, 4, 5);

int sum = list2.stream().reduce(0, (acc, value) -> acc + value);

System.out.println(sum);

List result = list2.stream().filter((value) -> value > 2).collect(toList());

result.forEach(System.out::println);

List result2 = list2.stream().map(value -> String.format("String:%s", value)).collect(toList());

result2.forEach(System.out::println);

// 用于收集统计数据的状态对象,例如count,min,max,sum和平均。

IntSummaryStatistics stats = list2.stream().mapToInt((x) -> x).summaryStatistics();

System.out.println("Max : " + stats.getMax());

System.out.println("Min: " + stats.getMin());

System.out.println("Sun: " + stats.getSum());

System.out.println("Average : " + stats.getAverage());

System.out.println("Count : " + stats.getCount());

System.out.println("toString : " + stats.toString());

}

}

class User {

private Long age;

private String name;

public User(Long i, String s) {

this.age = i;

this.name = s;

}

public Long getAge() {

return age;

}

public void setAge(Long age) {

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

@Override

public String toString() {

return "User [age=" + age + ", name=" + name + "]";

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值