The object attribute List List packet according to the number of re-&&

The object attribute List List packet according to the number of re-&&

List based on object properties deduplication

Student a conventional class, class has a name attribute, need to be equipped with Student's list according to the name attribute weight:


Student

public class Student {
    private String name;

    public Student() { }

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}

There are now equipped with a Student's list:

List<Student> list = new ArrayList<>();
Collections.addAll(list,
        new Student("张三"),
        new Student("李四"),
        new Student("王五"),
        new Student("张三"),
        new Student("李四"),
        new Student("赵六"));

System.out.println("去重之前: ");
System.out.println("list = " + list);
/* 输出:
去重之前: 
list = [张三, 李四, 王五, 张三, 李四, 赵六]
*/

The first way:

ArrayList<Student> afterList = list.stream()
        .collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(s -> s.getName())))
                , ArrayList::new));

System.out.println("第一种去重之后: ");
System.out.println("afterList = " + afterList);
/* 输出:
第一种去重之后: 
afterList = [张三, 李四, 王五, 赵六]
*/

At first glance this code Leng Shimo understand this is a de-emphasis, the following written in this way is better understood:

The second way:

Set<Student> set = new TreeSet<>(Comparator.comparing(Student::getName));
set.addAll(list);
List<Student> afterList2 = new ArrayList<>(set);

System.out.println("第二种去重之后: ");
System.out.println("afterList2 = " + afterList2);
/* 输出:
第二种去重之后: 
afterList2 = [张三, 李四, 王五, 赵六]
*/

The principle is the use of a constructor TreeSet: public TreeSet(Comparator<? super E> comparator)(the JDK 1.8) deduplication processing is performed, according to weight and may come more attributes.

Here I specifically looked at HashSet, HashSet and found no similar construction method, that is based on the attribute must be used to re-TreeSet

List according to the number of packets

Sometimes need to list a list split into a plurality of batch process, this time can be used in this way:

List<String> list = new ArrayList<>();
Collections.addAll(list, "1", "2", "3", "4", "5", "6", "7", "8");
List<List<String>> resultList = new ArrayList<>();

//多少个一组
int groupNum = 3;

//当前游标
int current = 0;
while (current < list.size()) {
    resultList.add(new ArrayList<>(list.subList(current, Math.min((current + groupNum), list.size()))));
    current += groupNum;
}

System.out.println("resultList = " + resultList);
/* 输出:
resultList = [[1, 2, 3], [4, 5, 6], [7, 8]]
*/

This uses list.subList(fromIndex, toIndex)this method ( range not header trailer, thus toIndexcan be used directlylist.size() ), it is noted that the subList()method returns the source list view, instead of the ArrayList a new, add and delete operations for subList affect the source list Therefore subList required as new ArrayList<>(subList)parameter to add to resultList inside.


Similar pit there is a Arrays.asList()method using Arrays.asList()a List object can not add and delete elements obtained, and therefore are generally used as new ArrayList<>(Arrays.asList(array))parameters to use, which is commonly used array to a List .

Guess you like

Origin www.cnblogs.com/lixin-link/p/12466544.html