標籤: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集合學習