java8 的一些基础集合操作

15 篇文章 0 订阅
11 篇文章 0 订阅

java 8 的一些使用:

1:list操作
String[] array 转 List
Arrays.asList(array);
List.toArray();

2:list转map
--list里的对象分组  -根据对象的元素进行分组
#### List ---> Map<Integer,List<Object>> 
list.stream().collect(Collectors.groupingBy(obj::getId));

3:多次分组 与数据组合
List ---> Map<Integer,Map<String,Long>> 
list.stream().collect(Collectors.groupingBy(obj::getId, Collectors.toMap(obj::getName, obj::getScore)));

List ---> Map<Integer,Map<String,List<Object>>>
list.stream().collect(Collectors.groupingBy(obj::getId, Collectors.groupingBy(obj::getName)));

4:分组 抽取某个元素形成集合
List ---> Map<Integer,List<String>>
list.stream().collect(Collectors.groupingBy(obj::getId, Collectors.mapping(obj::getName,Collectors.toList())));

MList ---> Map<Integer,Set<String>> 
list.stream().collect(Collectors.groupingBy(obj::getId, Collectors.mapping(obj::getName,Collectors.toSet())));


5:过滤List不需要的元素   list -> list
Integer value = 3;
objList.stream().filter(obj -> obj.getId().equals(value)).collect(toList()); // 过滤筛选

6:对一个对象只收集一个元素的集合
List<Object> -> List<Integer>
objList.stream().map(Object::getId).distinct().collect(Collectors.toList()); // distinct() 去重

7:List<Object> -> List<Integer>  再过滤元素
objList.stream().map(Object::getId).distinct().filter(x -> x>10 ).collect(Collectors.toList()); // distinct() 去重

8:综合例子
Student{id:1,name:2,age:3}
List<Student> list = Arrays.asList(new Student(2, "xx", 3), new Student(1, "yy", 3));
List<Integer> stIdList =
                list.stream()
                .map(LoginStudentScore::getId)
                .distinct()
                .collect(Collectors.toList());


9:计算一个list对象中元素的平均分 总分 等
list.stream().mapToDouble(Student::getId).sum();
list.stream().mapToDouble(Student::getId).max();
list.stream().mapToDouble(Student::getId).min();
list.stream().mapToDouble(Student::getId).average();


10:其他的一些集合操作工具
Collections.shuffle(list);   // 当前list对象进行乱序处理

List<Integer> list = Arrays.asList(2, 3, 1);
Collections.sort(list); // 排序

List<Student> list = Arrays.asList(new Student(2, "xx", 3), new Student(1, "yy", 3),new Student(3, "xx", 4));
list.sort(new Comparator<Student>() {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.getId() - o2.getId();
    }
});

11:lambda 语法
list.sort((o1, o2) -> o1.getId() - o2.getId());     // 可以自己指定比较关系
list.sort(Comparator.comparingInt(Student::getId));   // 根据对象某一元素进行升序
list.sort(Comparator.comparingInt(Student::getId).reversed()); // 反序
Collections.reversed(list); // 对list索引倒序


12:最大最小的元素
Student s = Collections.max(list, Comparator.comparingInt(Student::getId));
Student s = Collections.min(list, Comparator.comparingInt(Student::getId));

13: 置换元素
Collections.swap(list,1,2); // 兑换索引为1和2位置的元素

14: 2分查找元素   - 需要对之前的数据进行排序后使用
List<Integer> list = Arrays.asList(2, 3, 1);
Collections.sort(list); // 排序
Collections.binarySearch(list,2);  // 返回索引位置  // 对于list里面是对象的需要实现Comparable

15: 2分查找 对象的例子
@Data
@AllArgsConstructor
public class Student implements Comparable<Student> {
    private String name;
    private int age;

    @Override
    public int compareTo(Student o) {
        return o.getAge() - this.age;
    }
}

List<Student> list = Arrays.asList(new Student("2", 2), new Student("4", 4), new Student("10", 10));
list.sort(Comparator.comparingInt(Student::getAge));
System.out.println( Collections.binarySearch(list,new Student("4", 4)));

16:查看两个相同类型的集合是否有相同的元素
Collections.copy(newList,list);  // 复制元素  newList需要大于等于list对象集合数 ---> List<Object> newList = Arrays.asList(new Object[3]);
Collections.disjoint(list, list2); // 查看2个元素是否有不同的数据
Collections.fill(list,newObject);//用newObject替换该集合所有元素
Collections.frequency(list, oldObject); // 返回集合中全部的相同元素  -- 对查找的对象需要重写equals方法

--------windows 命令清除线程
taskkill /f /pid 5932
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值