1的烁、創(chuàng)建不可變的集合
// 在 Java 8 中绎橘,沒(méi)有 List.of() 方法。在 Java 9 中骑脱,引入了 List.of() 方法菜枷,用于創(chuàng)建一個(gè)不可變的列表。
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叁丧、常見(jiàn)遍歷方法
// list 遍歷
List<String> list = new ArrayList<>();
Collections.addAll(list,"張三豐","張無(wú)忌","周芷若","趙敏","張強(qiáng)");
// 1啤誊、增強(qiáng)for
for (String s : list) {
System.out.println(s);
}
// 2、for循環(huán)
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中的默認(rèn)方法
2孟抗、雙列集合--------------------無(wú)法直接使用stream流
3、數(shù)組--------------------------Arrys工具類中的靜態(tài)方法
4钻心、一堆零散數(shù)據(jù)--------------Stream流中的靜態(tài)方法
// 1凄硼、list 獲取流
List<String> list = new ArrayList<>();
Collections.addAll(list,"張三豐","張三","張無(wú)忌","周芷若","趙敏","張強(qiáng)");
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捷沸、數(shù)組獲取流
int [] arr = {1,2,3,4,5,6,7,8,9};
Arrays.stream(arr).forEach(item->System.out.println(item));
// 4摊沉、一堆零散數(shù)據(jù)獲取流
Stream.of(arr).forEach(item->System.out.println(item));
4、Stream流的中間方法
1痒给、filter--------------------------過(guò)濾
2说墨、limit--------------------------獲取前幾個(gè)元素
3、skip--------------------------跳過(guò)前幾個(gè)元素
4苍柏、distinct----------------------去重(依賴 hashCode 和 equals 方法)
5尼斧、concat-----------------------合并兩個(gè)流為一個(gè)流
5、map--------------------------轉(zhuǎn)換流中的數(shù)據(jù)類型
// 1试吁、過(guò)濾
List<String> list = new ArrayList<>();
Collections.addAll(list,"張三豐","張三","張無(wú)忌","周芷若","趙敏","張強(qiáng)");
// list 中所張開(kāi)頭的并且長(zhǎng)度為三個(gè)字的挑出來(lái)
list.stream().filter(name-> name.startsWith("張") && name.length()==3)
.forEach(name-> System.out.println(name));
// 2棺棵、獲取前三個(gè)元素
list.stream().limit(3).forEach(item-> System.out.println(item));
// 3楼咳、跳過(guò)前三個(gè)元素
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母怜、轉(zhuǎn)換流中的數(shù)據(jù)類型類型
List<String> list = new ArrayList<>();
Collections.addAll(list,"張三豐-15","張三-17","張無(wú)忌-12","周芷若-12","趙敏-18","張強(qiáng)-16");
// 獲取人員的年齡
list.stream().map(s-> Integer.parseInt(s.split("-")[1])).forEach(item-> System.out.println(item));
5、Stream流的終結(jié)方法
1缚柏、forEach--------------------------遍歷
2苹熏、count-----------------------------統(tǒng)計(jì)
3、toArray---------------------------流中的數(shù)據(jù)放到數(shù)組里
4币喧、collect----------------------------流中的數(shù)據(jù)放到集合里
// 1柜裸、遍歷
List<String> list = new ArrayList<>();
Collections.addAll(list,"張三豐","張三","張無(wú)忌","周芷若","趙敏","張強(qiáng)");
list.stream().forEach(item->System.out.println(item));
// 2、統(tǒng)計(jì)
List<String> list = new ArrayList<>();
Collections.addAll(list,"張三豐","張三","張無(wú)忌","周芷若","趙敏","張強(qiáng)");
list.stream().count();
// 3粱锐、toArray
List<String> list = new ArrayList<>();
Collections.addAll(list,"張三豐","張三","張無(wú)忌","周芷若","趙敏","張強(qiáng)");
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","張無(wú)忌-男-28","周芷若-男-29","趙敏-女-26","張強(qiáng)-男-27");
// 所有男性收集到一個(gè)list當(dāng)中
List<String> strings = list.stream().filter(v -> "男".equals(v.split("-")[1]))
.collect(Collectors.toList());
// 所有男生收集到一個(gè)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、練習(xí)
1怜浅、過(guò)濾list中字段值為指定字符的
List<PoiData> data = list.stream()
.filter(poiData -> poiData.getCityname().equals("烏魯木齊市"))
.collect(Collectors.toList());
2铐然、根據(jù)某個(gè)字段分組及統(tǒng)計(jì)數(shù)量
Map<String, Long> collect = list.stream()
.collect(Collectors.groupingBy(poiData -> poiData.getAdcode(), Collectors.counting()));