成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

java---List,Set,數(shù)組的互相轉換

chinafgj / 1774人閱讀

摘要:數(shù)組與數(shù)組通過方法數(shù)組數(shù)組與數(shù)組通過先轉之后引入數(shù)組通過與構造方法

1. 數(shù)組與List 1. List -> 數(shù)組
public static void main(String[] args) {
    List list = 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));
}
2. 數(shù)組 -> List
public static void main(String[] args) {
    String[] str =new String[] {"aaa","bbb","ccc","ffffd"};
    List list2 = 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());
}
2. 數(shù)組與Set 1. 數(shù)組 -> Set
public static void main(String[] args) {
    String[] str =new String[] {"aaa","bbb","ccc","ffffd"};
    
    //1. 通過先轉List之后引入Set
    Set set = new HashSet<>(Arrays.asList(str));
    
    //2. stream
    Set set2 = Stream.of(str).collect(Collectors.toSet());
    
    System.out.println(set);
    System.out.println(set2);
}
2. Set -> 數(shù)組
public static void main(String[] args) {
    Set set = 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));
}
3. List與Set 1. List -> Set

public static void main(String[] args) {

List list = 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 -> List
public static void main(String[] args) {
    Set set = 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

相關文章

  • JS筆記!Map 與其他數(shù)據(jù)結構互相轉換

    摘要:與其他數(shù)據(jù)結構的互相轉換僅作為一個學習筆記供往后翻閱轉為數(shù)組轉為數(shù)組最方便的方法,就是使用擴展運算符。對象轉為轉為轉為要區(qū)分兩種情況。轉為轉為,正常情況下,所有鍵名都是字符串。這往往是數(shù)組轉為的逆操作。 Map 與其他數(shù)據(jù)結構的互相轉換 PS:僅作為一個學習筆記供往后翻閱! (1)Map 轉為數(shù)組Map 轉為數(shù)組最方便的方法,就是使用擴展運算符(...)。 const myMap = ...

    Jenny_Tong 評論0 收藏0
  • 深入理解:ES6中Set和Map數(shù)據(jù)結構,Map與其它數(shù)據(jù)結構互相轉換

    摘要:學習筆記工作中常用到的語法只是簡單提及和,今天有空于是寫了這篇文章深入理解中的和數(shù)據(jù)結構,與其它數(shù)據(jù)結構的互相轉換。的提供了新的數(shù)據(jù)結構。本身是一個構造函數(shù),用來生成數(shù)據(jù)結構。 文中的內容主要是來自于阮一峰的《ES6標準入門》(第三版)?!秾W習ES6筆記──工作中常用到的ES6語法》只是簡單提及Set和Map,今天有空于是寫了這篇文章──《深入理解:ES6中的Set和Map數(shù)據(jù)結構,M...

    Cristalven 評論0 收藏0
  • Array、Set、Map、Object學習總結

    摘要:和對比都是一個存儲多值的容器,兩者可以互相轉換,但是在使用場景上有區(qū)別??梢岳斫鉃榈姆椒蠈嵗龑傩岳^承自操作方法遍歷方法和數(shù)組的轉換要求雙成員數(shù)組值得注意的是的鍵是跟內存綁定的參考文檔和和阮一峰教程 Array和Set對比 都是一個存儲多值的容器,兩者可以互相轉換,但是在使用場景上有區(qū)別。如下: Array的indexOf方法比Set的has方法效率低下 Set不含有重復值(可以利...

    Miracle_lihb 評論0 收藏0
  • Array、Set、Map、Object學習總結

    摘要:和對比都是一個存儲多值的容器,兩者可以互相轉換,但是在使用場景上有區(qū)別??梢岳斫鉃榈姆椒蠈嵗龑傩岳^承自操作方法遍歷方法和數(shù)組的轉換要求雙成員數(shù)組值得注意的是的鍵是跟內存綁定的參考文檔和和阮一峰教程 Array和Set對比 都是一個存儲多值的容器,兩者可以互相轉換,但是在使用場景上有區(qū)別。如下: Array的indexOf方法比Set的has方法效率低下 Set不含有重復值(可以利...

    MiracleWong 評論0 收藏0
  • Array、Set、Map、Object學習總結

    摘要:和對比都是一個存儲多值的容器,兩者可以互相轉換,但是在使用場景上有區(qū)別??梢岳斫鉃榈姆椒蠈嵗龑傩岳^承自操作方法遍歷方法和數(shù)組的轉換要求雙成員數(shù)組值得注意的是的鍵是跟內存綁定的參考文檔和和阮一峰教程 Array和Set對比 都是一個存儲多值的容器,兩者可以互相轉換,但是在使用場景上有區(qū)別。如下: Array的indexOf方法比Set的has方法效率低下 Set不含有重復值(可以利...

    nicercode 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<