本文列举了十个使用一行代码即可独立完成(不依赖其他代码)的业务逻辑,主要依赖的是Java8中的Lambda和Stream等新特性以及try-with-resources、JAXB等。
1、对列表/数组中的每个元素都乘以2
// Range是半开区间
int[] ia = range(1, 10).map(i -> i * 2).toArray();
List<Integer> result = range(1, 10).map(i -> i * 2).boxed().collect(toList());
int[] ia = range(1, 10).map(i -> i * 2).toArray();
List<Integer> result = range(1, 10).map(i -> i * 2).boxed().collect(toList());
2、计算集合/数组中的数字之和
range(1, 1000).sum();
range(1, 1000).reduce(0, Integer::sum);
Stream.iterate(0, i -> i + 1).limit(1000).reduce(0, Integer::sum);
IntStream.iterate(0, i -> i + 1).limit(1000).reduce(0, Integer::sum);
(1, 1000).sum();
range(1, 1000).reduce(0, Integer::sum);
Stream.iterate(0, i -> i + 1).limit(1000).reduce(0, Integer::sum);
IntStream.iterate(0, i -> i + 1).limit(1000).reduce(0, Integer::sum);
3、验证字符串是否包含集合中的某一字符串
final List<String> keywords = Arrays.asList("brown", "fox", "dog", "pangram");
final String tweet = "The quick brown fox jumps over a lazy dog. #pangram http://www.rinkworks.com/words/pangrams.shtml";
keywords.stream().anyMatch(tweet::contains);
keywords.stream().reduce(false, (b, keyword) -> b || tweet.contains(keyword), (l, r) -> l || r);
"brown", "fox", "dog", "pangram");
final String tweet = "The quick brown fox jumps over a lazy dog. #pangram http://www.rinkworks.com/words/pangrams.shtml";
keywords.stream().anyMatch(tweet::contains);
keywords.stream().reduce(false, (b, keyword) -> b || tweet.contains(keyword), (l, r) -> l || r);
4、读取文件内容
- 原作者认为try with resources也是一种单行代码编程。
try (BufferedReader reader = new BufferedReader(new FileReader("data.txt"))) {
String fileText = reader.lines().reduce("", String::concat);
}
try (BufferedReader reader = new BufferedReader(new FileReader("data.txt"))) {
List<String> fileLines = reader.lines().collect(toCollection(LinkedList<String>::new));
}
try (Stream<String> lines = Files.lines(new File("data.txt").toPath(), Charset.defaultCharset())) {
List<String> fileLines = lines.collect(toCollection(LinkedList<String>::new));
}
(BufferedReader reader = new BufferedReader(new FileReader("data.txt"))) {
String fileText = reader.lines().reduce("", String::concat);
}
try (BufferedReader reader = new BufferedReader(new FileReader("data.txt"))) {
List<String> fileLines = reader.lines().collect(toCollection(LinkedList<String>::new));
}
try (Stream<String> lines = Files.lines(new File("data.txt").toPath(), Charset.defaultCharset())) {
List<String> fileLines = lines.collect(toCollection(LinkedList<String>::new));
}
5、输出歌曲《Happy Birthday to You!》 – 根据集合中不同的元素输出不同的字符串
range(1, 5).boxed().map(i -> { out.print("Happy Birthday "); if (i == 3) return "dear NAME"; else return "to You"; }).forEach(out::println);
(1, 5).boxed().map(i -> { out.print("Happy Birthday "); if (i == 3) return "dear NAME"; else return "to You"; }).forEach(out::println);
6、过滤并分组集合中的数字
Map> result = Stream.of(49, 58, 76, 82, 88, 90).collect(groupingBy(forPredicate(i -> i > 60, "passed", "failed")));
> result = Stream.of(49, 58, 76, 82, 88, 90).collect(groupingBy(forPredicate(i -> i > 60, "passed", "failed")));
7、获取并解析xml协议的Web Service
FeedType feed = JAXB.unmarshal(new URL("http://search.twitter.com/search.atom?&q=java8"), FeedType.class);
JAXB.marshal(feed, System.out);
new URL("http://search.twitter.com/search.atom?&q=java8"), FeedType.class);
JAXB.marshal(feed, System.out);
8、获得集合中最小/最大的数字
int min = Stream.of(14, 35, -7, 46, 98).reduce(Integer::min).get();
min = Stream.of(14, 35, -7, 46, 98).min(Integer::compare).get();
min = Stream.of(14, 35, -7, 46, 98).mapToInt(Integer::new).min();
int max = Stream.of(14, 35, -7, 46, 98).reduce(Integer::max).get();
max = Stream.of(14, 35, -7, 46, 98).max(Integer::compare).get();
max = Stream.of(14, 35, -7, 46, 98).mapToInt(Integer::new).max();
of(14, 35, -7, 46, 98).reduce(Integer::min).get();
min = Stream.of(14, 35, -7, 46, 98).min(Integer::compare).get();
min = Stream.of(14, 35, -7, 46, 98).mapToInt(Integer::new).min();
int max = Stream.of(14, 35, -7, 46, 98).reduce(Integer::max).get();
max = Stream.of(14, 35, -7, 46, 98).max(Integer::compare).get();
max = Stream.of(14, 35, -7, 46, 98).mapToInt(Integer::new).max();
9、并行处理
long result = dataList.parallelStream().mapToInt(line -> processItem(line)).sum();
result = dataList.parallelStream().mapToInt(line -> processItem(line)).sum();
10、集合上的各种查询(LINQ in Java)
<Album> albums = Arrays.asList(unapologetic, tailgates, red);
//筛选出至少有一个track评级4分以上的专辑,并按照名称排序后打印出来。
albums.stream()
.filter(a -> a.tracks.stream().anyMatch(t -> (t.rating >= 4)))
.sorted(comparing(album -> album.name))
.forEach(album -> System.out.println(album.name));
//合并所有专辑的track
List<Track> allTracks = albums.stream()
.flatMap(album -> album.tracks.stream())
.collect(toList());
//根据track的评分对所有track分组
Map<Integer, List<Track>> tracksByRating = allTracks.stream()
.collect(groupingBy(Track::getRating));
如果对自己未来有想法,想提升自己,你现在在JAVA这条路上挣扎,也想在IT行业拿高薪,可以参加我们免费的公开课试听学习 干货满满的,选择最适合自己的课程学习,技术大牛亲授,课程内容有:Java工程化、高性能及分布式、高性能、深入浅出。高架构。性能调优、Spring,MyBatis,Netty源码分析和大数据等多个知识点。如果你想拿高薪的,想学习的,想就业前景好的,想跟别人竞争能取得优势的,想进阿里面试但担心面试不过的,你都可以来。
群号:468947140
进群修改群备注:开发年限-地区-经验
点击链接加入群聊【Java-BATJ企业级资深架构】:https://jq.qq.com/?_wv=1027&k=52j2FVO