摘要:數(shù)組與數(shù)組通過方法數(shù)組數(shù)組與數(shù)組通過先轉之后引入數(shù)組通過與構造方法
1. 數(shù)組與List 1. List -> 數(shù)組
public static void main(String[] args) { List2. 數(shù)組 -> Listlist = new ArrayList<>(); for (int i = 0; i < 5; i++) { list.add("i=" + i); } //1. 通過toArray方法 String[] array = list.toArray(new String[0]); //2. stream String[] array2 = list.stream().toArray(String[]::new); System.out.println(Arrays.toString(array)); System.out.println(Arrays.toString(array2)); }
public static void main(String[] args) { String[] str =new String[] {"aaa","bbb","ccc","ffffd"}; List2. 數(shù)組與Set 1. 數(shù)組 -> Setlist2 = new ArrayList<>(); //1. Arrays.asList() List list = Arrays.asList(str); //2. Collections.addAll(list, arrays) Collections.addAll(list2, str); //3. stream List list3 = Stream.of(str).collect(Collectors.toList()); System.out.println(list.toString()); System.out.println(list2.toString()); System.out.println(list3.toString()); }
public static void main(String[] args) { String[] str =new String[] {"aaa","bbb","ccc","ffffd"}; //1. 通過先轉List之后引入Set Set2. Set -> 數(shù)組set = new HashSet<>(Arrays.asList(str)); //2. stream Set set2 = Stream.of(str).collect(Collectors.toSet()); System.out.println(set); System.out.println(set2); }
public static void main(String[] args) { Set3. List與Set 1. List -> Setset = new HashSet<>(); set.add("aaa"); set.add("bbb"); set.add("ccc"); set.add("ffffd"); //1. 通過 toArray() String[] array = set.toArray(new String[0]); //2. stream String[] array2 = set.stream().toArray(String[]::new); System.out.println(Arrays.toString(array)); System.out.println(Arrays.toString(array2)); }
public static void main(String[] args) {
Listlist = new ArrayList<>(); list.add("aaa"); list.add("bbb"); list.add("ccc"); list.add("ffffd"); Set set = new HashSet<>(list); Set set2 = list.stream().collect(Collectors.toSet()); System.out.println(set.toString()); System.out.println(set2.toString());
}
2. Set -> Listpublic static void main(String[] args) { Setset = new HashSet<>(); set.add("aaa"); set.add("bbb"); set.add("ccc"); set.add("ffffd"); //1. list構造方法 List list = new ArrayList<>(set); //2. stream List list2 = set.stream().collect(Collectors.toList()); System.out.println(list.toString()); System.out.println(list2.toString()); }
文章版權歸作者所有,未經(jīng)允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉載請注明本文地址:http://systransis.cn/yun/73617.html
摘要:與其他數(shù)據(jù)結構的互相轉換僅作為一個學習筆記供往后翻閱轉為數(shù)組轉為數(shù)組最方便的方法,就是使用擴展運算符。對象轉為轉為轉為要區(qū)分兩種情況。轉為轉為,正常情況下,所有鍵名都是字符串。這往往是數(shù)組轉為的逆操作。 Map 與其他數(shù)據(jù)結構的互相轉換 PS:僅作為一個學習筆記供往后翻閱! (1)Map 轉為數(shù)組Map 轉為數(shù)組最方便的方法,就是使用擴展運算符(...)。 const myMap = ...
摘要:學習筆記工作中常用到的語法只是簡單提及和,今天有空于是寫了這篇文章深入理解中的和數(shù)據(jù)結構,與其它數(shù)據(jù)結構的互相轉換。的提供了新的數(shù)據(jù)結構。本身是一個構造函數(shù),用來生成數(shù)據(jù)結構。 文中的內容主要是來自于阮一峰的《ES6標準入門》(第三版)?!秾W習ES6筆記──工作中常用到的ES6語法》只是簡單提及Set和Map,今天有空于是寫了這篇文章──《深入理解:ES6中的Set和Map數(shù)據(jù)結構,M...
摘要:和對比都是一個存儲多值的容器,兩者可以互相轉換,但是在使用場景上有區(qū)別??梢岳斫鉃榈姆椒蠈嵗龑傩岳^承自操作方法遍歷方法和數(shù)組的轉換要求雙成員數(shù)組值得注意的是的鍵是跟內存綁定的參考文檔和和阮一峰教程 Array和Set對比 都是一個存儲多值的容器,兩者可以互相轉換,但是在使用場景上有區(qū)別。如下: Array的indexOf方法比Set的has方法效率低下 Set不含有重復值(可以利...
摘要:和對比都是一個存儲多值的容器,兩者可以互相轉換,但是在使用場景上有區(qū)別??梢岳斫鉃榈姆椒蠈嵗龑傩岳^承自操作方法遍歷方法和數(shù)組的轉換要求雙成員數(shù)組值得注意的是的鍵是跟內存綁定的參考文檔和和阮一峰教程 Array和Set對比 都是一個存儲多值的容器,兩者可以互相轉換,但是在使用場景上有區(qū)別。如下: Array的indexOf方法比Set的has方法效率低下 Set不含有重復值(可以利...
摘要:和對比都是一個存儲多值的容器,兩者可以互相轉換,但是在使用場景上有區(qū)別??梢岳斫鉃榈姆椒蠈嵗龑傩岳^承自操作方法遍歷方法和數(shù)組的轉換要求雙成員數(shù)組值得注意的是的鍵是跟內存綁定的參考文檔和和阮一峰教程 Array和Set對比 都是一個存儲多值的容器,兩者可以互相轉換,但是在使用場景上有區(qū)別。如下: Array的indexOf方法比Set的has方法效率低下 Set不含有重復值(可以利...
閱讀 5160·2023-04-25 19:30
閱讀 2187·2023-04-25 15:09
閱讀 2634·2021-11-16 11:45
閱讀 2193·2021-11-15 18:07
閱讀 1473·2021-11-11 17:22
閱讀 2133·2021-11-04 16:06
閱讀 3589·2021-10-20 13:47
閱讀 3051·2021-09-22 16:03