Java笔记(11)——Collection集合

这篇博客主要介绍了Java中的Collection集合,包括如何添加单个和多个元素,使用迭代器操作集合,移除元素的方法以及集合的实用工具方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0. 先创建一个Person类为例

public class Person {
    private String name;
    private int 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;
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (!(o instanceof Person))
            return false;

        Person person = (Person) o;

        if (getAge() != person.getAge())
            return false;
        return getName() != null ? getName().equals(person.getName()) : person.getName() == null;
    }

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

1. add():添加一个元素到集合中

public void testAdd(){
    Collection<Object> collection = new ArrayList<>();
    System.out.println(collection.size()); // 0
    collection.add("AAA");
    collection.add(new Person("Tom", 12));
    collection.add(new Person("Jerry", 13));
    collection.add(new Person("Lucy", 14));
    System.out.println(collection.size()); // 4
}

2. addAll(Collection coll):添加一组元素到集合中

public void testAddAll(){
    Collection<Object> collection2 = new ArrayList<>();
    collection2.add("Citicbank");
    Collection<Object> collection = new ArrayList<>();

    collection.add("AAA");
    collection.add(new Person("Tom", 12));
    collection.add(new Person("Jerry", 13));
    collection.add(new Person("Lucy", 14));

    collection2.addAll(collection);
    System.out.println("collection2.size() = " + collection2.size());
    for(Object c : collection2){
        System.out.println(c);
    }
}
/*
collection2.size() = 5
Citicbank
AAA
Person{name='Tom', age=12}
Person{name='Jerry', age=13}
Person{name='Lucy', age=14}
*/

3. Iterator迭代器

/*
 * 在Collection 中无法获取指定元素 但可以遍历所有的元素
 * 1. 使用增强的for循环
 * 2. 使用Iterator迭代器
 * 2.1 获取迭代器对象:调用Collection中的iterator()方法,获取Iterator接口的对象
 * 2.2 调用Iterator 接口的方法进行迭代
 */
public void testIterator(){
    Collection<Object> collection = new ArrayList<>();
    collection.add("AAA");
    collection.add(new Person("Tom", 12));
    collection.add(new Person("Jerry", 13));
    collection.add(new Person("Lucy", 14));
    for(Object obj : collection){
        System.out.println(obj);
    }
    System.out.println();
    Iterator it = collection.iterator();
    while(it.hasNext()){
        Object obj = it.next();
        System.out.println(obj);
    }
    // 若下一个记录无效 还调用next()方法,则抛出NoSuchElementException异常
}
/*
AAA
Person{name='Tom', age=12}
Person{name='Jerry', age=13}
Person{name='Lucy', age=14}

AAA
Person{name='Tom', age=12}
Person{name='Jerry', age=13}
Person{name='Lucy', age=14}
*/

4. 移除元素的方法

/*
* 1. clear():清空集合
* 2. remove():移除指定的元素 通过equals()方法在集合中查找指定的元素
* 若存在 则移除
*
* 3. removeAll(Collection coll) : 移除coll中有的元素
* 4. retainAll(Collection coll) : 保存coll中有的元素
*/
public void testRemove(){
    Collection<Object> collection = new ArrayList<>(
    collection.add("AAA");
    Person p = new Person("Tom", 12);
    collection.add(p);
    collection.add(new Person("Jerry", 13));
    collection.add(new Person("Lucy", 14));
    Collection<Object> collection1 = new ArrayList<>
    collection1.add("DEF");
    collection1.add(new Person("Mike", 13));
    collection1.add(new Person("Jerry", 13));
    /*
    A.retainAll(B),A调用这个方法之后,
    集合A中只剩下存在于B中的元素,
    返回值为false表示集合A没改变,
    返回true集合A发生改变
    */
    System.out.println("返回值为:" + collection.retainAl
    // 上述代码返回true说明 collection 中的元素值发生改变。
    System.out.println("collection = " + collection)
    collection.remove(p);
    collection.remove(new Person("Lucy", 14));
    System.out.println(collection.size()); // 1
    collection.clear();
    System.out.println(collection.size()); //  0
}
/*
返回值为:true
collection = [Person{name='Jerry', age=13}]
1
0
*/

5. 工具方法

public void testToolMethod(){
    Collection<Object> collection = new ArrayList<>();

    collection.add("AAA");
    Person p = new Person("Tom", 12);
    collection.add(p);
    collection.add(new Person("Jerry", 13));
    collection.add(new Person("Lucy", 14));

    // 1. contain(Object o):  利用equals()方法比较,查看集合中有没有指定的元素
    // 重写equals方法
    boolean flag = collection.contains(new Person("Jerry", 13));
    System.out.println(flag); // true

    // 2. containsAll(Colllection<?> c): 查看集合中有没有指定元素的集合
    Collection<Object> collection2 = new ArrayList<>();
    collection2.add("ABCD");
    collection2.add(new Person("Mike", 15));

    System.out.println(collection.containsAll(collection2)); // false

    // 3. isEmpty() : 检验集合是否为空集合
    System.out.println(collection.isEmpty()); // false

    // 4. toArray(): 把集合转为 Object 对象的数组
    Object[] objs = collection.toArray();
    System.out.println(objs.length); // 4
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值