java lambda之ListHelper
小的第一次写博客,各位别见笑。
刚从C#转java不久,写代码各种不自在,各种不舒服。很多以前在C#很简单的写法,在java里却不行。特别是对于集合的处理,比如我要取集合里所有对象的Id,我还需要写一个循环,我无力吐槽,谁叫C#市场不行呢。 于是乎。。。。
后面知道java里其实也有lambda表达式这东西,所以自己便研究了一下,封装了一些以前在C#常用的一些处理集合的方法,看名字就知道几乎照着以前C# Linq里的取的。
废话少说上示例
package lambda; import java.lang.reflect.Method; import java.nio.file.ClosedWatchServiceException; import java.util.*; public class test { public static void main(String[] args) { List<Student> students = new ArrayList<>(); students.add(new Student(1, "damao1", 13)); students.add(new Student(2, "damao2", 12)); students.add(new Student(3, "damao3", 13)); students.add(new Student(4, "damao2", 12)); students.add(new Student(5, "damao3", 13)); //需求:得到所有id的集合 //以前你得这样 List<Integer> ids=new ArrayList<>(); for (Student student: students){ ids.add(student.getId()); } //现在只需要这样 ids=ListHelper.select(students,n->n.getId()); //需求:查询一个id为1的Student //以前你得这样 Student stu=null; for (Student student: students){ if(student.getId().equals(1)){ stu=student;
break; } } //现在只需要这样 stu=ListHelper.firstOrDefault(students,n->n.getId().equals(1)); //需求:查询一个Id>2的Student 集合 //以前你得这样 List<Student> stus=new ArrayList<>(); for (Student student: students){ if(student.getId()>1){ stus.add(student); } } //现在只需要这样 stus=ListHelper.where(students,n->n.getId()>1); //需求:根据年龄分下组 //以前你得这样 //..........................我懒得写了,反正很麻烦 //现在只需要这样 HashMap<Integer, List<Student>> groups = ListHelper.group(students, n -> n.getAge());
//需求:判断集合中有没有重复的姓名
boolean repeated = ListHelper.isRepeated(students, n -> n.getName());
//根据年龄排序
students = ListHelper.orderBy(students, n -> n.getAge());
//求年龄最大的学生
Student max = ListHelper.max(students, n -> n.getAge());
//求年龄最小的学生
Student min = ListHelper.min(students, n -> n.getAge());
//还有等等,就不一一列举了
} }
下面是ListHelper的实现,你值得拥有
import java.util.*; import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; public class ListHelper { //example ListHelper.firstOrDefault(students, n -> n.getAge()==12) public static <T> T firstOrDefault(List<T> list, Predicate<? super T> predicate) { if (list != null) { Optional<T> first = list.stream().filter(predicate).findFirst(); if (first.isPresent()) { return first.get(); } } return null; } //example ListHelper.where(students, n -> n.getAge() >12) public static <T> List<T> where(List<T> list, Predicate<? super T> predicate) { if (list != null) { return list.stream().filter(predicate).collect(Collectors.toList()); } return null; } //example ListHelper.select(students, n -> n.getAge()) public static <T, E> List<E> select(List<T> list, Function<? super T, E> express) { List<E> eList = new ArrayList<>(); for (T t : list) { E e = express.apply(t); eList.add(e); } return eList; } //是否重复 // example ListHelper.isRepeated(students,n->n.getName() public static <T, E> boolean isRepeated(List<T> list, Function<? super T, E> express) { for (int i = 0; i < list.size()-1; i++) { E e = express.apply(list.get(i)); for (int j = i + 1; j <list.size(); j++) { E e2 = express.apply(list.get(j)); if (e.equals(e2)) { return true; } } } return false; } //分组 public static <T, E> HashMap<E,List<T>> group(List<T> list, Function<? super T, E> express) { HashMap<E,List<T>> groups=new HashMap<>(); for (T t:list){ E e = express.apply(t); if(groups.keySet().contains(e)){ groups.get(e).add(t); }else { List<T> tempList=new ArrayList<>(); tempList.add(t); groups.put(e,tempList); } } return groups; } // example ListHelper.orderBy(students,n->n.getAge()) public static <T, U extends Comparable<? super U>> List<T> orderBy(List<T> list, Function<? super T, ? extends U> keyExtractor) { list.sort(Comparator.comparing(keyExtractor)); return list; } // example ListHelper.orderBy(students,n->n.getAge()) public static <T, U extends Comparable<? super U>> List<T> orderByDesc(List<T> list, Function<? super T, ? extends U> keyExtractor) { list.sort(Comparator.comparing(keyExtractor).reversed()); return list; } // example ListHelper.max(students,n->n.getAge()) public static <T, U extends Comparable<? super U>> T max(List<T> list, Function<? super T, ? extends U> keyExtractor) { list.sort(Comparator.comparing(keyExtractor)); if (list.size() > 0) { return list.get(list.size() - 1); } return null; } // example ListHelper.min(student,n->n.getAge()) public static <T, U extends Comparable<? super U>> T min(List<T> list, Function<? super T, ? extends U> keyExtractor) { list.sort(Comparator.comparing(keyExtractor)); if (list.size() > 0) { return list.get(0); } return null; } }
为了方便大家测试,我把Student类也贴出来
public class Student { Integer id; String name; Integer age; public Student() { } public Student(Integer id, String name, Integer age) { this.id = id; this.name = name; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } 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; } }