JAVA集合及常用方法說明

JAVA集合框架中的接口

1.Collection接口偷俭。該接口定義了一個存取一組對象的方法,是最基本的接口

2.Set接口衩婚。該接口繼承Collection接口剔应,它包含的數(shù)據(jù)沒有順序且不可以重復(fù)

3.List接口。該接口繼承Collection接口熬拒,它包含的數(shù)據(jù)有順序且可以重復(fù)

4.Map接口爷光。該接口是一個單獨的接口,不繼承Collection接口澎粟。它是一種把鍵對象和值對象進行關(guān)聯(lián)的容器蛀序,不可以包含重復(fù)鍵

JAVA集合中的實現(xiàn)類

1.HashSet欢瞪。實現(xiàn)了Set接口,為無序集合徐裸,能夠快速定位一個元素遣鼓。需要注意的是,存入HashSet中的對象必須實現(xiàn)HashCode()方法

2.TreeSet重贺。不僅實現(xiàn)了Set接口骑祟,還實現(xiàn)了Sorted接口,可對集合進行自然排序(即升序排序)

3.ArrayList气笙。實現(xiàn)了List接口次企,為有序集合,它的大小可變并且可以像鏈表一樣被訪問潜圃。它是以數(shù)組的方式實現(xiàn)的List接口缸棵,允許快速隨機存取

4.LinkedList。實現(xiàn)了List接口谭期,為有序集合堵第,通過一個鏈表的形式實現(xiàn)List接口,提供最佳順序存取崇堵,適合插入和移除元素型诚。有這個類定義的鏈表也可以像棧或隊列一樣被使用

5.HashMap鸳劳。實現(xiàn)一個“鍵-值”映射的哈希表狰贯,通過鍵獲取值對象,沒有順序赏廓,通過get(key)方法來獲取value的值涵紊,允許存儲空對象,而且允許鍵是空的幔摸。不過摸柄,鍵只能有一個

Collection接口中的方法

Collection常用方法

舉栗子:

package com.moxiaofeng2;

import java.util.ArrayList;

import java.util.Collection;

import java.util.Iterator;

public class CollectionTest {

public static void main(String[] args) {

//創(chuàng)建集合C

        Collection collection =new ArrayList();

        //向集合中添加元素

        collection.add("Apple");

        collection.add("Orange");

        collection.add("Banana");

        collection.add("Pear");

        //創(chuàng)建Array集合

        ArrayList arrayList =new ArrayList();

        //向集合中添加元素

        arrayList.add("Dog");

        arrayList.add("Cat");

        System.out.println("集合collection中元素個數(shù):" + collection.size());

        //如果arraylist集合不為空

        if (!arrayList.isEmpty()){

//將集合array里面的元素添加到集合collection中

            collection.addAll(arrayList);

        }

System.out.println("集合collection中元素個數(shù):" + collection.size());

        //返回迭代器

        Iterator iterator = collection.iterator();

        System.out.println("集合collection中元素:");

        //判斷迭代器中是否存在下一個元素

        while (iterator.hasNext()){

//使用迭代器循環(huán)輸出集合中的元素

            System.out.print(iterator.next() +" ");

        }

System.out.println();

        //判斷集合中是否包含Cat

        if (collection.contains("Cat")) {

System.out.println("-------集合collection中包含Cat--------");

        }

//從集合collection中刪除集合arrayList中的所有元素

        collection.removeAll(arrayList);

        //返回迭代器對象

        iterator = collection.iterator();

        System.out.println("集合collection中元素:");

        while(iterator.hasNext()){

System.out.print(iterator.next() +" ");

        }

System.out.println();

        //將集合中的元素存放到字符串數(shù)組中

        Object[] objects = collection.toArray();

        String s ="";

        System.out.println("素組中元素:");

        for (int i =0; i < objects.length; i++){

//String strings = new String();

//strings = objects[i].toString();

            s = (String)objects[i];

            //System.out.print(strings + " ");

            System.out.print(s +" ");

        }

}

}

List接口中的方法

List常用方法

舉栗子:

package com.moxiaofeng2;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ArrayListTest {
    public static void main(String[] args) {
        //創(chuàng)建初始容量為10的空列表
        ArrayList array = new ArrayList();
        //添加元素
        array.add("cat");
        array.add("dog");
        array.add("pig");
        array.add("sheep");
        array.add("pig");
        //創(chuàng)建迭代器
        Iterator iterator = array.iterator();
        System.out.println("array集合中的元素有:");
        while (iterator.hasNext()){
            System.out.print(iterator.next() + " ");
        }
        System.out.println();
        //替換指定索引處的元素
        System.out.println("返回替換集合中索引是1的元素:" + array.set(1,"mouse"));
        iterator = array.iterator();
        System.out.println("元素替換后集合中的元素:");
        while (iterator.hasNext()){
            System.out.print(iterator.next() + " ");
        }
        System.out.println();
        //獲取指定索引處集合的元素
        System.out.println("獲取集合中索引是2的元素:" + array.get(2));
        System.out.println("集合中第一次出現(xiàn)pig的索引:" + array.indexOf("pig"));
        System.out.println("集合中最后一次出現(xiàn)dog的索引:" + array.lastIndexOf("pig"));
        List list = array.subList(1,4);
        iterator = list.iterator();
        System.out.println("新集合中的元素");
        while (iterator.hasNext()){
            System.out.print(iterator.next() + " ");
        }
    }
}

LinkedList類的方法

LinkedList常用方法

舉栗子:

package com.moxiaofeng2;

import java.util.Iterator;
import java.util.LinkedList;

public class LinkedListTest {
    public static void main(String[] args) {
        //創(chuàng)建LinkedList集合對象
        LinkedList linkedList = new LinkedList();
        //添加元素
        linkedList.add("cat");
        linkedList.add("dog");
        linkedList.add("pig");
        linkedList.add("sheep");
        linkedList.addFirst("mouse");
        linkedList.addLast("duck");
        //創(chuàng)建迭代器遍歷集合元素
        Iterator iterator = linkedList.iterator();
        System.out.println("linkedList集合中的元素分別有:");
        while (iterator.hasNext()){
            System.out.print(iterator.next() + " ");
        }
        System.out.println();
        System.out.println("獲取集合中第一個元素:" + linkedList.getFirst());
        System.out.println("獲取集合中最后一個元素:" + linkedList.getLast());
        System.out.println("刪除集合中第一個元素:" + linkedList.removeFirst());
        System.out.println("刪除集合中最后一個元素:" + linkedList.removeLast());
        iterator = linkedList.iterator();
        while (iterator.hasNext()){
            System.out.print(iterator.next() + " ");
        }
    }
}

TreeSet類提供方法

TreeSet常用方法

舉栗子:

package com.moxiaofeng2;

import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;

public class TreeSetTest {
    public static void main(String[] args) {
        //創(chuàng)建TreeSet集合
        TreeSet treeSet = new TreeSet();
        //添加元素
        treeSet.add("45");
        treeSet.add("32");
        treeSet.add("55");
        treeSet.add("87");
        treeSet.add("89");
        treeSet.add("34");
        treeSet.add("99");
        //獲取集合中元素個數(shù)
        System.out.println("集合中元素一共有:" + treeSet.size());
        //創(chuàng)建迭代器,遍歷集合
        Iterator iterator = treeSet.iterator();
        System.out.println("treeset中集合元素為:");
        while (iterator.hasNext()){
            System.out.print(iterator.next() + " ");
        }
        System.out.println();

        System.out.println("獲取34~89之間的集合:");
        //System.out.print(treeSet.subSet("34","89"));
        SortedSet sortedSet = treeSet.subSet("34","89");
        iterator = sortedSet.iterator();
        while (iterator.hasNext()){
            System.out.print(iterator.next() + " ");
        }
        System.out.println();
        System.out.println("集合中89之前的元素");
        SortedSet sortedSet1 = treeSet.headSet("89");
        iterator = sortedSet1.iterator();
        while (iterator.hasNext()){
            System.out.print(iterator.next() + " ");
        }
        System.out.println();
        System.out.println("獲取45之后的數(shù)據(jù)");
        SortedSet sortedSet2 = treeSet.tailSet("45");
        iterator = sortedSet2.iterator();
        while (iterator.hasNext()){
            System.out.print(iterator.next() + " ");
        }
        System.out.println();
        System.out.println("獲取集合中第一個元素:" + treeSet.first());
        System.out.println("獲取集合中最后一個元素:" + treeSet.last());
        System.out.println("獲取并移除集合中第一個元素:" + treeSet.pollFirst());
        System.out.println("獲取并移除集合中最后一個元素:" + treeSet.pollLast());
        //遍歷集合中的元素
        iterator = treeSet.iterator();
        while (iterator.hasNext()){
            System.out.print(iterator.next() + " ");
        }
    }
}

Map接口提供方法

Map常用方法

舉栗子:

package com.moxiaofeng2;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class HashMapTest {
    public static void main(String[] args) {
        //創(chuàng)建Map集合
        HashMap hashMap = new HashMap();
        //添加元素
        hashMap.put("1","一代天驕");
        hashMap.put("2","成吉思汗");
        hashMap.put("3","只識彎弓射大雕");
        hashMap.put("4","俱往矣");
        hashMap.put("5","數(shù)風流人物");
        hashMap.put("5","還看今朝");
        System.out.println("指定鍵2獲取值:" + hashMap.get("2"));
        Set set = hashMap.keySet();
        Iterator iterator = set.iterator();
        //獲取HashMap中值的合并輸出
        String key = "";
        while (iterator.hasNext()){
            key = (String)iterator.next();
            System.out.println(key + ":" + hashMap.get(key));
        }
    }
}
沒有夢想何必遠方既忆,我是小鯨魚驱负,一條奔跑在無邊海域的小鯨魚
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市患雇,隨后出現(xiàn)的幾起案子跃脊,更是在濱河造成了極大的恐慌,老刑警劉巖苛吱,帶你破解...
    沈念sama閱讀 218,122評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件酪术,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機绘雁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評論 3 395
  • 文/潘曉璐 我一進店門橡疼,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人庐舟,你說我怎么就攤上這事欣除。” “怎么了挪略?”我有些...
    開封第一講書人閱讀 164,491評論 0 354
  • 文/不壞的土叔 我叫張陵耻涛,是天一觀的道長。 經(jīng)常有香客問我瘟檩,道長,這世上最難降的妖魔是什么澈蟆? 我笑而不...
    開封第一講書人閱讀 58,636評論 1 293
  • 正文 為了忘掉前任墨辛,我火速辦了婚禮,結(jié)果婚禮上趴俘,老公的妹妹穿的比我還像新娘睹簇。我一直安慰自己,他們只是感情好寥闪,可當我...
    茶點故事閱讀 67,676評論 6 392
  • 文/花漫 我一把揭開白布太惠。 她就那樣靜靜地躺著,像睡著了一般疲憋。 火紅的嫁衣襯著肌膚如雪凿渊。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,541評論 1 305
  • 那天缚柳,我揣著相機與錄音埃脏,去河邊找鬼。 笑死秋忙,一個胖子當著我的面吹牛彩掐,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播灰追,決...
    沈念sama閱讀 40,292評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼堵幽,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了弹澎?” 一聲冷哼從身側(cè)響起朴下,我...
    開封第一講書人閱讀 39,211評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎裁奇,沒想到半個月后桐猬,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,655評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡刽肠,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,846評論 3 336
  • 正文 我和宋清朗相戀三年溃肪,在試婚紗的時候發(fā)現(xiàn)自己被綠了免胃。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,965評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡惫撰,死狀恐怖羔沙,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情厨钻,我是刑警寧澤扼雏,帶...
    沈念sama閱讀 35,684評論 5 347
  • 正文 年R本政府宣布,位于F島的核電站夯膀,受9級特大地震影響诗充,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜诱建,卻給世界環(huán)境...
    茶點故事閱讀 41,295評論 3 329
  • 文/蒙蒙 一蝴蜓、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧俺猿,春花似錦茎匠、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至谊惭,卻和暖如春汽馋,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背圈盔。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評論 1 269
  • 我被黑心中介騙來泰國打工惭蟋, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人药磺。 一個月前我還...
    沈念sama閱讀 48,126評論 3 370
  • 正文 我出身青樓告组,卻偏偏與公主長得像,于是被迫代替她去往敵國和親癌佩。 傳聞我的和親對象是個殘疾皇子木缝,可洞房花燭夜當晚...
    茶點故事閱讀 44,914評論 2 355