# 集合列表

返回:源码

# ArrayList

# sort排序

  • 第一种:实现Comparator接口的类的对象作为sort的入参
public class HumanComparetor implements Comparator<Human> {
    @Override
    public int compare(Human h1, Human h2) {
        if (h1.getAge() > h2.getAge()) {
            return 1;
        } else if (h1.getAge() == h2.getAge()) {
            return 0;
        } else {
            return -1;
        }
    }
}



public static void main(String[] args) {
    List<Human> humans = Human.getAInitHumanList();
    Collections.sort(humans, new HumanComparetor());
    System.out.println(humans);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  • 第二种:在方法的局部使用局部类,原理和第一种差不多
public static void main(String[] args) {
    List<Human> humans = Human.getAInitHumanList();
    //方法内-局部类
    class HumanComparetor implements Comparator<Human> {
        @Override
        public int compare(Human h1, Human h2) {
            return h1.getAge() - h2.getAge();
        }
    }
    Collections.sort(humans, new HumanComparetor());
    System.out.println(humans);
}
1
2
3
4
5
6
7
8
9
10
11
12
  • 第三种:基于第二种方法,局部类改为匿名类
public static void main(String[] args) {
    List<Human> humans = Human.getAInitHumanList();
    //匿名类
    Collections.sort(humans, new Comparator<Human>() {
        @Override
        public int compare(Human h1, Human h2) {
            return h1.getAge() - h2.getAge();
        }
    });
    System.out.println(humans);
}
1
2
3
4
5
6
7
8
9
10
11
  • 第四种:使用lamdba表达式->这种形式
public static void main(String[] args) {
    List<Human> humans = Human.getAInitHumanList();
    //lamdba 表达式 ->
    Collections.sort(humans, (Human h1, Human h2) -> h1.getAge() - h2.getAge());
    System.out.println(humans);
}
1
2
3
4
5
6
  • 第五种:借助Comparator和lamdba表达式多条件排序
public static void main(String[] args) {
    List<Human> humans = Human.getAInitHumanList();
    ////lamdba 表达式 ::
    Collections.sort(humans, Comparator.comparing(Human::getAge).thenComparing(Human::getName));
    System.out.println(humans);
}
1
2
3
4
5
6
  • 第六种:调用方式和第五种有区别,原理一样
public static void main(String[] args) {
    List<Human> humans = Human.getAInitHumanList();
    //直接用list.sort
    humans.sort(Comparator.comparing(Human::getAge).thenComparing(Human::getName));
    System.out.println(humans);
}
1
2
3
4
5
6

# 获取集合中重复元素列表

如果是对象的话,需要重写equals方法

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj instanceof PersonModel) {
        PersonModel person = (PersonModel) obj;
        if (StringUtils.equals(person.getSex(), this.sex)
                && StringUtils.equals(person.getLastName(), this.lastName)
                && person.getAge().equals(this.age)) {
            return true;
        }
    }
    return super.equals(obj);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Test
public void testMultiSet() {
    List<PersonModel> persons = Lists.newArrayList();
    persons.add(new PersonModel("male", 14, "ting"));
    persons.add(new PersonModel("male", 14, "ting"));
    persons.add(new PersonModel("male", 15, "ting"));
    persons.add(new PersonModel("male", 16, "ting"));
    persons.add(new PersonModel("male", 16, "ting"));
    persons.add(new PersonModel("male", 20, "ting"));
    persons.add(new PersonModel("male", 31, "ting"));
//        System.out.println(persons.count(new PersonModel("male",16,"ting")));
// persons.indexOf(obj) != persons.lastIndexOf(obj)此条件是获取非重复的列表
    List<PersonModel> personss = persons.stream()
            .filter(obj -> persons.indexOf(obj) != persons.lastIndexOf(obj))
            .collect(Collectors.toList());
    personss.forEach(System.out::println);
//        System.out.println(persons);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18