Java List 操作
Java List
- 对元素是基本数据类型的List排序 参考 对List集合中的元素进行排序 java对list进行排序
- 对 元素是复杂实体类的List排序
- List转为String
- String转为List
- List 和 Set 互转 参考 Set与List之间转化
- Array和List互转 参考Java中List转换为数组,数组转List,ArrayList深度分析(四):ArrayList和数组间的相互转换
- List<Long> 判断包含
定义实体类Student,实现Comparable接口,实现接口的compareTo()方法
public class Student implements Comparable<Student> { private String name; private Integer age; public Student(String name, Integer age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public int compareTo(Student o) { int i = this.getName().compareTo(o.getName()); if (i == 0) { i = this.getAge().compareTo(o.getAge()); } return i; // sort desc 降序排列 // if (i == -1) // return 1; // if(i == 1) // return -1; // else { // return 0; // } } }
具体实现方法
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; public class ListSortTest { public static void main(String[] args) { // 对元素是基本数据类型的List排序 listSortSimple(); // 对 元素是复杂实体类的List排序 listSortEntity(); // List转为String list2String(); // String转为List string2List(); // List 和 Set 互转 listAndSetConvert(); // Array转List array2List(); } // 对元素是基本数据类型的List排序 public static void listSortSimple() { List<String> nameList = new ArrayList<>(); nameList.add("serina"); nameList.add("blair"); nameList.add("calorin"); for (String name : nameList) { System.out.println("before name = " + name); } // 使用Java中提供的对集合进行操作的工具类Collections,其中的sort方法 Collections.sort(nameList); for (String name : nameList) { System.out.println("after name = " + name); } } // 对 元素是复杂实体类的List排序 public static void listSortEntity() { List<Student> students = new ArrayList<>(); students.add(new Student("serina", 13)); students.add(new Student("serina", 10)); students.add(new Student("blair", 15)); students.add(new Student("calorin", 16)); students.add(new Student("calorin", 12)); for (Student student : students) { System.out.println("before student name = " + student.getName() + " age = " + student.getAge()); } Collections.sort(students); // 排序 for (Student student : students) { System.out.println("after student name = " + student.getName() + " age = " + student.getAge()); } } public static void list2String() { // data List<String> nameList = new ArrayList<String>(); nameList.add("Joyce"); nameList.add("Eco"); nameList.add("Chole"); String name = ""; // convert if (!CollectionUtils.isEmpty(nameList)) { name = String.join(",", nameList); } // result System.out.println("name = " + name); } public static void string2List() { // data String name = "Joyce,Eco,Chole"; List<String> nameList = new ArrayList<String>(); // convert if (StringUtils.hasText(name)) { nameList.addAll(Arrays.asList(name.split(","))); } // result System.out.println("nameList " + nameList); } public static void listAndSetConvert() { List<String> nameList = new ArrayList<String>(); Set<String> nameSet = new HashSet<>(); nameList.add("blair"); nameList.add("serina"); nameList.add("dan"); nameList.add("dan"); System.out.println("------------------------------nameList==" + nameList.toString()); // convert list to set之后会丢失数据,并且会丢失原来的顺序 // convert list to set nameSet = new HashSet<>(nameList); System.out.println("------------------------------nameSet==" + nameSet.toString()); // convert set to list nameList.clear(); nameList.addAll(nameSet); System.out.println("------------------------------nameList==" + nameList.toString()); } public static void array2List() { String[] names = { "anna", "holy", "selly" }; // 方法2: ok List<String> nameList2 = new ArrayList<>(); nameList2.addAll(Arrays.asList(names)); nameList2.clear(); nameList2.add("serina2"); for (String name : nameList2) { System.out.println("name===" + name); } // 方法1: throw errors: java.lang.UnsupportedOperationException // 直接 Arrays.asList() new 的list不能执行 clear(),add()等操作 List<String> nameList = Arrays.asList(names); nameList.clear(); nameList.add("serina"); for (String name : nameList) { System.out.println("name===" + name); } } public void list2Array() { ArrayList<String> list = new ArrayList<String>(); String[] strings = new String[list.size()]; list.toArray(strings); } }
判断包含
public static void main(String[] args) { List<Long> ids= new ArrayList<Long>(); ids.add((long) 3); ids.add((long) 6); ids.add((long) 9); Integer value = 3; Long vaule2 = (long) 6; if(ids.contains(value.longValue())) { System.out.println("contains 3"); } if(ids.contains(vaule2.longValue())) { System.out.println("contains 6"); } }