本文實(shí)例講述了Java集合定義與用法。分享給大家供大家參考,具體如下:

java集合大體可分為三類,分別是Set、List和Map,它們都繼承了基類接口Collection,Collection接口定義了眾多操作集合的基本方法,如下:

為了訪問Collection集合,不得不去了解Iterator接口。該接口很簡(jiǎn)單,主要用于定義訪問集合的方法,如下:

所以上述的三大類子集合必定都繼承了上面2個(gè)接口。其中Set集合要求元素不重復(fù),且內(nèi)部無(wú)序,所以訪問時(shí)只能根據(jù)元素值來(lái)訪問;List內(nèi)部為動(dòng)態(tài)數(shù)組,支持有序,元素也可重復(fù),所以往往有index;Map所代表的集合是具有Key-Value的映射關(guān)系的集合,如哈希表。
1. Set 1.1 Set不可添加相同元素import java.util.Collection;
import java.util.HashSet;
public class TestSet {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) {
Collection c1 = new HashSet();
Person p = new Person();
c1.add(p);
c1.add(p);
System.out.println(c1);
Collection c2 = new HashSet();
String str1 = new String("123");
String str2 = new String("123");
c2.add(str1);
c2.add(str2);
System.out.println(c2);
}
}
class Person {
public Person() {
}
public Person(String name) {
this.name = name;
}
public String name;
}
當(dāng)前標(biāo)題:Java集合定義與用法實(shí)例總結(jié)【Set、List與Map】-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://www.chinadenli.net/article42/pcehc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、網(wǎng)站維護(hù)、軟件開發(fā)、網(wǎng)站策劃、網(wǎng)站營(yíng)銷、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容