java8
循环ForEach
对List、数组、Map都可以循环,nice
public class Test {
public static void main(String[] args) throws Exception {
mapForeach();
}
//forEach对List的循环
public static void listForeach(){
List<String> lst = new ArrayList<String>(5){{
add("A");
add("B");
add("H");
add("O");
add("M");
}};
lst.forEach(System.out::print);//ABHOM
System.out.println();
//配合filter处理就更赞
lst.forEach((item)-> System.out.print(item.concat("_")));//A_B_H_O_M_
}
//forEach对数组的循环
public static void arrForeach(){
String[] strArr = new String[]{"A","B","C","D"};
Arrays.stream(strArr).forEach(System.out::print);//ABCD
}
//forEach对Map的循环
public static void mapForeach(){
Map<String,Object> mps = new HashMap<String,Object>(5){{
put("a",1);
put("b",true);
put("c",23.44F);
put("d","hello");
put("e",11L);
}};
mps.forEach((k,v)-> System.out.print(k.concat(":").concat(String.valueOf(v)) + " "));
String str = "hello";
}
}
java8全新的日期和时间API
LocalDate
public class Test {
public static void main(String[] args) throws Exception {
LocalDate localDate = LocalDate.now();
int year = localDate.getYear();
Month month = localDate.getMonth();
int day = localDate.getDayOfMonth();
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
System.out.println(year);//2021
System.out.println(month);//JANUARY
System.out.println(day);//4
System.out.println(dayOfWeek);//MONDAY
}
}
其实还不如用Data+SimpleDateFormat(虽然线程不安全)
public class Test {
public static void main(String[] args) throws Exception {
LocalDate localDate = LocalDate.now();
LocalDate localDate1 = LocalDate.of(2019, 9, 10);
int year1 = localDate.get(ChronoField.YEAR);
int month1 = localDate.get(ChronoField.MONTH_OF_YEAR);
int day1 = localDate.get(ChronoField.DAY_OF_MONTH);
int dayOfWeek1 = localDate.get(ChronoField.DAY_OF_WEEK);
System.out.println(localDate1);//2019-09-10
System.out.println(year1);//2021
System.out.println(month1);//1
System.out.println(day1);//4
System.out.println(dayOfWeek1);//1
}
}
JDK8 的 Lambda、Stream、LocalDate
Lambda
其实也挺简单,就分成两部分,一个是参数(),另一个是语句{}
使用Lambda进行遍历:(用到java8的foreach)
对于map
map.forEach((k, v) -> {
System.out.println("k=" + k + ",v=" + v);
});
对于list
list.forEach(v -> {
System.out.println(v);
});
list的另一种写法
list.forEach(System.out::println);
Lambda除了在for循环遍历中使用外,它还可以代替匿名的内部类。
JDK8 的 Stream
注意事项:所有 Stream 的操作必须以 lambda 表达式为参数。
- 过滤用filter,不过滤用map
Stream适合拿来过滤集合(List、map)
List<String> result2 = list.stream().filter(str -> !"李四".equals(str)).collect(Collectors.toList());//把集合中的李四过滤掉
String result3 = list.stream().filter(str -> "李四".equals(str)).findAny().orElse("找不到!");//李四
int sum = lists.stream().filter(u -> "张三".equals(u.getName())).mapToInt(u -> u.getAge()).sum();//计算所有张三的年龄总和
map方法用于映射每个元素到对应的结果,一对一。(不过滤)
示例一:转换大写
List<String> list4 = list3.stream().map(String::toUpperCase).collect(Collectors.toList());//[zhangSan, liSi, wangWu] 到 [ZHANGSAN, LISI, WANGWU]
示例二:转换数据类型
List<Integer> list41 = list31.stream().map(Integer::valueOf).collect(Collectors.toList());//[1, 2, 3] 从String类型到Integer类型
示例三:获取平方
List<Integer> list6 = list5.stream().map(n -> n * n).collect(Collectors.toList());//[1, 2, 3, 4, 5] 到 [1, 4, 9, 16, 25]
示例三:通过与 mapToInt 计算和
flatMap 方法用于映射每个元素到对应的结果,一对多。(不过滤)
List<String> list8 = list7.stream().flatMap(str -> Stream.of(str.split(" ")))
.filter(world -> world.length() > 0).collect(Collectors.toList());
limit 方法用于获取指定数量的流。
示例一:获取前3条数的数据
rd.ints().limit(3).forEach(System.out::println);
取前3条数据,但是扔掉了前面的2条,可以理解为拿到的数据为 2<=i<3 (i 是数值下标)
List<String> list10 = list9.stream().map(User::getName).limit(3).skip(2).collect(Collectors.toList());