//新建测试类
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
public class Author {
//id
private Long id;
//姓名
private String name;
//年龄
private Integer age;
//简介
private String intro;
//作品
private List<Book> books;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode//用于后期的去重使用
public class Book {
//id
private Long id;
//书名
private String name;
//分类
private String category;
//评分
private Integer score;
//简介
private String intro;
}
//代码示例
/**
* 获取任意一个年龄大于18的作家,如果存在就输出他的名字
*/
@Test
public void test18(){
List<Author> list = getAuthors();
Optional<Author> optionalS = list.stream()
.filter(author -> author.getAge() > 18)
.findAny();
System.out.println(optionalS.get());
}
/**
* 判断作家是否都没有超过100岁的。
*/
@Test
public void test17(){
List<Author> list = getAuthors();
boolean flag = list.stream().noneMatch(author -> author.getAge() > 100);
System.out.println(flag);
}
/**
* 判断是否所有的作家都是成年人
*/
@Test
public void test16(){
List<Author> list = getAuthors();
boolean flag = list.stream().allMatch(author -> author.getAge() >= 18);
System.out.println(flag);
}
/**
* 判断是否有年龄在29以上的作家
*/
@Test
public void test15(){
List<Author> list = getAuthors();
boolean flag = list.stream().anyMatch(author -> author.getAge() > 35);
System.out.println(flag);
}
/**
* 获取一个Map集合,map的key为作者名,value为List<Book>
*/
@Test
public void test14(){
List<Author> list = getAuthors();
Map<String,Object> map = list.stream()
.distinct()
.collect(Collectors.toMap
(author -> author.getName(), author -> author.getBooks()
.stream().map(a->a.getName())
.distinct()
.collect(Collectors.toList())));
System.out.println(map);
}
/**
* 获取一个所有书名的Set集合
*/
@Test
public void test13(){
List<Author> list = getAuthors();
Set<Book> strings = list.stream().flatMap(author -> author.getBooks().stream())
.collect(Collectors.toSet());
Set<String> strings2 = list.stream().flatMap(author -> author.getBooks().stream())
.map(a->a.getName())
.distinct()
.collect(Collectors.toSet());
System.out.println(strings2);
}
/**
* 获取一个存放所有作者名字的List集合。
*/
@Test
public void test12(){
List<Author> list = getAuthors();
List<String> strings = list.stream().map(author -> author.getName())
.distinct()
.collect(Collectors.toList());
System.out.println(strings);
}
/**
* 分别获取这些作家的所出书籍的最高分和最低分并打印
*/
@Test
public void test11(){
List<Author> list = getAuthors();
Optional<Integer> strings = list.stream().flatMap(author -> author.getBooks().stream())
.map(a->a.getScore())
.max((s1,s2)->s1 - s2);
Optional<Integer> strings2 = list.stream().flatMap(author -> author.getBooks().stream())
.map(a->a.getScore())
.min((s1,s2)->s1 - s2);
System.out.println(strings.get() + "---" +strings2.get());
}
/**
* 打印这些作家的所出书籍的数目,注意删除重复元素
*/
@Test
public void test10(){
List<Author> list = getAuthors();
long count = list.stream().flatMap(author -> author.getBooks().stream())
.distinct()
.count();
System.out.println(count);
}
/**
* 打印现有书籍的所有分类。要求对分类进行去重。不能出现这种格式:哲学,爱情
*/
@Test
public void test09(){
List<Author> list = getAuthors();
List<String> strings = list.stream()
.flatMap(author -> author.getBooks().stream())
.distinct()
.flatMap(a->Arrays.stream(a.getCategory().split(",")))
.collect(Collectors.toList());
for (String s : strings){
System.out.println(s);
}
}
/**
* 打印所有书籍的名字。要求对重复的元素进行去重。
*/
@Test
public void test08(){
List<Author> list = getAuthors();
List<String> strings = list.stream()
.flatMap(author -> author.getBooks().stream())
.distinct()
.map(a->a.getName())
.collect(Collectors.toList());
for (String author : strings){
System.out.println(author);
}
}
/**
* 打印除了年龄最大的作家外的其他作家,要求不能有重复元素,并且按照年龄降序排序
*/
@Test
public void test07(){
List<Author> list = getAuthors();
List<Author> authors = list.stream().distinct()
.sorted((a1,a2) ->a2.getAge() - a1.getAge())
.skip(1)
.collect(Collectors.toList());
for (Author author : authors){
System.out.println(author);
}
}
/**
* 对作家进行年龄排序,不能有重复.并且打印年龄最大的一个作家
*/
@Test
public void test06(){
List<Author> list = getAuthors();
List<Author> authors = list.stream().distinct()
.sorted((a1,a2) ->a2.getAge() - a1.getAge())
.limit(1)
.collect(Collectors.toList());
for (Author author : authors){
System.out.println(author);
}
}
/**
* 对作家进行年龄排序,不能有重复
*/
@Test
public void test05(){
List<Author> list = getAuthors();
List<Author> authors = list.stream().distinct()
.sorted((o1,o2)->o2.getAge() - o1.getAge())
.collect(Collectors.toList());
for (Author author : authors){
System.out.println(author);
}
}
/**
* 打印所有作家的姓名,不能有重复的姓名
*/
@Test
public void test04(){
List<Author> list = getAuthors();
List<String> strings = list.stream().map(author -> author.getName())
.distinct()
.collect(Collectors.toList());
for (String author : strings){
System.out.println(author);
}
}
/**
* 打印所有作家的姓名
*/
@Test
public void test03(){
List<Author> list = getAuthors();
List<String> authors = list.stream().map(author -> author.getName()).collect(Collectors.toList());
for (String author : authors){
System.out.println(author);
}
}
/**
* 打印说明姓名长度大于1的作家
*/
@Test
public void test02(){
List<Author> list = getAuthors();
List<Author> authors = list.stream().filter(author -> author.getName().length() > 1).collect(Collectors.toList());
for (Author author : authors){
System.out.println(author);
}
}
/**
* 打印所有年龄小于18的作者
*/
@Test
public void test01(){
List<Author> list = getAuthors();
List<Author> authors = list.stream().filter(author -> author.getAge() < 18).collect(Collectors.toList());
for (Author author : authors){
System.out.println(author);
}
}
标签:stream,author,list,void,List,特性,println,java8 From: https://www.cnblogs.com/802syy/p/17412498.html