Java集合,java集合類詳解
Collection
Collection是一個介面,允許集合中有重複元素,也能禁止元素重複;既可以強制排序也可以不限制順序,下面看看Collection中一些常用的基本方法
1 Collection<String> hashSet = new HashSet<>(); 2 hashSet.add("hello Collection");// 添加元素 3 Collection<String> arrays=Arrays.asList("a","b","c"); 4 hashSet.addAll(arrays);//添加一個集合 5 System.out.println(hashSet.size());//擷取集合元素個數 6 System.out.println(hashSet.contains("b"));//集合是否包含某個元素 7 System.out.println(hashSet.containsAll(arrays));//集合是否包含某組元素 8 Object[] elments=hashSet.toArray();//集合轉數組 9 String[] strings=hashSet.toArray(new String[0]);//指定元素轉換10 hashSet.remove("a");//刪除單個元素11 hashSet.removeAll(arrays);//刪除一組元素 Set
Set不能有兩個引用指向同一個對象,或者指向null,即使是a.equals(b)=true也不行,因為它是無重複對象組成的集合;如果在Set集合中添加已有的元素,並不會拋出異常,而是返回一個Boolean值false
繼承之Set的SortedSet介面進一步擴充了集合的方法
1 SortedSet<String> treeSet = new TreeSet<>(); 2 treeSet.add("a"); 3 treeSet.add("b"); 4 treeSet.add("c"); 5 System.out.println(treeSet.first());//擷取集合中的第一個元素 6 System.out.println(treeSet.last());//擷取集合中的最後一個元素 7 //返回除了第一個元素之外的所有元素 8 SortedSet<String> tail= treeSet.tailSet(treeSet.first()+'\0'); 9 System.out.println(tail);10 //返回除了最後一個元素之外的所有元素11 SortedSet<String> head=treeSet.headSet(treeSet.last());12 System.out.println(head);13 //截取第N個元素到M個元素之間的所有元素14 SortedSet<String> middle=treeSet.subSet(treeSet.first()+'\0', treeSet.last());15 System.out.println(middle); List
和集(Set)不同,List是有序的對象集合,並且可以包含重複元素,列表的大小可以隨著元素的數量變化,不像數組一樣固定,List也繼承了Collection的方法,同時也擴充了一些方法
1 java.util.List<String> list=Arrays.asList("Hello","Java"); 2 java.util.List<String> arrayList=new ArrayList<>(list); 3 System.out.println(arrayList.get(0));//通過索引擷取集合元素 4 arrayList.set(0, "Hi");//通過索引設定集合的元素 5 System.out.println(arrayList.get(0)); 6 arrayList.add(1, "Bob"); 7 System.out.println(arrayList);//通過索引插入元素 8 System.out.println(arrayList.indexOf("Bob"));//查詢某個元素的位置 9 System.out.println(arrayList.lastIndexOf("Java"));//反向查詢某個元素的位置10 arrayList.retainAll(list);//刪除與list不相同的元素11 System.out.println(arrayList);12 arrayList.clear();//清除所有元素13 System.out.println(arrayList); Map
Map是一系列索引值對,一個鍵對應一個值,可以把Map看作是Set加Collection組合的集合,Map的鍵就像Set一樣,不允許有重複元素,同樣來看下Map常用 的一些方法
1 Map<Integer , String> map=new HashMap<>(); 2 String[] words={"a","b","cc"}; 3 for (int i = 0; i < words.length; i++) { 4 map.put(i, words[i]);//通過put方法填充Map 5 } 6 System.out.println(map); 7 System.out.println(map.get(1));//擷取索引值為1對應的值 8 System.out.println(map.containsKey(5));//是否包含某個鍵 9 System.out.println(map.containsValue("b"));//是否包含某個值10 //把映射關係的鍵和值分別作集合處理11 Set<Integer> keys=map.keySet();12 System.out.println(keys);13 Collection<String> values=map.values();14 System.out.println(values);15 for (Map.Entry<Integer, String> elment: map.entrySet()) {16 System.out.println(elment);17 }18 map.remove(1);//刪除索引值對19 System.out.println(map);集合中常用的一些技巧
通過Collections類的靜態方法,可以對集合進行一些操作
1 java.util.List<Integer> numbers=Arrays.asList(12,5,6,8,11,4); 2 Collections.sort(numbers);//排序 3 System.out.println(numbers); 4 Collections.reverse(numbers);//反轉 5 System.out.println(numbers); 6 Collections.shuffle(numbers);//打亂順序 7 System.out.println(numbers); 8 //最大值、最小值 9 Collections.max(numbers);10 Collections.min(numbers);
防止並發訪問集合
1 java.util.List<String> list=Collections.synchronizedList(new ArrayList<String>());2 Map<Integer, String> map=Collections.synchronizedMap(new HashMap<Integer,String>());
唯讀集合
java.util.List<String> words=Collections.unmodifiableList(new ArrayList<String>());