JAva集合の接続


詳細
余計なことは言わない
直接コード、注釈解釈すべて
 
       Collection c = new ArrayList();
        //      item
        c.add(100);
        c.add("name");
        c.add("age");
        c.add("resunly");
        c.add("scan");

        //    
        System.out.println("CollectionExample c.size(): " + c.size());
        System.out.println("CollectionExample c.toString(): " + c.toString());
        //         item,        ,         equeal     ,     
        System.out.println("CollectionExample c.contains: " + c.contains("name"));

        //     
        Iterator it = c.iterator();
        while (it.hasNext()) {
            System.out.println(" c.iterator() C.items:" + it.next().toString());
        }

        List list = new ArrayList();
        list.add("name");
        list.add("addr");
        //              ,      
        System.out.println("c.containsAll: " + c.containsAll(list));

        //      
        c.addAll(list);
        System.out.println("after add list: " + c.toString());
        

特別注意
 
1.removeAll(list)
       //removeAll(list)
        //1.  list      ,        list items     true;
        //2.  list       ,        false;
        List ss = new ArrayList();
        ss.add("test");
        ss.add("fox");
        System.out.println("removeAll(list):"+c.removeAll(ss));

2.retainAll(list)
        //retainAll(list)     removeAll(list)        
        // 1.         list    ,       list    ,    true,
        // 2.         list    ,  false.       。
        System.out.println("c.retainAll "+c.retainAll(list));
        System.out.println("c.retainAll(list)" + c.toString());
        //    
        c.clear();
        System.out.println("after c.clear(): " + c.size());

 3.コレクション内のオブジェクトcontainsの使用例.
コレクション内のitemがオブジェクトである場合、contains()およびcontainsAll()メソッドを使用する場合は、オブジェクトequalメソッドを実装します.
彼らが呼び出したのはオブジェクト自身のequalメソッドを比較したものです
Info.java
public class Info{
    private String title;
    private String link;
    private String count;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;  //1.            true
        if (!(o instanceof Info)) return false;  //2.               false;

        Info info = (Info) o; //3.   Object  Info    

        if (count != null ? !count.equals(info.count) : info.count != null) return false;
        return true;
    }

    @Override
    public int hashCode() {
        int result = title != null ? title.hashCode() : 0;
        result = 31 * result + (link != null ? link.hashCode() : 0);
        result = 31 * result + (count != null ? count.hashCode() : 0);
        return result;
    }

    public Info(String title, String link, String count) {
        this.title = title;
        this.link = link;
        this.count = count;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getCount() {
        return count;
    }

    public void setCount(String count) {
        this.count = count;
    }


}

 
//      items contains     
        Collection cc = new ArrayList();
        //    
        cc.add(new Info("sum","baidu.com","100"));
        cc.add(new Info("com","test.com","5100"));
        cc.add(new Info("qiang","sina.com","60"));

        List listc = new ArrayList();
        listc.add(new Info("sum","baidu.com","100"));
        listc.add(new Info("com","test.com","5100"));
        listc.add(new Info("qiang","sina.com","60"));

        System.out.println("Collection containsAll()      :"+cc.containsAll(listc));
        //return true