赞
踩
- List<List<String>> lists = Lists.partition(list,5);
- for(int i = 0, len = list.size(); i < len; i++){
- if(list.get(i) == 1){
- list.remove(i);
- len--;
- i--;
- }
- }
- // 要转换的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和数组相互转换方法
4. List对象类型转换
List<Response1> collect = response2List.stream().map(item -> ConvertHelper.convert(item, Response1.class)).collect(Collectors.toList());
- // 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());
List<Long> numberList = list.stream().map(Response::getOrgId).collect(Collectors.toList());
allDataList.removeAll(removeDataList);
- 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就是最后的交集结果
list1.addAll(list2);
- 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;
- }
- 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("有重复元素");
- }
- }
- 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");
- // 去重并转类型
- List<String> collect = longList.stream().distinct().map(String::valueOf).collect(Collectors.toList());
Map<String, Long> collect = studentList.stream().collect(Collectors.toMap(Student::getName, Student::getId));
- // 并过滤名字为空的数据
- HashMap<String,Student> studentMap = studentList.filter(item -> StringUtils.isNotBlank(item.getName())).collect(Collectors.groupingBy(Student::getName));
Collections.reverse(list);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。