最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java中Map、Set、List的簡(jiǎn)單使用教程(快速入門(mén))

 更新時(shí)間:2021年01月14日 11:42:58   作者:黑峯  
這篇文章主要給大家介紹了關(guān)于java中Map、Set、List簡(jiǎn)單使用教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

Map、Set、List

List的常用方法

1、創(chuàng)建

List<Integer> list = new ArrayList<>(); 
List<Integer> list = new LinkedList<>(); //同時(shí)可以作為鏈表用
List<List<Integer>> list = new ArrayList<>();

2、遍歷

//本質(zhì)上其實(shí)是調(diào)用Iterator
for(String s:list){
	System.out.print(s);
}

3、List轉(zhuǎn)化為數(shù)組

//兩種寫(xiě)法都可以, 第一種微微規(guī)范一點(diǎn) , 但是只能是封裝類(lèi)型的數(shù)組
Integer array[] = list.toArray(new Integer[list.size()]);
Integer array[] = list.toArray();
//java8的寫(xiě)法, 可以借助stream來(lái)實(shí)現(xiàn)直接轉(zhuǎn)化為 基本類(lèi)型數(shù)組
int[] result = list.stream().mapToInt(i->i).toArray();

4、數(shù)組轉(zhuǎn)化為L(zhǎng)ist

//array[]是不是封裝類(lèi)都無(wú)所謂, 不是封裝類(lèi)型 系統(tǒng)會(huì)自動(dòng)轉(zhuǎn)換為封裝類(lèi)型
Integer array[] = {1,2,3};
intarray[] = {1,2,3};
List<Integer> list = new ArrayList<>(Arrays.asList(array));

//由此有直接默認(rèn)給List賦值的簡(jiǎn)便寫(xiě)法
List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3));

5、合并兩個(gè)List

List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
list.addAll(list); //list2的元素全部加入list1

Set的常用方法

1、創(chuàng)建Set

Set<String> set = new HashSet<>();
Set<String> set = new TreeSet<>();

2、遍歷

for(Integer t:set){
 System.out.print(t);
}

3、數(shù)組轉(zhuǎn)Set

String[] strs = new String[]{"Tom", "Bob", "Jane"};
Set<String> set = new HashSet<>(Arrays.asList(strs));

3、Set轉(zhuǎn)數(shù)組

String[] strs = new String[]{"Tom", "Bob", "Jane"};
Set<String> set = new HashSet<>(Arrays.asList(strs));
//同樣的也是封裝類(lèi)型不能直接轉(zhuǎn)為 基本類(lèi)型
Object[] result = set.toArray();

Map的常見(jiàn)用法

Map最大的特點(diǎn)就是以 key-value的方式才存儲(chǔ)數(shù)據(jù)

創(chuàng)建Map集合

Map<String,Integer> map = new HashMap<>(); //無(wú)序
Map<String,Integer> map = new TreeMap<>(); //按key有序, 并可以實(shí)現(xiàn)自定義排序

遍歷Map集合

//通過(guò)索引遍歷, 效率不高
for(String key:map.keySet()){
	Integer temp = map.get(key);
}

//通過(guò)entry遍歷 (推薦使用)
for(Entry<String,Integer> entry: map.entrySet()){
 String s = entry.getKey();
 Integer a = entry.getValue();
}
//獲取值
map.get(object);
map.getOrDefault(tmp, 0); //如果為空則設(shè)為0

注意事項(xiàng)

1、當(dāng)調(diào)用List的contains(Object o)方法時(shí)即使是相同內(nèi)容的對(duì)象,但不是相同對(duì)象會(huì)被判斷成不相同。需要手動(dòng)在對(duì)象中編寫(xiě)equals方法,用Object.equals(a,b);

其余的常見(jiàn)方法均在圖片中給出。

總結(jié)

到此這篇關(guān)于java中Map、Set、List的簡(jiǎn)單使用教程的文章就介紹到這了,更多相關(guān)java中Map、Set、List使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

罗平县| 南投县| 遵化市| 炉霍县| 镇安县| 江达县| 绿春县| 黑水县| 开平市| 遂昌县| 东光县| 平南县| 如皋市| 白河县| 浙江省| 游戏| 荥经县| 彭泽县| 桦甸市| 玛曲县| 祁东县| 灵寿县| 兴文县| 丰镇市| 华容县| 溧水县| 翁源县| 江华| 岳阳市| 梨树县| 博罗县| 板桥市| 义乌市| 定陶县| 林芝县| 大名县| 浙江省| 资兴市| 海丰县| 华宁县| 从江县|