#java讀書筆記#集合架構2

來源:互聯網
上載者:User

#java讀書筆記#集合架構2
4:基礎資料型別 (Elementary Data Type)的對象封裝類
(1)為了更方便的操作每個基礎資料型別 (Elementary Data Type),java對其提供了很多的屬性和方法供我們使用。
(2)用途:
將基礎資料型別 (Elementary Data Type)封裝成對象的好處在於可以在對象中定義更多的功能操作該資料。
常用的操作之一:用於基礎資料型別 (Elementary Data Type)與字串之間的轉換。
A:方便操作
B:用於和字串進行相互轉換
(3)基礎資料型別 (Elementary Data Type)和物件類型的對應
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character
(4)構造方法
欄位摘要:
static int MAX_VALUE 值為 2^31-1 的常量,它表示 int 類型能夠表示的最大值
static int MIN_VALUE 值為 -2^31 的常量,它表示 int 類型能夠表示的最小值
static Class TYPE 表示基本類型int的Class 執行個體

Integer(int value) 構造一個新分配的Integer對象,它表示指定的int值。
Inreger(String s) 注意:s必須是純數位字串。否則會有異常NumberFormatException

(5)幾個常用的方法
Integer.toBinaryString();
以二進位(基數 2)不帶正負號的整數形式返回一個整數參數的字串表示形式。
Integer.toOctalString();
以八進位(基數 8)不帶正負號的整數形式返回一個整數參數的字串表示形式。
Integer.toHexString();
以十六進位(基數 16)不帶正負號的整數形式返回一個整數參數的字串表示形式。
static int Integer.parseInt(String s) 將字串參數作為有符號的十進位整數進行解析,
字串必須是int型範圍內的數字字串
static int Integer.parseInt(String s,int basic)
使用第二個參數指定的基數,將字串參數解析為有符號的整數.
字串必須是int型範圍內的數字字串
short shortValue() 以short類型返回該Integer的值。
int intValue() 以int類型返回該Integer的值。
static Integer valueOf(int num) 返回一個表示指定的 int 值的 Integer 執行個體。
static Integer valueOf(String s) 返回儲存指定的String的值的Integer對象。
static Integer valueOf(String s, int radix)
返回一個Integer對象,該對象中儲存了用第二個參數提供的基數進行
解析時從指定的String中提取的值。
(6)類型轉換
int -- Integer
int num = 20;
A:Integer i = new Integer(num);
B:Integer i = Integer.valueOf(num);
Integer -- int
Integer i = new Integer(20);
A:int num = i.intValue();

int -- String
int num = 20;
A:String s = String.valueOf(num);
B:String s = ""+num;
C:String s = Integer.toString(num);
String -- int
String s = "20";
A:int num = Integer.parseInt(s);
B:Integer i = new Integer(s);或者Integer i = Integer.valueOf(s);
int num = i.intValue();
5、集合架構:
(1)為什麼出現集合類?
物件導向對事物的體現都是以對象的形式,為了方便對多個對象的操作,就對對象進行儲存。
集合就是儲存物件最常用的一種方式.
(2)數組和集合都是容器,兩者有何不同?
數組長度固定,而集合長度是可變的
數組值可以儲存物件,還可以儲存基礎資料型別 (Elementary Data Type);而集合只能儲存物件
數組儲存資料類型是固定的,而集合儲存的資料類型不固定
(3)集合類的特點:
集合只能儲存物件
集合的長度是可變的
集合可以儲存不同類型的對象
(4)集合類架構(重要!!!要分清幾種容器間的區別):
Collection:頂層介面
|--->List:列表,元素是有序的(元素帶角標索引),可以有重複元素,可以有null元素。
|--->ArrayList(JDK1.2):底層的資料結構是數組資料結構,特點是查詢速度快(因為帶角標),
但是增刪速度稍慢,因為當元素多時,增刪一個元素則所有元素的角標都得改變
線程不同步。預設長度是10,當超過長度時,按50%延長集合長度。
|--->LinkedList(JDK1.2):底層資料結構式鏈表資料結構(即後面一個元素記錄前一個),
特點:查詢速度慢,因為每個元素只知道前面一個元素,但增刪速度快
因為元素再多,增刪一個,只要讓其前後的元素重新相連即可
線程是不同步的。
|--->Vector(JDK1.0):底層資料結構是數組資料結構.特點是查詢和增刪速度都很慢。
預設長度是10,當超過長度時,按100%延長集合長度。
線程同步。
(Vector功能跟ArrayList功能一模一樣,已被ArrayList替代)


List使用注意!
|--->ArrayList:
(1)當往ArrayList裡面存入元素沒什麼要求時,即只要求有序就行時;

(2)當往ArrayList裡面存入元素要求不重複時,比如存入學生對象,當同名同姓時
視為同一個人,則不往裡面儲存。則定義學生對象時,需複寫equals方法
public boolean equals(Object obj)
{
if(!(obj instanceof Student))
return false;
Student stu = (Student)obj;
return this.name.equals(stu.name)&&this.age==stu.age;
}
則往ArrayList集合通過add存入學生對象時,集合底層自己會調用學生類的equals方法,
判斷重複學生則不存入。
註:對於List集合,無論是add、contains、還是remove方法,判斷元素是否相同,
都是通過複寫equals方法來判斷!


|--->LinkedList
(1)LinkLedist的特有方法:
boolean offerFirst(E e) 在此列表的開頭插入指定的元素。
boolean offerLast(E e) 在此列表末尾插入指定的元素。
E peekFirst() 擷取但不移除此列表的第一個元素;如果此列表為空白,則返回 null。
E peekLast() 擷取但不移除此列表的最後一個元素;如果此列表為空白,則返回 null。
E pollFirst() 擷取並移除此列表的第一個元素;如果此列表為空白,則返回 null。
E pollLast() 擷取並移除此列表的最後一個元素;如果此列表為空白,則返回 null。
(2)通過LinkLedist的特有方法,可以實現某些資料特殊方式的存取,比如堆棧和隊列。


一般情況下,使用哪種List介面下的實作類別呢?
如果要求增刪快,考慮使用LinkedList
如果要求查詢快,考慮使用ArrayList
如果要求安全執行緒,考慮使用Vector。


|--->Set:集合,元素是無序的(因為沒有索引),元素不可以重複。可以有null元素。
|--->HashSet(JDK1.2):底層資料結構是雜湊表、存取速度快、元素唯一、線程不同步。
保證性元素唯一的原理:
先判斷元素的hashCode值是否相同,再判斷兩元素的equals方法是否為true
(往HashSet裡面存的自訂元素要複寫hashCode和equals方法,
以保證元素的唯一性!)
|--->TreeSet:底層資料結構式二叉樹。可以對Set集合中的元素進行排序。元素有序、線程不同步。
保證元素唯一性的依據:compareTo方法return 0
TreeSet排序的第一種方式:讓元素自身具備比較性,比如八種基礎資料型別 (Elementary Data Type)或則字串,
實現Compareble介面,覆蓋compareTo方法,
此方式是元素的自然順序
TreeSet排序的第一種方式:當元素自身不具備比較性(比如儲存學生對象時)或者具備的
比較性不是我們所需要的比較性時(比如想字串的長度排序),
此時就需要讓集合自身具備自訂的比較性。
那如何讓集合自身具備比較性呢?可在集合初始化時,
就讓集合具備比較方式。即定義一個類,
實現Comparator介面,覆蓋compare方法。


Set集合使用注意事項:
(1)HashSet:
通過new的方式往HashSet裡面存的元素的hashCode都不同,但通常我們定義對象,
比如學生對象時,雖然是new的兩個學生對象,但是當他們name和age一樣時,我們認為是
同一個對象,所以為了保證元素的唯一性,我們通常在往HashSet集合裡面儲存元素時,
在定義對象的類中通常複寫hashCode和equals方法。
public int hashCode()
{
return name.hashCode()+age*39;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Student))
return false;
Student stu = (Student)obj;
return this.name.equals(stu.name)&&this.age==stu.age;
}


HashSet是如何保證元素唯一性的呢?
如果兩元素的hashCode值不同,則不會調用equals方法
如果兩元素的hashCode值相同,則繼續判斷equals是否返回true;
hashCode和equals方法雖然定義在自訂對象類裡面,但不是我們手動調用
而是往HashSet集合裡面儲存元素的時候,集合底層自己調用hashCode和equals
它自己拿對象去判斷,自己判斷兩元素是否是同一個元素。


(2)TreeSet:
TreeSet要求往裡面存的元素具備比較性,否則會報錯。
TreeSet排序的第一種方式:讓元素自身具備比較性
定義對象類,實現Compareble介面,複寫compareTo方法,此方式是元素的自然順序
class Student implements Comparable
{
private String name;
private int age;
public Student(String name,int age)
{
this.name=name;
this.age=age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public int compareTo(Object obj)
{
if(!(obj instanceof Student))
throw new RuntimeException("不是學生對象!");
Student stu = (Student)obj;
int num = this.age-stu.age;
if(num==0)
return this.name.compareTo(stu.name);
return num;
}
}
TreeSet排序的第一種方式:讓集合具備比較性
當元素自身不具備比較性(比如儲存學生對象時)或者具備的
比較性不是我們所需要的比較性時(比如想字串的長度排序),
此時就需要讓集合自身具備自訂的比較性。
那如何讓集合自身具備比較性呢?可在集合初始化時,
就讓集合具備比較方式。即定義一個類,
實現Comparator介面,覆蓋compare方法。
class StringLengthComparator implements Comparator
{
public int compare(Object obj1,Object obj2)
{
String s1 = (String)obj1;
String s2 = (String)obj2;
int num = new Integer(s1.length()).compareTo(new Integer(s2.length()));
if(num==0)
return s1.compareTo(s2);
return num;
}
}
class TreeSetTest
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet(new StringLengthComparator());
ts.add("addfg");
ts.add("dfg");
ts.add("agtuug");
ts.add("vgjkg");
sop(ts);
}
}

基礎資料型別 (Elementary Data Type)或字串對象均實現了Comparable介面,故同種類型基本資料間具備比較性,即自然順序。


Map:頂層介面,該集合儲存的是索引值對,而且鍵是唯一的,Map和Set很像,Set集合底層就是使用了Map集合。
Map集合沒有迭代器,要取出元素必須先將Map集合轉換成Set集合才能遍曆元素
|--->HashTable(JDK1.0):
底層是雜湊表資料結構;
不可以使用null鍵和null值;
用作鍵的對象必須實現hashCode和equals方法來保證鍵的唯一性
線程同步,效率低
|--->HashMap(JDK1.2):
底層是雜湊表資料結構;
允許使用null鍵和null值;
線程不同步,效率高;
保證元素唯一性的:
原理:先判斷元素的hashCode值是否相同,再判斷兩元素的equals方法是否為true
(往HashSet裡面存的自訂元素要複寫hashCode和equals方法,
以保證元素的唯一性!)
class Student {
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

@Override
public int hashCode(){
return name.hashCode()+age*34;
}
@Override
public boolean equals(Object obj){

if(!(obj instanceof Student))
return false;
Student stu = (Student)obj;
return this.name.equals(stu.name)&&this.age==stu.age;
}
public class HashMapDemo1 {
public static void main(String[] args) {
Map hmap = new HashMap();
hmap.put(new Student("001",20), "beijing");
hmap.put(new Student("002",25), "hebei");
hmap.put(new Student("003",50), "hainan");
hmap.put(new Student("001",20), "beijing");

System.out.println(hmap.size());
Set keySet = hmap.keySet();
Iterator it = keySet.iterator();
while(it.hasNext()){
Student stu = it.next();
String addr = hmap.get(stu);
System.out.println(stu.getName()+".."+stu.getAge()+"::"+addr);
}
}
}
|--->TreeMap(JDK1.0):
底層是二叉樹結構;
允許使用null鍵和null值;
線程不同步;
可以給Map集合中的鍵進行排序.
TreeMap排序的第一種方式:讓元素自身具備比較性,比如八種基礎資料型別 (Elementary Data Type)或則字串,
實現Compareble介面,覆蓋compareTo方法,
此方式是元素的自然順序
TreeMap排序的第一種方式:當元素自身不具備比較性(比如儲存學生對象時)或者具備的
比較性不是我們所需要的比較性時(比如想字串的長度排序),
此時就需要讓集合自身具備自訂的比較性。
那如何讓集合自身具備比較性呢?可在集合初始化時,
就讓集合具備比較方式。即定義一個類,
實現Comparator介面,覆蓋compare方法。
class Student implements Comparable{
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Student stu) {
int num = new Integer(this.age).compareTo(new Integer(stu.age));
if(num==0)
return this.name.compareTo(stu.name);
return num;
}
}


public class HashMapDemo1 {
public static void main(String[] args) {

Map tmap = new TreeMap();
tmap.put(new Student("001",20), "beijing");
tmap.put(new Student("002",25), "hebei");
tmap.put(new Student("003",50), "hainan");
tmap.put(new Student("001",20), "beijing");

System.out.println(tmap.size());
Set keySet1 = tmap.keySet();
Iterator it1 = keySet1.iterator();
while(it1.hasNext()){
Student stu = it1.next();
String addr = tmap.get(stu);
System.out.println(stu.getName()+".."+stu.getAge()+"::"+addr);
}
}
}


Iterator:對collection進行迭代的迭代器.迭代器取代了Enumeration。
迭代器和枚舉的區別:
迭代器允許調用者利用定義良好的語義在迭代期間從迭代器所指向的collection移除元素
方法名稱得到了改進,簡化書寫
LisIterator:系列表迭代器,允許程式員按任一方向遍曆列表、迭代期間修改列表
Comparable:此介面強行對實現它的每個類的對象進行整體自然排序。使元素具備比較性
Comparator:強行對某個對象collection進行整體排序的比較函數,使集合具備比較性
Collections:此類完全由在 collection 上進行操作或返回 collection 的靜態方法組成。
Arrays:此類包含用來運算元組(比如排序和搜尋)的各種靜態方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.