應(yīng)用場(chǎng)景:需要對(duì)集合內(nèi)的元素進(jìn)行從頭到尾的遍歷操作時(shí)可以考慮使用
不可變的List集合
不可變集合指的是一旦創(chuàng)建扣甲,就無(wú)法修改,只能進(jìn)行查詢(xún)齿椅,在Java中可以使用以下方式構(gòu)建
List<String> list = List.of("A", "B", "C");
// list.add("D"); 會(huì)報(bào)錯(cuò)
// list.remove("A"); 也會(huì)報(bào)錯(cuò)
Set<String> set = Set.of("A", "B", "C");
對(duì)于雙列集合Map
來(lái)說(shuō)琉挖,Map.of()
方法最多容納10組鍵值對(duì),有上限涣脚,但是list和set沒(méi)有
//自動(dòng)兩兩一組k-v
//1-A
//2-B
//3-C
Map<String, String> map = Map.of("1", "A", "2", "B", "3", "C");
// 補(bǔ)充兩種map集合遍歷的方法
// 第一種是用獲取keyset后遍歷
Set<String> keyset = map.KeySet();
for(String key:keyset){
System.out.println(map.get(key));
}
// 第二種是獲得entity集合后遍歷
// map中的每一個(gè)元素就是一個(gè)Entity對(duì)象
Set<Map.Entity<String, String>> entitySet = map.entitySet();
for(Map.Entity<String, String> entity:entitySet){
System.out.println(entity.getKey()+entity.getValue());
}
無(wú)限制的不可變map集合創(chuàng)建使用ofEntries()
示辈、copyOf()
HashMap<String, String> hm = new HashMap<>();
// 持續(xù)插入
hm.put("張三", "南京");
…
//copyOf()方法直接接收一個(gè)map對(duì)象作為參數(shù),生成一個(gè)不可變的map集合對(duì)象
Map<String, String> map = Map.copyOf(hm);
//ofEntities()方法接收一個(gè)`Map.Entity`類(lèi)型的數(shù)組作為參數(shù)
// 首先要把hm轉(zhuǎn)換為數(shù)組遣蚀,采用toArray()方法
Map.Entity<String,String> entities = hm.entitySet();
Map map = Map.ofEntities(entities.toArray(new Map.Entity[0]));
集合的
toArray()
和toArray(T[] a)
方法:
toArray()方法返回一個(gè)Object[]類(lèi)型的數(shù)組
toArray(T[] a)方法返回一個(gè)T[]類(lèi)型的數(shù)組:
如果傳入的數(shù)組a的長(zhǎng)度小于List中元素的個(gè)數(shù)顽耳,那么toArray(T[] a)方法會(huì)新建一個(gè)T[]類(lèi)型的數(shù)組,長(zhǎng)度為L(zhǎng)ist中元素的個(gè)數(shù)妙同,然后將List中的元素復(fù)制到新建的數(shù)組中射富。
如果傳入的數(shù)組a的長(zhǎng)度大于等于List中元素的個(gè)數(shù),那么toArray(T[] a)方法會(huì)將List中的元素復(fù)制到傳入的數(shù)組a中粥帚,然后返回傳入的數(shù)組a胰耗。
toArray()方法的實(shí)現(xiàn)就是調(diào)用toArray(new Object[0])方法。
流
結(jié)合lambda表達(dá)式芒涡,簡(jiǎn)化集合柴灯、數(shù)組整體的需要遍歷到底的操作。
- 先得到一條stream流水線(xiàn)费尽,并把數(shù)據(jù)放上去
- 然后調(diào)用中間方法
- 最后以終結(jié)方法結(jié)束
在這個(gè)過(guò)程中赠群,lambda表達(dá)式里面出現(xiàn)的變量指的就是集合中的元素
生成數(shù)據(jù)流的方法
- 單列集合可以直接使用
stream()
方法 - 雙列集合獲取entrySet
//map沒(méi)有valueSet!
Stream<String> stream = hm.values().stream();
Stream<String> keyStream = hm.keySet().stream();
Stream<Map.Entry<String, String>> entryStream = hm.entrySet().stream();
- 數(shù)組類(lèi)型使用
Arrays.stream(arr)
生成流
int[] arr = new int[5];
IntStream intStream = Arrays.stream(arr);
中間方法
流只能使用一次旱幼,所以要采用鏈?zhǔn)骄幊痰姆椒ú槊瑁词故前阎虚g流通過(guò)變量保存,使用完一次后再利用也會(huì)報(bào)錯(cuò)
Stream<Integer> stream1 = list.stream();
//這一步stream1已經(jīng)被使用
Stream<Integer> stream2 = stream1.filter(n -> n == 3);
stream2.forEach(System.out::println);
//下面語(yǔ)句報(bào)錯(cuò): Stream has already been linked or consumed
Stream<Integer> stream3 = stream1.filter(n -> n == 4);
刷算法題常用
mapToInt()
方法柏卤,它通常用于將一個(gè)對(duì)象流(Stream<T>
)轉(zhuǎn)換為一個(gè)原始整數(shù)流(IntStream
)
終結(jié)方法
收集到數(shù)組
mapToInt()
返回 IntStream
map()
返回 Stream
IntStream
可以使用sum()
min()
max()
average()
等方法
//toArray無(wú)參情況
Object[] array = list.stream().toArray();
//給toArray()參數(shù)
//這里因?yàn)橛玫氖欠盒投砸彩遣豢梢灾苯邮褂胣ew int[]的
//這里的value就是list的size
Integer[] array1 = list.stream().toArray(value -> new Integer[value]);
//想要變成int數(shù)組
int[] array2 = list.stream().mapToInt(Integer::intValue).toArray();
收集到集合List/Set
使用Collectors
工具類(lèi)中的toList()``toSet()
方法
Set<Integer> collect = list.stream().collect(Collectors.toSet());
收集到Map
使用Collectors
工具類(lèi)中的toMap()
方法
List<String> list2 = new ArrayList<>();
list2.add("hwh-23");
Map<String, Integer> collect1 = list2.stream().collect(Collectors.toMap(s -> s.split("-")[0],
s -> Integer.parseInt(s.split("-")[1])));