溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java中Collection的常用方法有哪些

發(fā)布時(shí)間:2021-06-15 09:29:17 來源:億速云 閱讀:162 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“Java中Collection的常用方法有哪些”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Java中Collection的常用方法有哪些”這篇文章吧。

Java中Collection的常用方法

1、add() 向中添加元素

add(100) 自動(dòng)裝箱操作,實(shí)際上是放進(jìn)去的一個(gè)對(duì)象, Integer n = new Integer(100),實(shí)際上是把n放進(jìn)了

Collection co = new ArrayList();
co.add(1);

2、addAll( Collection c )

將指定集合中的所有元素添加到從集合中
因?yàn)锳rryList類中重寫了equals() 方法,所以兩個(gè)集合比較相等。

public class lxc {
    public static void main(String[] args) {
        Collection c = new ArrayList();
        for(int i = 0; i < 5; i ++) {
            c.add(i);
        }
        Collection c1 = new ArrayList();
        c1.addAll(c);
        System.out.println(c1.equals(c)); // true
    }
}

3、size() 獲取集合中元素個(gè)數(shù)

Collection co = new ArrayList();
int n = co.size();

4、clear() 清空集合

Collection co = new ArrayList();
co.clear();

5、contains(100) 判斷當(dāng)前集合中是否包含100這個(gè)元素 返回 true、false

Collection co = new ArrayList();
co.add(100);
co.add(200);
boolean r = co.contains(100); // true

*** 深入探究***

例一:

下邊代碼,new了兩個(gè)字符串,s1被添加到集合中去了,但是s2沒有添加進(jìn)去,最后輸入s2是否在集合當(dāng)中?
分析:
按道理來說,s1和s2在棧內(nèi)存中是兩個(gè)變量分別指向了在堆內(nèi)存中存儲(chǔ)的也是兩個(gè)對(duì)象,只不過這兩個(gè)對(duì)象同時(shí)指向了 "123" 在常量池中的地址而已,怎么地集合中都不能包含s2啊?
下邊我們來看下contains源碼:

public class lxc {
    public static void main(String[] args) {
        Collection r = new ArrayList();
        String s1 = new String("123");
        r.add(s1);
        String s2 = new String("123");
        System.out.println(r.contains(s2)); // true
    }
}

contains()源碼:

參數(shù)o是調(diào)用contains()方法傳遞的參數(shù),內(nèi)部調(diào)用了indexOf(),而indexof方法內(nèi)部調(diào)用了indexOfRange方法,在這個(gè)方法中會(huì)去獲取集合中的每一個(gè)元素,然后通過equals() 方法來判斷傳遞的參數(shù)與集合中的元素是否相等,我們傳的參數(shù)是字符串,而字符串的equals()方法在源碼中已經(jīng)被重寫了,只要字符串值相等就想等,實(shí)際判斷的是:s1.equals(s2), 結(jié)果相等,返回元素在集合中的索引,而索引一定 >= 0,所以返回true!
其實(shí)調(diào)用contains() 方法,內(nèi)部是調(diào)用equals()方法來判斷的?。。。。。。。。。。。。。。。?/p>

Java中Collection的常用方法有哪些

例二:

下邊知道為什么返回false了吧,Person類的eqauls() 方法繼承的是object對(duì)象上的,所以沒有重寫equals() 方法的兩個(gè)對(duì)象比較自然返回false了。

public class lxc {
    public static void main(String[] args) {
        Collection r = new ArrayList();
        Person p1 = new Person("lxc", 20);
        r.add(p1);
        Person p2 = new Person("lxc", 20);
        System.out.println(r.contains(p2));  // false
    }
}
class Person{
    String name;
    int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

我們來重寫下Person對(duì)象的eqauls() 方法:

public class lxc {
    public static void main(String[] args) {
        Collection r = new ArrayList();
        Person p1 = new Person("lxc", 20);
        r.add(p1);
        Person p2 = new Person("lxc", 20);
        System.out.println(r.contains(p2));  // true
    }
}
class Person{
    String name;
    int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public boolean equals(Object obj) {
        if(!(obj instanceof Person)) return false;
        if(this == obj) return true;
        Person o = (Person) obj;
        if((this.name == o.name) && (this.age == o.age)) {
            return true;
        }
        return false;
    }

6、remove() 刪除集合中某個(gè)元素

Collection co = new ArrayList();
co.remove(100);

****深入探究****

其實(shí)remove() 方法和contains() 方法類似,內(nèi)部也是調(diào)用了equals() 方法,所以s1和s2相等,刪除了s2等同于刪除了s1。

public class lxc {
    public static void main(String[] args) {
        Collection r = new ArrayList();
        String s1 = new String("abc");
        r.add(s1);
        String s2 = new String("abc");
        Boolean res = r.remove(s2);
        System.out.println(res); // 刪除成功了
        System.out.println(r.size()); // 0
    }
}

remove源碼:
獲取集合中的每一個(gè)元素,使用equals() 方法判斷是否相等,如果相等調(diào)用fastRemove方法刪除元素。

Java中Collection的常用方法有哪些

7、isEmpty() 判斷集合是否為空 true、false

co.isEmpty();

8、Object r = col.toArray() 把集合轉(zhuǎn)數(shù)組

9、iterator 迭代器對(duì)象 (重點(diǎn))

xxx.iterator( ); 獲取迭代器。
Collection h = new HashSet();
Iterator r = h.iterator() 獲取iterator對(duì)象,目的遍歷數(shù)組  r迭代器對(duì)象 - 負(fù)責(zé)迭代集合當(dāng)中的元素。

r迭代器對(duì)象中的方法:
(1)boolean hasNext()如果仍有元素可迭代,則返回true;
(2)Object next() 返回迭代的下一個(gè)元素。
(3)void remove() 沒返回,刪除集合中的元素

public class lxc {
    public static void main(String[] args) {
        Collection h = new HashSet();
        h.add(1);
        h.add(2);
        h.add(new Object());
        // 獲取迭代器
        Iterator r = h.iterator();
        while(r.hasNext()) {
            Object res = r.next();
            System.out.println(res);
        }
    }
}
public class lxc {
    public static void main(String[] args) {
        Collection c = new ArrayList();
        Iterator i = c.iterator();
        c.add(1);
        c.add(2);
        Iterator i1 = c.iterator();
        while(i1.hasNext()) {
            Object r = i1.next();
            i1.remove();
            System.out.println(r);
        }
        System.out.println(c.size()); // 0
    }
}

****重點(diǎn)****

當(dāng)集合的結(jié)構(gòu)發(fā)生改變的時(shí)候,迭代器必須重新獲取,如果還是以前老的迭代器,會(huì)出現(xiàn)異常。
下邊集合的結(jié)構(gòu)發(fā)生了改變,結(jié)果報(bào)錯(cuò):

// 報(bào)錯(cuò):java.base/java.util.ArrayList$Itr.checkForComodification
public class lxc {
    public static void main(String[] args) {
        Collection c = new ArrayList();
        Iterator i = c.iterator();
        c.add(1);
        c.add(2);
        while(i.hasNext()) {
            Object r = i.next();
            System.out.println(r);
        }
    }
}

修改:

public class lxc {
    public static void main(String[] args) {
        Collection c = new ArrayList();
        Iterator i = c.iterator();
        c.add(1);
        c.add(2);
        Iterator i1 = c.iterator();
        while(i1.hasNext()) {
            Object r = i1.next();
            System.out.println(r);
        }
    }
}

以上是“Java中Collection的常用方法有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI