个人理解:java是一个比较易上手的一门语言编程,很多的小伙伴参加一些培训看一些视频,经历了一些项目完全就可以正常的开发,开发技巧还是要看一些经验而已,或多或少需要一些大神的指点或者一些见解,本人认为java更像是一个工具库,你只需要用法在不考虑效率的问题完全的开发是没有问题的,下面是学习java8的表达式的一些demo,与大家分享,后续会逐渐地补充。
原创地址 : https://yanchenglong.blog.csdn.net/article/details/96974536
1.循环
java8 之前我们常用的写法和java8的 循环写法
/**
* @author YanChengLong
* @date 2019/7/23
* @desc 测试循环
*/
public static void main(String[] args) {
System.out.println("======java 8 之前的写法 第一种=========");
/*java 8 之前的写法 */
//***************************************************//
//1.定义一个集合
List<Integer> list = Arrays.asList(1, 2, 3, 3, 2, 4);
//第一种写法
for (Integer i : list) {
System.out.println(i);
}
System.out.println();
System.out.println("===============");
System.out.println();
System.out.println("======java 8 之前的写法 第二种=========");
//第二种写法
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println();
System.out.println("===============");
System.out.println();
System.out.println("======java 8 写法=========");
//***************************************************//
/*java 8 写法 */
//***************************************************//
//第一种
list.stream().forEach(li -> System.out.println(li));
//第二种
list.forEach(li -> System.out.println(li));
//***************************************************//
}
2.排序 我们设置一个用户实体类 分别 用户名 年龄 时间 如下:
/**
* @author YanChengLong
* @date 2019/7/23
* @desc 设置实体类User
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserDTO {
//用户名
private String name;
//年龄
private Integer age;
//时间
private LocalDateTime time;
}
/**
* @author YanChengLong
* @date 2019/7/23
* @desc 排序 例子根据时间正序倒序
*/
public static void main(String[] args) {
//初始化数据
List<UserDTO> userDTOList = new ArrayList();
userDTOList.add(new UserDTO("测试1", 1, LocalDateTime.parse("2007-10-03T10:15:30")));
userDTOList.add(new UserDTO("测试2", 2, LocalDateTime.parse("2008-10-03T10:15:30")));
userDTOList.add(new UserDTO("测试3", 3, LocalDateTime.parse("2009-10-03T10:15:30")));
System.out.println("正序");
System.out.println(userDTOList.stream().sorted(Comparator.comparing(UserDTO::getTime)).collect(Collectors.toList()));
System.out.println();
System.out.println("倒序");
System.out.println(userDTOList.stream().sorted(Comparator.comparing(UserDTO::getTime).reversed()).collect(Collectors.toList()));
}
3.去重复
/**
* @author YanChengLong
* @date 2019/7/23
* @desc 去重复
*/
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 3, 2, 4);
System.out.println(list.stream().distinct().collect(Collectors.toList()));
}
4.最大值 最小值 总和 平均值
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 3, 2, 4);
IntSummaryStatistics intSummaryStatistics = list.stream().mapToInt(li -> li).summaryStatistics();
System.out.println("最大值 : " + intSummaryStatistics.getMax());
System.out.println("最小值 : " + intSummaryStatistics.getMin());
System.out.println("总和 : " + intSummaryStatistics.getSum());
System.out.println("平均值 : " + intSummaryStatistics.getAverage());
}
5.过滤
/**
* @author YanChengLong
* @date 2019/7/23
* @desc 过滤
*/
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 3, 2, 4);
//过滤掉集合里面等于 1的
System.out.println(list.stream().filter(li -> li != 1).collect(Collectors.toList()));
}
6.list转换map
/**
* @author YanChengLong
* @date 2019/7/23
* @desc list 转换map
*/
public static void main(String[] args) {
List<UserDTO> userDTOList = new ArrayList();
userDTOList.add(new UserDTO("测试1", 1, LocalDateTime.parse("2007-10-03T10:15:30")));
userDTOList.add(new UserDTO("测试2", 2, LocalDateTime.parse("2008-10-03T10:15:30")));
userDTOList.add(new UserDTO("测试3", 3, LocalDateTime.parse("2009-10-03T10:15:30")));
//例子1
Map<String, Object> map = userDTOList.stream().collect(Collectors.toMap(UserDTO::getName, UserDTO::getAge));
map.forEach((key, value) -> System.out.println("key:" + key + "," + "value:" + value));
//例子2
Map<String, UserDTO> map1 = userDTOList.stream().collect(Collectors.toMap((key -> key.getName()), (value -> value)));
map1.forEach((key, value) -> System.out.println(key + "," + value));
}
7.统计
/**
* @author YanChengLong
* @date 2019/7/23
* @desc 统计 (根据年龄进行统计)
*/
public static void main(String[] args) {
List<UserDTO> userDTOList = new ArrayList();
userDTOList.add(new UserDTO("测试1", 1, LocalDateTime.parse("2007-10-03T10:15:30")));
userDTOList.add(new UserDTO("测试2", 2, LocalDateTime.parse("2008-10-03T10:15:30")));
userDTOList.add(new UserDTO("测试3", 3, LocalDateTime.parse("2009-10-03T10:15:30")));
userDTOList.add(new UserDTO("测试4", 2, LocalDateTime.parse("2008-10-03T10:15:30")));
userDTOList.add(new UserDTO("测试5", 3, LocalDateTime.parse("2009-10-03T10:15:30")));
Map<Integer, List<UserDTO>> data = userDTOList.stream().collect(Collectors.groupingBy(UserDTO::getAge));
data.forEach((key, value) -> System.out.println(key + "," + value));
}