Java 集合初步瞭解

ArrayList

單線程可用,線程沒有Vector安全

ArrayList al = new ArrayList();
for(Object o : al){
	system.out.print(o);
}

泛型 指定存入類型後,AllayList只能存入制定的數據類型

ArrayList<Integer> al = new ArrayList<Integer>();

vector

多線程可用,線程更安全
其他方法與ArrayList類似

LinkedList

頻繁插入刪除可用—>鏈表

能添加無序的,能重複的數據


HashSet —哈希值

TreeSet —樹

只能添加無序的,不能重複的數據

自定義數據類型存儲

    ArrayList<Student> arr = new ArrayList<Student>();
    vector<Student> arr = new vector<Student>();
    LinkedList<Student> arr = new LinkedList<Student>();
    HashSet <Student> = new HashSet <Student>();
    TreeSet  <Student> = new TreeSet  <Student>();

    class Student{
		private String Name;
		private int Age;
		public void Student(String Name,Int Age){
			this.Name = Name;
			this.Age = Age;
		}
	}
    

HashMap

只能添加無序的,不能重複的key
什麼是HashMap?
HashMap 內部就是鍵值對,需要一個key 對應一個value 或 一個key 對應 一個集合

HashMap<k,v> map = new HashMap<k,v>();
map.put("name","comi");

遍歷HashMap,獲取map所有的key
返回set

	for(Object o:map.keyset()){
		system.out.print(o);
	}

遍歷HashMap,獲取map所有的value
map.value()返回collection

	for(Object o:map.value()){
		system.out.print(o);
	}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章