1、创建不可变的集合
// 在 Java 8 中,没有 List.of() 方法。在 Java 9 中,引入了 List.of() 方法,用于创建一个不可变的列表。
List<String> list = List.of("apple", "banana", "orange");
Set<String> set = Set.of("apple", "banana", "orange");
Map<String,String> map = Map.of("6501","乌鲁木齐","6543","阿勒泰");
2、常见遍历方法
// list 遍历
List<String> list = new ArrayList<>();
Collections.addAll(list,"张三丰","张无忌","周芷若","赵敏","张强");
// 1、增强for
for (String s : list) {
System.out.println(s);
}
// 2、for循环
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
// 3、迭代器
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
// 4、forEach
list.forEach(item->{
System.out.println(item);
});
// Map 遍历
Map<String,String> map = new HashMap<>();
map.put("0001","zhangsan");
map.put("0002","wanger");
// 1、keyset
Set<String> keys = map.keySet();
for (String key : keys) {
System.out.println(key);
}
// 2、entrySet
Set<Map.Entry<String, String>> entries = map.entrySet();
for (Map.Entry<String, String> entry : entries) {
System.out.println(entry);
}
3、获取流
1、单列集合--------------------Collection中的默认方法
2、双列集合--------------------无法直接使用stream流
3、数组--------------------------Arrys工具类中的静态方法
4、一堆零散数据--------------Stream流中的静态方法
// 1、list 获取流
List<String> list = new ArrayList<>();
Collections.addAll(list,"张三丰","张三","张无忌","周芷若","赵敏","张强");
list.stream().forEach(name-> System.out.println(name));
// 2、map获取流
Map<Integer,String> map = new HashMap<>();
map.entrySet().stream().forEach(item-> System.out.println(item));
// 3、数组获取流
int [] arr = {1,2,3,4,5,6,7,8,9};
Arrays.stream(arr).forEach(item->System.out.println(item));
// 4、一堆零散数据获取流
Stream.of(arr).forEach(item->System.out.println(item));
4、Stream流的中间方法
1、filter--------------------------过滤
2、limit--------------------------获取前几个元素
3、skip--------------------------跳过前几个元素
4、distinct----------------------去重(依赖 hashCode 和 equals 方法)
5、concat-----------------------合并两个流为一个流
5、map--------------------------转换流中的数据类型
// 1、过滤
List<String> list = new ArrayList<>();
Collections.addAll(list,"张三丰","张三","张无忌","周芷若","赵敏","张强");
// list 中所张开头的并且长度为三个字的挑出来
list.stream().filter(name-> name.startsWith("张") && name.length()==3)
.forEach(name-> System.out.println(name));
// 2、获取前三个元素
list.stream().limit(3).forEach(item-> System.out.println(item));
// 3、跳过前三个元素
list.stream().skip(3).forEach(item-> System.out.println(item));
// 4、去重
list.stream().distinct().forEach(item-> System.out.println(item));
// 5、合并流
Stream.concat(list1.stream(),list2.stream()).forEach(item-> System.out.println(item));
// 6、转换流中的数据类型类型
List<String> list = new ArrayList<>();
Collections.addAll(list,"张三丰-15","张三-17","张无忌-12","周芷若-12","赵敏-18","张强-16");
// 获取人员的年龄
list.stream().map(s-> Integer.parseInt(s.split("-")[1])).forEach(item-> System.out.println(item));
5、Stream流的终结方法
1、forEach--------------------------遍历
2、count-----------------------------统计
3、toArray---------------------------流中的数据放到数组里
4、collect----------------------------流中的数据放到集合里
// 1、遍历
List<String> list = new ArrayList<>();
Collections.addAll(list,"张三丰","张三","张无忌","周芷若","赵敏","张强");
list.stream().forEach(item->System.out.println(item));
// 2、统计
List<String> list = new ArrayList<>();
Collections.addAll(list,"张三丰","张三","张无忌","周芷若","赵敏","张强");
list.stream().count();
// 3、toArray
List<String> list = new ArrayList<>();
Collections.addAll(list,"张三丰","张三","张无忌","周芷若","赵敏","张强");
Object[] objects = list.stream().toArray();
String[] strings = list.stream().toArray(value -> new String[value]);
// 4、collect
List<String> list = new ArrayList<>();
Collections.addAll(list,"张三丰-男-30","张三-女-20","张无忌-男-28","周芷若-男-29","赵敏-女-26","张强-男-27");
// 所有男性收集到一个list当中
List<String> strings = list.stream().filter(v -> "男".equals(v.split("-")[1]))
.collect(Collectors.toList());
// 所有男生收集到一个map 键:姓名 值:年龄
Map<String, String> map = list.stream().filter(v -> "男".equals(v.split("-")[1]))
.collect(Collectors.toMap(k -> k.split("-")[0], v -> v.split("-")[1]));
6、练习
1、过滤list中字段值为指定字符的
List<PoiData> data = list.stream()
.filter(poiData -> poiData.getCityname().equals("乌鲁木齐市"))
.collect(Collectors.toList());
2、根据某个字段分组及统计数量
Map<String, Long> collect = list.stream()
.collect(Collectors.groupingBy(poiData -> poiData.getAdcode(), Collectors.counting()));
网友评论