public class Employee {
public String name;
public int age;
public char sex;
public String time;
public int salary;
}
List<Employee> data = Arrays.asList(e1,e2,e3......)
public static Map<Character, List<Employee>> groupByFirstChar(
List<Employee> data){
Map<Character, List<Employee>> result = new HashMap<>();
for(Employee e : data){
Character c = e.name.charAt(0);
List<Employee> l = result.get(c);
if(l == null){
l = new ArrayList<>();
result.put(c, l);
}
l.add(e);
}
return result;
}
public static Map<String, List<Employee>> groupBySalary(List<Employee> data) {
Map<String, List<Employee>> result = new HashMap<>();
for (Employee e : data) {
String key = separate(e.salary);
List<Employee> l = result.get(key);
if (l == null) {
l = new ArrayList<>();
result.put(key, l);
}
l.add(e);
}
return result;<br>}
private static String separate(int salary) {
if (salary <= 5000) {
return "5000以下";
}
if (salary <= 10000) {
return "5000~10000";
}
if (salary <= 20000) {
return "10000~20000";
}
return "20000以上"
}
Employee e -> e.name.charAt(0)
Employee e -> separate(e.salary):String
Employee e -> getYear(e.time):String
Employee e -> getFirstChar(e.name):Character
Employee e -> getFirstChar(e) : Character Employee e -> separate(e) : String Employee e -> getYear(e) : String
public static <K> Map<K, List<Employee>> groupByKey(List<Employee> data, GetKey<K> getKey){
Map<K, List<Employee>> result = new HashMap<>();
for(Employee e : data) {
K key = getKey.getKey(e);
List<Employee> l = result.get(key);
if (l == null) {
l = new ArrayList<>();
result.put(key, l);
}
l.add(e);
}
return result;
}
interface GetKey<K>{
K getKey(Employee e);
}
Map<Character, List<Employee>> result = groupByKey(data, new GetKey<Character>() {
@Override
public Character getKey(Employee e) {
e.name.charAt(0);
}
});
Map<String, List<Employee>> result = groupByKey(list, new GetKey<String>() {
@Override
public String getKey(Employee e) {
separate(e.salary);
}
});
Map<Character, List<Employee>> resultByFirstChar = groupByKey(list, e -> e.name.charAt(0)); Map<String, List<Employee>> resultBySalary = groupByKey(list, e -> separate(e.salary));
public static <K, E> Map<K, List<E>> groupBy(List<? extends E> data, Function<? super E, ? extends K> fun) {
Map<K, List<E>> result = new HashMap<>();
for(E e : data) {
K k = fun.apply(e);<br> List<E> l = result.get(k);
if(l == null) {
l = new ArrayList<>();
result.put(k, l);
}
l.add(e);
}
return result;<br>
}
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有