Java8使用实例
Stream
过滤器
List<String> list = Lists.newArrayList("aaa", "bbb", "cccc", "dd");
List<String> result = list.stream()
.filter(e -> e.length() >= 3)
.collect(Collectors.toList());
System.out.println(result);
排序
List<String> list = Lists.newArrayList("aaa", "bbb", "cccc", "dd");
Long count = list.stream()
.parallel()
.filter(e -> e.length() >= 3)
.sorted()
.count();
System.out.println(count);
转换数组
Object[] objs = Stream.of("aaa", "bbbb", "cc", "ddd").toArray();
String[] strs = Stream.of("aaa", "bbbb", "cc", "ddd").toArray(String[]::new);
System.out.println(Arrays.asList(objs));
System.out.println(Arrays.asList(strs));
转换map
Map<String, String> map = Stream.of("hello world").collect(Collectors.toMap(s -> s.substring(0, 5), s -> s));
System.out.println(map);
MapReduce
分组->归集
List<Integer> list = Arrays.asList(100, 200, 300, 400, 500);
double r = list.stream().map(a -> (a + a * 0.12)).reduce((sum, a) -> sum + a).get();
System.out.println(r);
统计
List<Integer> list = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29);
IntSummaryStatistics stat = list.stream().mapToInt(x -> x).summaryStatistics();
System.out.println(stat.getAverage());
System.out.println(stat.getMax());
System.out.println(stat.getMin());
System.out.println(stat.getSum());
遍历list&map
遍历list
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
list.forEach(i -> System.out.println(i));
List初始化数据
Arrays.asList(100, 200, 300, 400, 500);
List<String> list = new ArrayList<String>() {{
add("A");
add("B");
if (...) {
add("C");
}
}};
System.out.println(list);
遍历map
Map<String, Integer> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
map.forEach((k, v) -> System.out.println("Key:" + k + ",v:" + v));
Optional
可以理解为防止NPE产生
Person person = new Person();
person.setName("haha");
String name = Optional.ofNullable(person).map(u -> u.getName()).orElse("unknow");
System.out.println(name);
实体类嵌套
Person person = new Person();
Car car = new Car();
car.setBrand("aaa");
person.setCar(car);
String brand = Optional.of(person)
.map(Person::getCar)
.map(Car::getBrand)
.orElse("unknow");
System.out.println(brand);
排序使用
List<Integer> list = Arrays.asList(20, 30, 10, 34, 13, 9, 39, 23);
Optional<Integer> min = list.stream().min(Comparator.comparing(a -> a));
if (min.isPresent()) {
System.out.println(min.get());
}
Interface
函数式编程
定义函数接口
@FunctionalInterface
interface Convert<F, T> {
T convert(F t);
}
Convert<String, Integer> convert = Integer::valueOf;
System.out.println(convert.convert("123"));
Convert<Integer, String> convert1 = String::valueOf;
System.out.println(convert1.convert(1));
单方法接口
Converter converter = (x, y) -> {
return x.getName() + "-" + y.getName();
};
System.out.println(converter.add(person1, person2));
interface Converter {
String add(Person a, Person b);
}
Runnable
Runnable runnable = () -> {
// 业务处理
};
threadPool.submit(runnable);
类似的Thread,可以这样实现:
new Thread(() -> {
// 业务处理
}).start();
接口式的匿名内部类
public interface JavaInterface {
void get();
void set();
}
public static void test() {
JavaInterface inter = new JavaInterface() {
@Override
public void get() {
System.out.println("get");
}
@Override
public void set() {
System.out.println("set");
}
};
inter.get();
}
Try-catch
try(Resource) {…} catch(e) {…},Resource
必须实现Closeable
接口
File file = new File("/path/test.txt");
try (InputStream is = new FileInputStream(file)) {
byte[] bytes = new byte[1024];
int length;
while ((length = is.read(bytes)) != -1) {
System.out.println(new String(bytes, "UTF-8"));
}
} catch (Exception e) {
e.printStackTrace();
}