java collection 集合

 

java 基本數據類型

byte、short、int、long、float、double、char、boolean。

原文: https://blog.csdn.net/weixin_44551646/article/details/94295677

List集合

List集合爲列表類型,以線性方式存儲對象。List集合中的元素允許重複,各元素的順序就是對象插入的順序。用戶可以通過使用索引來訪問List集合中的元素。

ArrayList集合 

底層是使用數組實現,所以查詢速度快,增刪速度慢

 

LinkedList集合

是基於鏈表結構實現的,所以查詢速度慢,增刪速度快,提供了特殊的方法,對頭尾的元素操作(進行增刪查)

 

ArrayList與LinkedList
    ArrayList和LinkedList顧名思義,ArrayList是Array(動態數組)的數據結構,相當於動態數組;LinkedList是Link(鏈表)的雙向數據結構,也可當作堆棧、隊列、雙端隊列。
    對於隨機訪問List時(get和set操作),ArrayList比LinkedList的效率更高,因爲LinkedList是線性的數據存儲方式,所以需要移動指針從前往後依次查找。
    對於新增和刪除操作add和remove,LinedList比較佔優勢,因爲ArrayList要移動數據(可以在上述ArrayList代碼中體現)。
兩者缺點都爲線性不安全
    ArrayList和LinkedList線程不安全,在多線程中不建議使用。

上兩個線程都不安全 vector 線程安全 但是比上面的兩個增刪改查都慢

 

 

Set集合

  • Set集合中的對象不按特定的方式排序,只是簡單的將對象加入到集合中,但是Set集合不能包括重複對象
  • Set接口繼承了Collection接口,因此也包含Collection接口的所有方法

哈希表:

 å¨è¿éæå¥å¾çæè¿°

 

Hashset集合

我們要想往hashSet裏存入數據,就只能重寫hashcode()和equals()方法!! hashset 存入的數據其實是根據一定的順序排列的 這裏的順序跟存入的順序不是一個概念


public class Student{	
	
	private String name;
	private int id;
	
	public Student(){}
	
	public Student(String name, int id)
	{
		super();
		this.name = name;
		this.id = id;
	}

	@Override
	public int hashCode()
	{
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (id != other.id)
			return false;
		if (name == null)
		{
			if (other.name != null)
				return false;
		}
		else if (!name.equals(other.name))
			return false;
		return true;
	}

	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public int getId()
	{
		return id;
	}
	public void setId(int id)
	{
		this.id = id;
	}

	@Override
	public String toString()
	{
		return "Student [name=" + name + ", id=" + id + "]";
	}	
	
}

import java.util.HashSet;
import java.util.Iterator;



class Test{
	public static void main(String []args) {
		HashSet<Student> hs = new HashSet<>();
		
		hs.add(new Student("yiyi",1));
		hs.add(new Student("feifei",2));
		hs.add(new Student("lili",3));

		System.out.println(hs.add(new Student("wawa",4)));	
		System.out.println(hs.add(new Student("wawa",4)));
		System.out.println(hs);  //第一種打印方式,利用重寫的toString()方法和Println()直接打印
		System.out.println("------------");
		for(Student s : hs) {   //第二種打印方式,增強for循環
			System.out.println(s);
		}
		System.out.println("------------");   //第三種打印方式,利用Iteator
		Iterator<Student> it = hs.iterator();
			while(it.hasNext()) {
				Student s = it.next();
				System.out.println(s);
			}
		
		
			
	}
}
true
false
[Student [name=feifei, id=2], Student [name=wawa, id=4], Student [name=yiyi, id=1], Student [name=lili, id=3]]
------------
Student [name=feifei, id=2]
Student [name=wawa, id=4]
Student [name=yiyi, id=1]
Student [name=lili, id=3]
------------
Student [name=feifei, id=2]
Student [name=wawa, id=4]
Student [name=yiyi, id=1]
Student [name=lili, id=3]


 

LinkedHashSet集合

LinkedHashSet繼承自HashSet,特點是:有序,唯一,效率高

 

TreeSet集合

TreeSet爲使用樹來進行儲存的Set接口提供了一個工具,對象按升序儲存,訪問和檢索是很快的。在存儲了大量的需要進行快速檢索的排序信息的情況下,TreeSet是一個不錯的選擇。

 

Map集合

HashMap集合

Map集合沒有繼承Collection接口,其提供的是鍵到值的映射。Map不能包含相同的鍵,每個鍵只能映射一個值。鍵還決定了儲存對象在映射中的儲存位置。單一,無序

LinkedHashMap集合

用法跟HashMap基本一致,它是基於鏈表和哈希表結構的所以具有存取有序,鍵不重複的特性,存的順序和遍歷出來的順序是一致的

TreeMap集合

TreeMap與TreeSet類似,都需要重寫比較器(外部比較器+內部比較器)
TreeMap集合特點:單一,有序

 

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