lambda和stream流是jdk1.8的新特性,可以在以后的编码过程中慢慢用起来。这阶段学习完之后,准备入手线程,这个模块极其重要,本阶段相当于给线程铺路。复盘一下当前开发,未进行多线程和并发问题的考虑,后期坑很大,在接下的开发过程中多去考虑线程类的问题。再接下来的计划是IO流和Invoke,Collection最后入手。
Lambda
lambda的存在意义是什么?简化代码,仅此而已,并且简化的范围有限,函数式接口—仅有一个抽象方法的接口
那为什么还去使用它呢?优雅式编码以及它的使用是JDK的一种趋势,JDK底层多使用Lamda表达式去进行构建。
1、Lambda的使用
(抽象类被重写的形参实体)-> {被重写方法的方法实体}
ublic class LambdaDem_one {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(222);
}
});
thread.start();
Thread thread1 = new Thread(() ->
System.out.println(111)
);
//必须要start才能起线程
thread1.start();
//run方法被重写,且未提现到当前的调用中,调用接口特定
new Thread(() ->
System.out.println(333) ).start();
}
2、Lambda的优化
(1)当方法体中代码只有一行时,可去掉"{}"和";"
(2)当代码只有一行时,就算存在return,也可以进行省略
(3)参数类型默认是可以省略的,在当前的表达式中,可以去进行类型的搜索—关注用法
(4)当单参数时,存放参数的()是可以省略的
public static void main(String[] args) {
List<User> userList = new ArrayList<>();
userList.add(new User("qw", 15));
userList.add(new User("er", 78));
userList.add(new User("tr", 10));
userList.add(new User("uy", 700));
userList.add(new User("sd", 3));
Collections.sort(userList, new Comparator<User>() {
@Override
public int compare(User o1, User o2) {
return o1.getAge() - o2.getAge();
}
});
System.out.println(userList);
Collections.sort(userList, (User q, User w) -> {
return q.getAge() - w.getAge();
});
System.out.println(userList);
Collections.sort(userList, (User q, User w) ->
//自动默认为返回
q.getAge() - w.getAge()
);
System.out.println(userList);
Collections.sort(userList, (q, w) ->
//自动默认为返回
q.getAge() - w.getAge()
);
System.out.println(userList);
}
泛型
如何去理解泛型,泛型相当于是一种针对多种对象的,提供公用方法的一种机制,它的用处多体现在设计层面上
1、类中使用泛型
/**
* 使用了泛型
* @param <E>
*/
class xue<E>{
private ArrayList arrayList = new ArrayList();
public void add(E t){
arrayList.add(t);
}
}
2、方法中使用泛型
//注意泛型方法的格式
private static <T> String getStr(T[] ints) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ints.length; i++) {
sb.append(i == ints.length - 1 ? ints[i] : ints[i] + ",");
}
//相当于返回也是String类型的格式
return sb.toString();
}
注:在方法中要对泛型进行声明
3、泛型通配符
/**
* 在使用的时候 ?的使用,首先在使用的时候,?代表一切类型
* ?号作为通配符去进行使用
* 泛型上限 <? extends Object> - 其对象继承Object或者是Object其子类的子类便可 - 有助于阅读源码
* 泛型下限 <? super Car> - 表示其是car的父类就可
*/
public class GenericityDem_three {
public static void main(String[] args) {
}
public static void run(List<?> e){
System.out.println(e + "run");
}
}
Stream流
stream流是什么?stream流是一种处理Collection和数组的方式,在原有的API基础上,考虑了安全性(并发)和快速性。
如何理解stream流?Stream流相当于一条流水线,将集合中的数据放在这条流水线上去进行筛选和操作,最后获取想要的集合—流是一种手段,集合是一种目的。
流的使用,与自身的各种API去进行结合。
1、获取流的几种方式
(1)Collection直接通过.stream()去进行获取
(2)Map可获取key,value与entrySet的流
(3)数组:Stream.of()和Arrays.Stream()去进行流获取
public static void main(String[] args) {
//通过集合获取流
Collection<String> list = new ArrayList<>();
Stream<String> stream = list.stream();
//map中过去流的三种方式
Map<String, String> stringMap = new HashMap<>();
Stream<String> stream1 = stringMap.values().stream();
Stream<String> stream2 = stringMap.keySet().stream();
//将map转化为key=value这种形式
Stream<Map.Entry<String, String>> stream3 = stringMap.entrySet().stream();
//数组获取
String[] strings = new String[]{"wqwq","wqwqwq"};
Stream<String> stream4 = Arrays.stream(strings);
Stream<String> strings1 = Stream.of(strings);
}
2、流常用的几个API
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("qwq", "wqwq");
map.put("wwq", "wytwq");
long count = map.entrySet().stream().count();
System.out.println(count);
//即只获取第一个元素,其它元素不对其进行获取
map.entrySet().stream().limit(1).forEach(s -> System.out.println(s));
map.entrySet().stream().skip(1).forEach(s -> System.out.println(s));
map.entrySet().stream().map(s -> "Xxx" + s).forEach(s -> System.out.println(s));
String[] strings = new String[]{"qwq","wqw"};
Stream<String> stream = Arrays.stream(strings);
List<String> strings1 = new ArrayList<>();
strings1.add("qwq");
strings1.add("qwq");
strings1.add("qwq");
strings1.add("qwq");
Stream<String> stream1 = strings1.stream();
Stream.concat(stream, stream1).forEach(s -> System.out.println(s));
}
注:这几种API中,foreach()和count()是终结方法,相当于流在此终结,不能再去进行对象的操作,而其它几种API可以对流进行接下来的操作。—— 这块体现了一种编程的流式思想;
3、Stream的收流——collect()
在收流时会对流的状态去进行判断校验,当流状态正常才能去进行操作,否则会报流关闭类的异常
public static void main(String[] args) {
String[] strings = new String[]{"qwqwq","qwqwq"};
//将流转换为Set集合形式 - 流中集合未取
Stream<String> stream = Arrays.stream(strings);
// Set<String> collect = stream.collect(Collectors.toSet());
// collect.forEach(s -> System.out.println(s));
// //将流转换为List集合形式 - 如果流中集合已经取了,那么再次去取流就会报错
// stream.collect(Collectors.toList()).forEach(s -> System.out.println(s));
// List<String> collect1 = stream.collect(Collectors.toList());
// collect1.forEach(s -> System.out.println(s));
//当使用collect的时候,就表示stream流已经取完并关闭0
// stream.collect(Collectors.toList()).forEach(s -> System.out.println(s));
// stream.collect(Collectors.toSet()).forEach(s -> System.out.println(s));
//转换为数组 - 将其强转为String类型的对象
String[] objects = stream.toArray(String[]::new);
System.out.println(objects);
}
stream流是使用并行的一种使用并行处理的一种方式,增强了集合类的处理能力,极大程度上规避了集合foreach等操作带来的时间损耗,并且流是面向操作的,更加实用。