import java.lang.reflect.Method;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* lambda之ListHelper
* Created by zhaojiy on 2018/12/15.
*/
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;
}
public static <K, V> Map<K, V> listToMap(List<V> list, String keyMethodName, Class<V> c) {
Map<K, V> map = new HashMap<K, V>();
if (list != null) {
try {
Method methodGetKey = c.getMethod(keyMethodName);
for (int i = 0; i < list.size(); i++) {
V value = list.get(i);
@SuppressWarnings(\"unchecked\")
K key = (K) methodGetKey.invoke(list.get(i));
map.put(key, value);
}
} catch (Exception e) {
throw new IllegalArgumentException(\"field can\'t match the key!\");
}
}
return map;
}
// 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;
}
}
继续阅读与本文标签相同的文章
下一篇 :
《算法技术手册》一1.1 理解问题
-
学宏程序编程,这些知识必不可少!
2026-05-14栏目: 教程
-
华为准备卖出“落后”的5G,多家美企极力竞争!任正非格局太大!
2026-05-14栏目: 教程
-
百度:飞桨深度学习平台已累计服务150多万开发者
2026-05-14栏目: 教程
-
滴滴公布安全功能数据:近2亿用户添加紧急联系人
2026-05-14栏目: 教程
-
滴滴自动驾驶或将于年底落地上海
2026-05-14栏目: 教程
