/**
* 过滤
*/
public static void forListTest(){
List<Pension<String,Integer>> list = new ArrayList<>();
Pension pension = new Pension("小红", 25);
Pension pension2 = new Pension("小红dfssdf", 20);
Pension pension3 = new Pension("小红dfs", 21);
Pension pension4 = new Pension<>("小sdf红", 20);
Pension pension5 = new Pension("小sdf红", 25);
Pension pension6 = new Pension("小红", 20);
list.add(pension);
list.add(pension2);
list.add(pension5);
list.add(pension3);
list.add(pension6);
list.add(pension3);
list.add(pension6);
list.add(pension4);
list.add(pension);
list.add(pension2);
list.add(pension5);
list.add(pension3);
list.add(pension6);
list.add(pension6);
list = list.stream().filter((Pension<String,Integer> p)->p.getAge()>20&&!p.getName().equals("小红")).collect(Collectors.toList());
List<Pension<String,Integer>> resultList = new ArrayList<>();
for (Pension<String,Integer> p:list){
if (p.getAge()>20&&!p.getName().equals("小红")){
resultList.add(p);
}
}
resultList.stream().forEach((Pension<String,Integer> p)->{
System.out.println(p.getName());
});
System.out.println(resultList.size());
}
/**
* 截取
*/
public static void subListTest(){
List list = new ArrayList<>();
Pension pension = new Pension("小红", 25);
Pension pension2 = new Pension("小红dfssdf", 20);
Pension pension3 = new Pension("小红dfs", 21);
Pension pension4 = new Pension<>("小sdf红", 20);
Pension pension5 = new Pension("小sdf红", 25);
Pension pension6 = new Pension("小红", 20);
list.add(pension);
list.add(pension2);
list.add(pension5);
list.add(pension3);
list.add(pension6);
list.add(pension3);
list.add(pension6);
list.add(pension4);
list.add(pension5);
list.add(pension);
list.add(pension2);
list.add(pension5);
list.add(pension3);
list.add(pension6);
list.add(pension6);
int num = 5;
int size = list.size();
int count = (size/num)+1;
for (int i=0;i<count;i++){
int from = i*num;
int to = (i+1)*num;
to = to>size?size:to;
if (from==to) break;
List list1 = list.subList(from,to);
System.out.println(list1.size());
}
}
/**
* 去重
*/
public static void testquchong() {
List<Pension> list = new ArrayList<>();
Pension pension = new Pension("小红", 25);
Pension pension2 = new Pension("小红dfssdf", 20);
Pension pension3 = new Pension("小红dfs", 21);
Pension<String, Integer> pension4 = new Pension<>("小sdf红", 20);
Pension pension5 = new Pension("小sdf红", 25);
Pension pension6 = new Pension("小红", 20);
list.add(pension);
list.add(pension2);
list.add(pension5);
list.add(pension3);
list.add(pension6);
list.add(pension3);
list.add(pension6);
list.add(pension4);
list.add(pension5);
//把自己添加进去
list.addAll(list);
// 去重
Set<Pension> set = new TreeSet<>(Comparator.comparing(Pension<String, Integer>::getAge));
set.addAll(list);
list = new ArrayList<>(set);
System.out.println(list.size());
Map<Object, Object> map = new HashMap();
List<Pension> list2 = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if (map.get(list.get(i).getAge()) == null) {
list2.add(list.get(i));
map.put(list.get(i).getAge(), "true");
}
}
System.out.println(list2.size());
}
class Pension<E, T> {
private E name;
private T age;
public Pension() {
}
public Pension(E name, T age) {
this.name = name;
this.age = age;
}
public E getName() {
return name;
}
public void setName(E name) {
this.name = name;
}
public T getAge() {
return age;
}
public void setAge(T age) {
this.age = age;
}
}