Java collection classes --Collection Interface

A, Java collection classes

1, the diagram in Java collection class
Here Insert Picture Description

2, the role of collections

  • Java collection class is to facilitate the operation of the plurality of objects (object-oriented)

3, arrays and collections are similar containers, What's the difference?

  • Array: the basic data types stored in the array, fixed length, may be stored basic data types (int), may store the reference data type (Integer)
  • Collections: a collection of storage objects only, variable length, can only store a reference type (Integer)

4, the characteristics of collections

  • You can only store a collection of objects
  • The collection length is variable
  • Collection of different types of objects may be stored

Two, Collection Interface

1, Collection Interface Overview

  • Collection: Collection
  • Root interface Collection hierarchy
  • Collection represents a group of objects, which are also called collection element
  • Collection is the top set of interfaces and its sub-systems have repeated, there is a unique, there is orderly, there is disorder

2, Collection interface member method

  • boolean add(E e)// add an element
  • boolean remove(Object o)
  • void clear()// remove all elements
  • boolean contains(Object o)// determines whether the collection contains the specified elements
  • boolean isEmpty()
package shouji;

import java.util.ArrayList;
import java.util.Collection;

public class CollectionDemo {
    public static void main(String[] args){
        //Collection c = new Collection();//错误的,接口不能实例化
        Collection c = new ArrayList();
        System.out.println(c.add("hello"));
        c.add("world");//添加
        c.add("java");
        System.out.println(c);
        c.clear();//清空
        System.out.println(c);
        System.out.println("-------------------------");
        c.add("苍井空");//添加
        c.add("波多野结衣");
        System.out.println(c);
        System.out.println(c.remove("苍井空"));//移除一个元素
        System.out.println(c);
        System.out.println("++++++++++++++++++++++++++++++");
        System.out.println(c.contains("苍井空"));//判断集合中是否包含苍井空
        System.out.println(c.contains("波多野结衣"));//判断集合中是否包含波多野结衣
        System.out.println(c.isEmpty());//判断集合是否为空
        System.out.println(c.size());//判断元素的个数
    }
}
  • int size()
  • boolean addAll(Collection c)
  • boolean removeAll(Collection c)
  • boolean containsAll(Collection c)
  • boolean retainAll(Collection c)
package shouji;

import java.util.ArrayList;
import java.util.Collection;

public class CollectionDemo {
    public static void main(String[] args){
        Collection c1 = new ArrayList();//创建集合c1
        c1.add("苍井空");
        c1.add("波多野结衣");
        c1.add("小泽玛利亚");
        c1.add("泷泽萝拉");

        Collection c2 = new ArrayList();//创建集合 c2
        c2.add("17岁");
        c2.add("18岁");
        c2.add("19岁");
        c2.add("20岁");
        c2.add("21岁");
        c2.add("22岁");
        c2.add("23岁");
        c2.add("24岁");
        System.out.println(c1.addAll(c2));
        System.out.println(c1);
        System.out.println(c2);
        System.out.println("-----------------------");
        System.out.println(c1.removeAll(c2));
        System.out.println(c1);
        System.out.println(c2);
        System.out.println("===========================");
        System.out.println(c1.containsAll(c2));//c1集合是否包含c2
    }
}
  • Object[] toArray()// set to turn into an array, can be achieved through the collection of
package shouji;

import java.util.ArrayList;
import java.util.Collection;

public class CollectionDemo {
    public static void main(String[] args){
        Collection c1 = new ArrayList();//创建集合c1
        c1.add("苍井空");
        c1.add("波多野结衣");
        c1.add("小泽玛利亚");
        c1.add("泷泽萝拉");
        Object[] obj = c1.toArray();
        for (int i = 0; i < obj.length; i++){
            String s = (String) obj[i];
            System.out.println(s);
        }
    }
}
  • Iterator iterator()// iterator, a collection of special traversal

New two Java files

package shouji;

public class Student {
    private String name;
    private int age;
    public Student(){
        super();
    }
    public Student(String name, int age){
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
package shouji;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionDemo {
    public static void main(String[] args){
        Collection c1 = new ArrayList();//创建集合c1
        Student s1 = new Student("苍井空",17);
        Student s2 = new Student("小泽玛利亚",18);
        Student s3 = new Student("波多野结衣",19);
        Student s4 = new Student("泷泽萝拉",20);
        Student s5 = new Student("水菜丽",21);
        Student s6 = new Student("刘诗诗",22);
        Student s7 = new Student("范冰冰",23);

        c1.add(s1);//把人添加到集合里
        c1.add(s2);
        c1.add(s3);
        c1.add(s4);
        c1.add(s5);
        c1.add(s6);
        c1.add(s7);

        Iterator it = c1.iterator();//迭代器,集合的专用遍历方式
        while (it.hasNext()){
            Student s = (Student) it.next();
            System.out.println("姓名:"+s.getName()+"\t 年龄:"+s.getAge());
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_43860260/article/details/91461433