List整理

List

  1. 一个list转为二维数组,五个一组

    List<List<String>> lists = Lists.partition(list,5);

  2. 删除list中的元素 删除下标以及定位到遍历的位置

    for(int i = 0, len = list.size(); i < len; i++){
    if(list.get(i) == 1){
    list.remove(i);
    len--;
    i--;
    }
    }

参考:删除list元素

  1. List转数组

    // 要转换的list集合
    List<String> testList = new ArrayList<String>(){{add("aa");add("bb");add("cc")}}

    // 使用toArray(T[] a)方法
    String[] array2 = testList.toArray(new Strinng[testList.size()]);

java List和数组相互转换方法

  1. List对象类型转换

    List<Response1> collect = response2List.stream().map(item -> ConvertHelper.convert(item, Response1.class)).collect(Collectors.toList());

  2. List和List相互转换

    // string转long
    List<Long> longList = stringList.stream.map(Long::valueOf).collect(Collectors.toList());
    // long转string
    List<String> stringList = longList.stream.map(String::valueOf).collect(Collectors.toList());

  3. list取其中一个字段组成List

    List<Long> numberList = list.stream().map(Response::getOrgId).collect(Collectors.toList());

  4. List取差集

    allDataList.removeAll(removeDataList);

  5. 多个List取交集

    List<Integer> A = new ArrayList<>(Arrays.asList(1,2,5,8));
    List<Integer> B = new ArrayList<>(Arrays.asList(5,7,8,9));
    List<Integer> C = new ArrayList<>(Arrays.asList(5,8,9));
    List<List<Integer>> D = new ArrayList<>();
    D.add(A);
    D.add(B);
    D.add(C);
    List<Integer> E = D.get(0);
    D.forEach(integers ->{
    E.retailAll(integers);
    });

    // E就是最后的交集结果

  6. 在list后追加list / 在list中添加list

    list1.addAll(list2);

  7. list的两种分组方式
    假设有个student类,有id、name、score属性,list集合中存放所有学生信息,现在要根据学生姓名进行分组
    10.1 java8之前list分组

    public Map<String, List<Student>> groupList(List<Student> students) {
    Map<String, List<Student>> map = new Hash<>();
    for (Student student : students) {
    List<Student> tmpList = map.get(student.getName());
    if (tmpList == null) {
    tmpList = new ArrayList<>();
    tmpList.add(student);
    map.put(student.getName(), tmpList);
    } else {
    tmpList.add(student);
    }
    }
    return map;
    }

10.2 java8的list分组

List转map

复制代码
public Map<String, List<Student>> groupList(List<Student> students) {
	Map<String, List<Student>> map = students.stream().collect(Collectors.groupingBy(Student::getName));
	return map;
}
  1. 查看list中是否有重复值
    11.1 将list转为set,通过两个集合的size大小是否相等来判断有无重复元素

    public static void main(String[] args) {
    List<String> stringList=new ArrayList<>(Arrays.asList("a","a","b","c"));
    Set<String> stringSet=new HashSet<>(stringList);
    if (stringList.size() == stringSet.size()) {
    System.out.println("没有重复元素");
    } else {
    System.out.println("有重复元素");
    }
    }

11.2 使用jdk8的stream判断

复制代码
public static void main(String[] args) {
    List<String> stringList=new ArrayList<>(Arrays.asList("a","a","b","c"));
    long count = stringList.stream().distinct().count();
    if (stringList.size() == count) {
    System.out.println("没有重复元素");
    } else {
    System.out.println("有重复元素");
    }
}

11.3 判断一个对象集合中 的某个字段是否有重复

判断Person集合中的name属性有没有重复

复制代码
public static void main(String[] args) {
    List<Person> personList = new ArrayList<Person>(){{
        add(new Person("张三"));
        add(new Person("李四"));
        add(new Person("张三"));
    }};
    List<String> stringList = personList.stream().map(Person::getName)
        .collect(Collectors.toList());
    long count = stringList.stream().distinct().count();
    if (stringList.size() == count) {
        System.out.println("没有重复元素");
    } else {
        System.out.println("有重复元素");
    }
}
  1. 判断list集合中是否包含某个元素 list.contains()

    List<String> list = new ArrayList<>();
    list.add("1");
    list.add("2");
    list.add("3");
    list.add("4");
    list.add("5");
    boolean isHave = list.contains("2");

  2. list去重

    // 去重并转类型
    List<String> collect = longList.stream().distinct().map(String::valueOf).collect(Collectors.toList());

  3. 从List中取两个字段作为map的key和value

    Map<String, Long> collect = studentList.stream().collect(Collectors.toMap(Student::getName, Student::getId));

  4. 从list中取一个字段进行分组转map 并过滤

    // 并过滤名字为空的数据
    HashMap<String,Student> studentMap = studentList.filter(item -> StringUtils.isNotBlank(item.getName())).collect(Collectors.groupingBy(Student::getName));

  5. list倒排序

    Collections.reverse(list);

相关推荐
Lxinccode8 小时前
python(55) : python程序设置为Windows快捷方式
windows·python固定到开始·快速调用开始
纳于大麓10 小时前
Kotlin基础语法
linux·windows·kotlin
呉師傅14 小时前
关于联想ThinkCentre M950t-N000 M大师电脑恢复预装系统镜像遇到的一点问题
运维·网络·windows·电脑
网安INF16 小时前
Python核心数据结构与函数编程
数据结构·windows·python·网络安全
知识分享小能手20 小时前
uni-app 入门学习教程,从入门到精通,uni-app 基础知识详解 (2)
前端·javascript·windows·学习·微信小程序·小程序·uni-app
waving-black21 小时前
windows系统下安装测试kafka
windows·分布式·kafka
傻啦嘿哟1 天前
Python 高效清理 Excel 空白行列:从原理到实战
windows
晋人在秦 老K1 天前
鼠标连点效率低?MouseClickTool 2.0三步配置,自动化操作提速80% 重复点击太耗时?Windows鼠标连点器实操教程,开发测试效率翻倍
windows·自动化·计算机外设·鼠标连点器·gui模拟点击·自动化点击解决方案
shykevin1 天前
uni-app x商城,商品列表组件封装以及使用
windows·uni-app
CHH32131 天前
在 Mac/linux 的 VSCode 中使用Remote-SSH远程连接 Windows
linux·windows·vscode·macos