JavaSE-集合
集合類提供一種存儲空間可變的存儲模型,存儲數據的容量可以隨時發生改變,比如ArrayList
。
集合按照存儲的結構可以分爲兩類,分別是單列集合java.util.Collection
和雙列集合java.util.Map
,這裏就先來講講collection
集合。
List
的特點是存儲的元素有序,元素可以重複。而set
的特點是元素無序、存儲的元素不可重複。List
的接口主要實現類有ArrayList
和LinkedList
,Set
接口的主要實現類有HashSet
和TreeSet
;Map
集合主要有HashMap
Collection
* `public boolean add(E e)`: 把給定的對象添加到當前集合中 。
* `public void clear()` :清空集合中所有的元素。
* `public boolean remove(E e)`: 把給定的對象在當前集合中刪除。
* `public boolean contains(E e)`: 判斷當前集合中是否包含給定的對象。
* `public boolean isEmpty()`: 判斷當前集合是否爲空。
* `public int size()`: 返回集合中元素的個數。
* `public Object[] toArray()`: 把集合中的元素,存儲到數組中。
簡單使用下這個幾個方法
public static void main(String[] args) {
Collection<String> c = new ArrayList<String>();
c.add("zh1z3ven");
c.add("agou");
c.add("cafei");
c.add("xiaoming");
System.out.println(c);
Boolean a = c.isEmpty();
System.out.println(a);
int a2 = c.size();
System.out.println(a2);
c.remove(2);
System.out.println(c);
Boolean a3 = c.contains("cafei");
System.out.println(a3);
}
Iterator迭代器
java給我們提供了Iterator
接口,也就是迭代器,該接口也是集合的一種。也是集合專用的一種遍歷方式。
Iterator<String> it = col.iterator();
集合的iterator()
方法返回該集合元素的一個迭代器對象,之後就可以通過此對象的hashNext()
和next()
遍歷這個集合。
next()
:返回迭代的下一個元素。
hasNext()
:如果仍有元素可以迭代,則返回 true,否則返回false。
public static void main(String[] args) {
Collection<String> col = new ArrayList<String>();
col.add("Hello");
col.add("World");
col.add("Java");
System.out.println(col);
Iterator<String> it = col.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
}
List
List
接口是Collection
的子接口,
List
接口裏面允許出現重複元素得,在程序中可以通過索引來訪問。
List
接口存儲的數據是有序的。
List
的特有方法
void add(int index, Element) 在指定位置插入指定的元素
E remove(int index) 刪除指定位置處的元素,返回被刪除的元素
E set(int index, Element) 修改指定位置處的元素,返回被修改的元素
E get(int index) 返回指定位置處的元素
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Hello");
list.add("World");
list.add("Java");
System.out.println(list);
list.add(1,"agou");
System.out.println(list);
list.remove(1);
System.out.println(list);
}
//
[Hello, World, Java]
[Hello, agou, World, Java]
[Hello, World, Java]
LinkedList
LinkedList集合特有方法
public void addFirst(E e) 在該列表開頭插入指定的元素
public void addLast(E e) 將指定元素追加到此列表末尾
public E getFirst() 返回此列表第一個元素
public E getLast() 返回次列表末尾元素
public E removeFirst() 刪除並返回此列表第一個元素
public E removeLast() 刪除並返回此列表最後一個元素
set
set接口也是繼承了Collection接口,也是單列集合。
set接口中元素無序,並且都會以某種規則保證存入的元素不出現重複。
HashSet
無序,不保證元素的迭代順序
不包含重複元素的集合
HashSet<String> hashSet = new HashSet<>();
簡單例子
public static void main(String[] args) {
HashSet<String> hashSet = new HashSet<>();
hashSet.add("hello");
hashSet.add("world");
hashSet.add("java");
//迭代器循環
Iterator<String> iterator = hashSet.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//foreach循環
for (String s : hashSet
) {
System.out.println(s);
}
}
LinkedHashSet
上面提到HashSet是無序的,如果想要有序,就需要用到LinkedHashSet
Collections類
conections是集合的工具類。
- public static <T> boolean addAll(Collection<T> c, T... elements) `:往集合中添加一些元素。
- `public static void shuffle(List<?> list) 打亂順序`:打亂集合順序。
- `public static <T> void sort(List<T> list)`:將集合中元素按照默認規則排序。
- `public static <T> void sort(List<T> list,Comparator<? super T> )`:將集合中元素按照指定規則排序。
public static void main(String[] args) {
//addAll()
ArrayList<String> arrayList = new ArrayList<>();
Collections.addAll(arrayList, "hello","world","java","agou");
Iterator<String> iterator = arrayList.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
System.out.println("================================");
//sort()
ArrayList<Integer> integers = new ArrayList<>();
Collections.addAll(integers, 11,22,12,45,67453,213,123456);
System.out.println(integers);
Collections.sort(integers);
System.out.println(integers);
}
Map
在 Collection
中,元素都是單個存在的。
而map中的集合,元素是成對存在的(類似於python中的字典)
每個元素都由鍵和值組成,通過鍵可以找到對應的值。
Map接口中的方法
public V put(K key, V value) : 把指定的鍵與指定的值添加到Map集合中。
public V remove(Object key) : 把指定的鍵 所對應的鍵值對元素 在Map集合中刪除,返回被刪除元素的
值。
public V get(Object key) 根據指定的鍵,在Map集合中獲取對應的值。
public Set<K> keySet() : 獲取Map集合中所有的鍵,存儲到Set集合中。
public Set<Map.Entry<K,V>> entrySet() : 獲取到Map集合中所有的鍵值對對象的集合(Set集合)
Map接口中的集合都有兩個泛型變量,在使用時,要爲兩個泛型變量賦予數據類型。兩個泛型變量的數
據類型可以相同,也可以不同。
public static void main(String[] args) {
HashMap<Integer, String> stringIntegerHashMap = new HashMap<Integer, String>();
//put() 添加元素
stringIntegerHashMap.put(1, "xiaoming");
stringIntegerHashMap.put(2, "xiaohong");
stringIntegerHashMap.put(3, "xiaobai");
stringIntegerHashMap.put(4, "agou");
stringIntegerHashMap.put(5, "zhendegou");
System.out.println(stringIntegerHashMap);
//size() 獲取長度
System.out.println(stringIntegerHashMap.size());
//remove() 刪除指定key所對應的value
stringIntegerHashMap.remove(3);
System.out.println(stringIntegerHashMap);
//get() 獲取指定key對應的value
System.out.println(stringIntegerHashMap.get(4));
}
HashMap循環遍歷value
map裏面提供了一個獲取所以鍵值的方法keyset。
public class HashMapKeySetDemo {
public static void main(String[] args) {
HashMap<Integer, String> integerStringHashMap = new HashMap<>();
integerStringHashMap.put(1,"xiao");
integerStringHashMap.put(2,"a");
integerStringHashMap.put(3,"gou");
Set<Integer> integers = integerStringHashMap.keySet();
for (int key : integers
) {
String value = integerStringHashMap.get(key);
System.out.println(value);
}
}
}
HashMap循環遍歷Key Value
Set<Map.Entry<String, String>> entries = map.entrySet();
Entry將鍵值對的對應關係封裝成了對象。即鍵值對對象,這樣我們在遍歷Map集合時,就可以從每一個鍵值對(Entry)對象中獲取對應的鍵與對應的值。
public K getKey():獲取Entry對象中的鍵。
public V getValue()`:獲取Entry對象中的值。
public class HashMapEntrySetDemo {
public static void main(String[] args) {
HashMap<Integer, String> integerStringHashMap = new HashMap<>();
integerStringHashMap.put(1,"xiao");
integerStringHashMap.put(2,"a");
integerStringHashMap.put(3,"gou");
Set<Map.Entry<Integer, String>> entries = integerStringHashMap.entrySet();
for (Map.Entry<Integer, String> entry : entries) {
int key = entry.getKey();
String value = entry.getValue();
System.out.println("Key:" + key + " " + "Value:" + value);
}
}
}
Key:1 Value:xiao
Key:2 Value:a
Key:3 Value:gou
LinkedHashMap
Hashmap的存放是無序的,如果需要有序存儲數據的話,就需要用到LinkedHashMap。
public class LinkedHashMapDemo {
public static void main(String[] args) {
LinkedHashMap<Integer, String> integerStringLinkedHashMap = new LinkedHashMap<>();
integerStringLinkedHashMap.put(1, "xiao");
integerStringLinkedHashMap.put(2, "bei");
integerStringLinkedHashMap.put(3, "bei");
Set<Map.Entry<Integer, String>> entries = integerStringLinkedHashMap.entrySet();
for (Map.Entry<Integer, String> entry : entries
) {
String value = entry.getValue();
int key = entry.getKey();
System.out.println("Key:" + key + " " + "Value:" + value);
}
}
}