Java集合學習

來源:互聯網
上載者:User

標籤:create   arm   pre   擷取   string   遍曆   方式   system   jdk   

一、集合的分類:

 

二、常用的集合:

1、Java collection:Jdk中的集合

1、List

//ListList<String>  list = new ArrayList<>();list.add("a");list.add("b");list.add("c");System.out.println(list); // [a, b, c]

 

2、Map

//MapMap<String,String>  map = new HashMap<>();map.put("name","by");map.put("age","18");System.out.println(map); // {name=by, age=18}

 

3、Set

//SetSet<String> set = new HashSet<>();set.add("a");set.add("b");System.out.println(set); // [a, b]

 

4、Iterator遍曆集合

//Iterator遍曆List集合Iterator iterator = list.iterator();while (iterator.hasNext()){    String parm = (String) iterator.next();    System.out.println(parm);    if(parm.equals("a")){        iterator.remove();    }}System.out.println(list); // [b, c]

 

5、遍曆MAP

/**     * 遍曆Map     *     * 擷取方法:     * 第一種方式: 使用keySet     * 需要分別擷取key和value,沒有物件導向的思想     * Set<K> keySet() 返回所有的key對象的Set集合     */   static void traverseMap(){       Map<Integer, String> map = new HashMap<>();       map.put(1, "aaaa");       map.put(2, "bbbb");       map.put(3, "cccc");       System.out.println(map);       Set<Integer> ks = map.keySet();       Iterator<Integer> it = ks.iterator();       while (it.hasNext()) {           Integer key = it.next();           String value = map.get(key);           System.out.println("key=" + key + " value=" + value);       }   }

 

2、Guava Collections(google開源工具 )

1、List

//建立ListList<String> list = Lists.newArrayList("a","b","c");list.add("d");//反轉ListSystem.out.println(Lists.reverse(list)); // [d, c, b, a]//將List集合轉換為特定規則的字串String listResult = Joiner.on("-").join(list);System.out.println(listResult); // a-b-c-d

 

2、Map

//定義MapMap<String,String > map = Maps.newHashMap();map.put("name","by");map.put("age","23");System.out.println(map); //{name=by, age=23}//將Map集合轉換為特定規則的字串String mapResult = Joiner.on(",").withKeyValueSeparator("=").join(map);System.out.println(mapResult); // name=by,age=23//定義Map中放List的形式(Map<String,List<Integer>>)Multimap<String,Integer> maps = ArrayListMultimap.create();maps.put("map",1);maps.put("map",2);System.out.println(maps); //{map=[1, 2]}

 

3、Set

//定義SetSet<String> set = Sets.newHashSet();set.add("value");

 

3、Trove

1、構造基本類型的集合

//直接構造int類型的集合TIntArrayList intList = new TIntArrayList();intList.add(1);intList.add(2);intList.add(3);intList.reverse();System.out.println(intList);

 

Java集合學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.