jdk8 Lambda表达式 集合过滤,排序,最大值,最小值,平均值等操作
package cn.sh.ideal.jdk8;
import cn.sh.ideal.bean.UserBean;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Lambda表达式写法
*
* @author gilbert
*/
public class LambdaDemo {
/**
* 功能描述 无参无返回值
*
* @param list
* @return void
* @author gilbert
* @date 2019/6/12
*/
public static void lambdaWithParamAndNoReturn(List
list.forEach(userBean -> System.out.println("hello," + userBean.getUsername()));
}
/**
*功能描述 重新封装集合数据
* @author gilbert
* @date 2019/6/12
* @param [list]
* @return void
*/
public static void initList(List
List
userList.forEach(userBean -> System.out.println("new list:" + userBean.getUsername()));
}
/**
*功能描述 集合过滤
* @author gilbert
* @date 2019/6/12
* @param [list]
* @return void
*/
public static void filterList(List
//List
List
filterList.forEach(userBean -> System.out.println("filter list:" + userBean.getUsername()));
}
/**
*功能描述 排序
* @author gilbert
* @date 2019/6/12
* @param [list]
* @return void
*/
public static void sortList(List
//按userid排序
List
sortList.forEach(userBean -> System.out.println("sortList:" + userBean.getUserid() + "," + userBean.getUsername()));
}
/**
*功能描述 多条件排序
* @author gilbert
* @date 2019/6/12
* @param [list]
* @return void
*/
public static void multiSortList(List
list.sort(Comparator.comparing(UserBean::getUserid).thenComparing(UserBean::getUsername));
list.forEach(userBean -> System.out.println("multiSortList:" + userBean.getUserid() + "," + userBean.getUsername()));
}
/**
*功能描述 倒序
* @author gilbert
* @date 2019/6/12
* @param [list]
* @return void
*/
public static void reversedSortList(List
//第一种写法
Comparator
list.sort(comparator.reversed());
//第二种写法
//list.sort(Comparator.comparing(UserBean::getUserid).reversed());
list.forEach(userBean -> System.out.println("reverseSortList:" + userBean.getUserid() + "," + userBean.getUsername()));
}
/**
*功能描述 多条件倒序
* @author gilbert
* @date 2019/6/12
* @param [list]
* @return void
*/
public static void multiReversedSortList(List
list.sort(Comparator.comparing(UserBean::getUserid).thenComparing(UserBean::getUsername).reversed());
list.forEach(userBean -> System.out.println("multiReversedSortList:" + userBean.getUserid() + "," + userBean.getUsername()));
}
/**
*功能描述 集合分组
* @author gilbert
* @date 2019/6/12
* @param [list]
* @return void
*/
public static void groupByList(List
Map
groupByMap.forEach((k,v) -> System.out.println(k+"," + v));
}
/**
*功能描述 求和
* @author gilbert
* @date 2019/6/12
* @param [list]
* @return void
*/
public static void sumByList(List
System.out.println("sum="+ list.stream().mapToInt(UserBean::getUserid).sum());
}
/**
*功能描述 最大值
* @author gilbert
* @date 2019/6/12
* @param [list]
* @return void
*/
public static void maxByList(List
OptionalInt optional = list.stream().mapToInt(UserBean::getUserid).max();
System.out.println("max=" + optional.getAsInt());
}
/**
*功能描述 最小值
* @author gilbert
* @date 2019/6/12
* @param [list]
* @return void
*/
public static void minByList(List
OptionalInt optional = list.stream().mapToInt(UserBean::getUserid).min();
System.out.println("min=" + optional.getAsInt());
}
/**
*功能描述 平均值
* @author gilbert
* @date 2019/6/12
* @param [list]
* @return void
*/
public static void averageByList(List
OptionalDouble optionalDouble = list.stream().mapToInt(UserBean::getUserid).average();
System.out.println("average=" + optionalDouble.getAsDouble());
}
/**
*功能描述 List转map
* @author gilbert
* @date 2019/6/12
* @param [list]
* @return void
*/
public static void listToMap(List
//用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2
Map
map.forEach((k,v) -> System.out.println("k=" + k + ",v=" + v));
}
/**
*功能描述 map转list
* @author gilbert
* @date 2019/6/12
* @param [map]
* @return void
*/
public static void mapToList(Map
List
list.forEach(userBean -> System.out.println(userBean.getUserid() + "," + userBean.getUsername()));
}
/**
*功能描述 字符串转list
* @author gilbert
* @date 2019/6/12
* @param [str]
* @return void
*/
public static void stringToList(String str){
//不需要处理
//
//需要处理
List
list.forEach(string -> System.out.println(string));
}
/**
*功能描述 姓名以逗号拼接
* @author gilbert
* @date 2019/6/12
* @param [list]
* @return void
*/
public static void joinStringValueByList(List
System.out.println(list.stream().map(UserBean::getUsername).collect(Collectors.joining(",")));
}
/**
*功能描述 分组统计
* @author gilbert
* @date 2019/6/12
* @param [list]
* @return void
*/
public static void countByList(List
Map
map.forEach((k,v) -> System.out.println("key=" + k + ",value=" + v));
}
public static void main(String[] args) {
//List
List
new UserBean(2, "AKB002", "apple"),
new UserBean(4, "AKB004", "dog"),
new UserBean(5, "AKB005", "egg"),
new UserBean(6, "AKB006", "frog"),
new UserBean(6, "AKB006", "banana"),
new UserBean(7, "AKB007", "google"),
new UserBean(3, "AKB003", "cat"))
.collect(Collectors.toList());
lambdaWithParamAndNoReturn(list);
initList(list);
filterList(list);
sortList(list);
reversedSortList(list);
multiSortList(list);
multiReversedSortList(list);
groupByList(list);
sumByList(list);
maxByList(list);
minByList(list);
averageByList(list);
listToMap(list);
String str = "apple,banana,cat,dog";
stringToList(str);
Map
{
put(1, "apple");
put(2, "banana");
put(3, "cat");
put(4, "dog");
put(5, "frog");
}
};
mapToList(map);
joinStringValueByList(list);
countByList(list);
}
}