取值方面
根据集合即可获取
先获取一个集合
List<Student> stu =Arrays.asList(//数组转集合
new Student("qwe", 10, "男"),
new Student("qwe1", 13, "男"),
new Student("qwe2", 12, "女1"),
new Student("qwe3", 11, "男"),
new Student("qwe4", 16, "女"),
new Student("qwe5", 20, "男")
);
- 方法一
stu.stream()//流化
//控制台输出
Student{name='qwe', age=10, sex='男'}
Student{name='qwe1', age=13, sex='男'}
Student{name='qwe2', age=12, sex='女1'}
Student{name='qwe3', age=11, sex='男'}
Student{name='qwe4', age=16, sex='女'}
Student{name='qwe5', age=20, sex='男'}
- 方法二
stu.stream().limit(2) //遍历集合获取前两个
//控制台输出
Student{name='qwe', age=10, sex='男'}
Student{name='qwe1', age=13, sex='男'}
- 方法三(加上Lambda表达式filter())
stus.stream().filter((stu)->stu.getAge()>11).limit(2).forEach(System.out::println);//获取年龄大于11,前两数据
//控制台输出
Student{name='qwe1', age=13, sex='男'}
Student{name='qwe2', age=12, sex='女1'}
- 方法四
stus.stream().map(Student::getName).limit(2).forEach(System.out::println);//只获取名字
//控制台输出
qwe
qwe1
- 方法五
//输出
forEach()//不按照顺序输出
//控制台输出
Student{name='qwe5', age=20, sex='男'}
Student{name='qwe1', age=13, sex='男'}
Student{name='qwe2', age=12, sex='女1'}
Student{name='qwe', age=10, sex='男'}
Student{name='qwe4', age=16, sex='女'}
Student{name='qwe3', age=11, sex='男'}
-------------
forEachOrdered()//按照顺序输出
//控制台输出
Student{name='qwe', age=10, sex='男'}
Student{name='qwe1', age=13, sex='男'}
Student{name='qwe2', age=12, sex='女1'}
Student{name='qwe3', age=11, sex='男'}
Student{name='qwe4', age=16, sex='女'}
Student{name='qwe5', age=20, sex='男'}
- 方法六
stus.count()//数量
- 方法七(求根据某一列集合中最大的
System.out.println(stus.stream().max(Comparator.comparingInt(Student::getAge)));
//输出
Optional[Student{name='qwe5', age=20, sex='男'}]
- 方法八(分组,例子根据性别)
stus.stream().collect(Collectors.groupingBy(student -> student.getSex()));
//输出结果
{女=[Student{name='qwe4', age=16, sex='女'}], 女1=[Student{name='qwe2', age=12, sex='女1'}], 男=[Student{name='qwe', age=10, sex='男'}, Student{name='qwe1', age=13, sex='男'}, Student{name='qwe3', age=11, sex='男'}, Student{name='qwe5', age=20, sex='男'}]}
- 方法九(集合转换)
- 注意(map集合需要定义key值所以在toMap() 时括号加上 (数组::get某个字段,再加上条件 对象->对象)
LIst和Set不用
- 注意(map集合需要定义key值所以在toMap() 时括号加上 (数组::get某个字段,再加上条件 对象->对象)
System.out.println("map"+
stus.stream().collect(Collectors.toMap(Student ::getName,stu->stu))+"/n"
);
System.out.println("list"+
stus.stream().collect(Collectors.toList())+"/n"
);
System.out.println("set"+
stus.stream().collect(Collectors.toSet())+"/n"
);
//输出
map{qwe4=Student{name='qwe4', age=16, sex='女'}, qwe3=Student{name='qwe3', age=11, sex='男'}, qwe5=Student{name='qwe5', age=20, sex='男'}, qwe2=Student{name='qwe2', age=12, sex='女'}, qwe1=Student{name='qwe1', age=13, sex='男'}, qwe=Student{name='qwe', age=10, sex='男'}}/n
list[Student{name='qwe', age=10, sex='男'}, Student{name='qwe1', age=13, sex='男'}, Student{name='qwe2', age=12, sex='女'}, Student{name='qwe3', age=11, sex='男'}, Student{name='qwe4', age=16, sex='女'}, Student{name='qwe5', age=20, sex='男'}]/n
set[Student{name='qwe4', age=16, sex='女'}, Student{name='qwe1', age=13, sex='男'}, Student{name='qwe', age=10, sex='男'}, Student{name='qwe2', age=12, sex='女'}, Student{name='qwe5', age=20, sex='男'}, Student{name='qwe3', age=11, sex='男'}]/n
筛选(只要我们选择的数据)
收集 分组后收集成一个map集合写法
分组后收集成一个map集合写法(LinkedHashMap)
LinkedHashMap<Object, List<Map<String, Object>>> confirmationTime = list.stream().collect(Collectors.groupingBy(m -> m.get("confirmationTime"), LinkedHashMap::new, Collectors.toList()));
分组后收集成一个map集合写法(HashMap)
HashMap<Object, List<Map<String, Object>>> confirmationTime = list.stream().collect(Collectors.groupingBy(m -> m.get("confirmationTime"),HashMap::new, Collectors.toList()));
先通过某个字段分组,转换成指定map集合,收集toList集合
不使用map对象就可以实现将生成一个map的对象
方法:
ImmutableMap.of("text",键值,"value",值)
创建一个map集合
List<Map<String, String>> matNameMap = new ArrayList<>();
ImmutableMap.of(“key”,“value”)
通过:方法ImmutableMap.of
matNameMap.add(ImmutableMap.of("key","key1","value","value1"));
matNameMap.add(ImmutableMap.of("key","key2","value","value2"));
matNameMap.add(ImmutableMap.of("key","key3","value","value3"));
结果
[{key:key1,value:value1},
{key:key2,value:value2},
{key:key3,value:value}]
总结
通过方法ImmutableMap.of可以实现,不用new map()数组,实现获得一个map集合