java常見的集合:
- Map、HashMap、TreeMap
- List、ArrayList、LinkedList
- Set、HashSet、TreeSet
代碼demo:
- 主函數(shù):
import javax.sound.midi.Soundbank;
import java.sql.SQLOutput;
import java.util.*;
public class MyHogwartsDemo2 {
public static void main(String[] args) {
method8();
}
}
- Map、HashMap、TreeMap
/**
* HashSet:不允許數(shù)據(jù)重復(fù);允許null;無序
*/
public static void method6() {
HashSet<String> hashSet = new HashSet();
hashSet.add("aaa");
hashSet.add("bbb");
hashSet.add("bbb");
hashSet.add("bbb");
hashSet.add("ccc");
hashSet.add(null);
// 遍歷:增強for循環(huán)
for (String h : hashSet) {
System.out.println(h);
}
}
/**
* linkedHashSet:不允許數(shù)據(jù)重復(fù);允許null;無序;按照插入順序排序
*/
public static void method7() {
LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("aaa");
linkedHashSet.add("bbb");
linkedHashSet.add("bbb");
linkedHashSet.add("bbb");
linkedHashSet.add("ccc");
linkedHashSet.add(null);
// 遍歷:增強for循環(huán)
for (String h : linkedHashSet) {
System.out.println(h);
}
}
/**
* TreeSet:自動排序 put一個null的時候報錯空指針
*/
public static void method8() {
TreeSet<String> treeSet = new TreeSet<>();
treeSet.add("aaa");
treeSet.add("bbb");
treeSet.add("eee");
treeSet.add("fff");
treeSet.add("ccc");
// treeSet.add(null);
System.out.println(treeSet.size());
// 遍歷:增強for循環(huán)
for (String h : treeSet) {
System.out.println(h);
}
}
- List、ArrayList、LinkedList
/**
* arraylist的了解及基本操作
* arraylist底層為可變大小的數(shù)組,遍歷和查找元素較快,插入刪除元素較慢
*/
public static void method2() {
// new一個ArrayList數(shù)組對象,以及練習(xí)常用的方法
// <String> 泛型,是指數(shù)組中存的是什么類型的內(nèi)容;這樣添加的時候就有類型限制;開發(fā)常用
// 添加元素,在指定位置添加元素;判斷數(shù)組中是否含有元素
ArrayList<String> arrayList = new ArrayList();
arrayList.add("aaa");
arrayList.add("bbb");
arrayList.add("ccc");
arrayList.add("ddd");
arrayList.add("eee");
arrayList.add(0, "zzz");
// 遍歷數(shù)組并且打?。鹤⒁馊〉拈L度的辦法以及獲取數(shù)組值的辦法
// for (int i = 0; i < arrayList.size(); i++) {
// System.out.println("元素的位置是:" + i + ";元素內(nèi)容是" + arrayList.get(i));
// }
// System.out.println(arrayList);
ArrayList<String> arrayList1 = new ArrayList();
arrayList1.add("aaa");
arrayList1.add("222");
arrayList1.add("333");
arrayList1.add("444");
arrayList1.add("555");
arrayList.addAll(arrayList1);
// 遍歷addall之后的數(shù)組;拿數(shù)據(jù)用get
for (int i = 0; i < arrayList.size(); i++) {
System.out.println("元素的位置是:" + i + ";元素內(nèi)容是" + arrayList.get(i));
}
// 判斷是否包含某個元素
boolean flag = arrayList.contains("1");
System.out.println(flag);
// 倒序查找元素
int a = arrayList.lastIndexOf("aaa");
System.out.println(a);
// arraylist 轉(zhuǎn)成數(shù)組,就可以使用數(shù)組的遍歷了
Object[] oo = arrayList.toArray();
for (int i = 0; i < oo.length; i++) {
System.out.println(oo[i]);
}
System.out.println(oo);
}
/**
* Linkedlist的了解及基本操作
* Linkedlist底層為鏈表,插入和刪除元素較慢,遍歷和查找元素較慢
*/
public static void method3() {
LinkedList linkedList = new LinkedList();
linkedList.addFirst("AAA");
linkedList.addLast("ZZZ");
linkedList.add("BBB");
// System.out.println(linkedList.iterator());;
// peek() 方法是從隊列中刪除最后一個元素;隊列為空,返回null
// poll() 方法是從隊列中刪除第一個元素;隊列為空,返回null
for (int i = 0; i < linkedList.size(); i++) {
System.out.println(linkedList.peek());
System.out.println("----------");
// System.out.println(linkedList.poll());
}
}
- Set、HashSet、TreeSet
/**
* HashMap無序的;;鍵值對key-value映射,訪問較快;鍵值可為null;
* HashMap的三種遍歷方式;key重復(fù)的話,會被替換;按照put的順序取值
*/
public static void method4() {
HashMap<String, String> hashMap = new HashMap();
hashMap.put("aaa", "111");
hashMap.put("bbb", "222");
hashMap.put("ccc", "333");
// Set<String> keySet = hashMap.keySet();
// hashmap的第一種遍歷,遍歷key,通過key獲取value
for (String key : hashMap.keySet()) {
System.out.println("key:" + key);
System.out.println("key is " + key + ";value is " + hashMap.get(key));
}
// hashmap的第二種遍歷;最推薦
for (Map.Entry<String, String> entry : hashMap.entrySet()) {
System.out.println("key is " + entry.getKey() + ";value is " + entry.getValue());
}
// hashmap的第三種遍歷,直接遍歷value
for (String value : hashMap.values()) {
System.out.println("value is " + value);
}
}
/**
* LinkedHashMap:繼承自HashMap,使用元素的自然順序?qū)υ剡M行排序
* 遍歷同hashmap
*/
public static void method5() {
LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap();
linkedHashMap.put(null, null);
linkedHashMap.put("name", "zhangsan");
}