Java方法containsAll學習

來源:互聯網
上載者:User

標籤:實現   nsa   public   pac   判斷   代碼   print   imp   als   

有時候我們需要判斷B鏈表是不是A鏈表的子集,我們可以使用A.containsAll(B)來判斷,當傳回值是true的時候就表明B鏈表是A鏈表的子集,當傳回值是false時候就表明B鏈表不是A鏈表的子集。 

實驗代碼如下:

package learnIng;import java.util.ArrayList;public class Cationsall {    public static void main(String[] args) {        ArrayList<String> als = new ArrayList<String>();        als.add("a");        als.add("b");        ArrayList<String> alss = new ArrayList<String>();        alss.add("a");        alss.add("c");        System.out.println(als.containsAll(alss));    }}

實驗結果:false

 

實驗代碼如下:

package learnIng;import java.util.ArrayList;public class Cationsall {    public static void main(String[] args) {        ArrayList<String> als = new ArrayList<String>();        als.add("a");        als.add("b");        ArrayList<String> alss = new ArrayList<String>();        alss.add("a");        System.out.println(als.containsAll(alss));    }}

實驗結果:true

 

我們看下原始碼瞭解containsALL的實現。

public boolean containsAll(Collection<?> c) {    Iterator<?> e = c.iterator();    while (e.hasNext())        if (!contains(e.next()))        return false;    return true;}

 

Java方法containsAll學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.