java学习(尚硅谷)集合之Collection接口中的方法

又是好几天没有学Java了,今天下午睡了个觉状态很好,来到图书馆写了点C++的项目代码,写累了看了几集康师傅。笔记如下:

Collection接口中的方法:

package com.atguigu.java;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 *  Collection接口中声明的方法的测试
 *  结论:
 *  向Collection接口实现类的对象中添加数据obj时,要求obj所在类要重写equals方法
 * @author 
 * @create 2022-03-28-19:31
 */
public class CollectionTest {

    @Test
    public void test1(){

        Collection coll=new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(new Person("Jerry",20));
        //1. contains(Object obj):判断当前集合中是否包含obj,contains调用是的实际上是equals(如果重写的话),比较的是内容
        //而不是地址,如果equals没有重写,还是用的==,比较的是地址
        //比较的过程是拿着contains括号内的obj对象,利用其所在类的equals方法去与coll中的每一个元素进行比较。

        boolean contains = coll.contains(123);
        System.out.println(contains);
        System.out.println(coll.contains(new String("Tom")));

        //2. containsAll(Collection coll1):判断形参coll1中的所有元素是否都存在于当前集合中
        Collection coll1= Arrays.asList(123,456);
        System.out.println(coll.containsAll(coll1));



        }
    @Test
    public void test2(){
        //3. remove(Object obj):从当前集合中删除obj元素
        Collection coll=new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(new Person("Jerry",20));
        coll.remove(123);
        System.out.println(coll);

        //4. removeAll(Collection coll1):差集:从当前集合中移除coll1中所有的元素,将原来集合赋值为差集部分
        Collection coll1=Arrays.asList(123,456);
        coll.removeAll(coll1);
        System.out.println(coll);
    }
    @Test
    public void test3(){
        Collection coll=new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(new Person("Jerry",20));

//        Collection coll1=Arrays.asList(123,456,789);
//
//        //5. retainAll(Collection coll1):得到原集合和coll1和交集,将原来集合赋值为交集部分
//        coll.retainAll(coll1);
//        System.out.println(coll);

        //6. equals(Object obj):要想返回true,当前集合和形参集合的元素要相同。
        Collection coll1=new ArrayList();
        coll1.add(123);
        coll1.add(456);
        coll1.add(new String("Tom"));
        coll1.add(new Person("Jerry",20));

        System.out.println(coll.equals(coll1));
    }
    @Test
    public void test4(){
        Collection coll=new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(new Person("Jerry",20));

        //7. hashCode:返回当前对象的哈希值
        System.out.println(coll.hashCode());

        //8. 集合--->数组 toArray()
        Object[] arr = coll.toArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }

        //拓展:数组--->集合:调用Arrays类的静态方法asList()
        List<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"});
        System.out.println(list);


        List<int[]> list1 = Arrays.asList(new int[]{123, 456});
        System.out.println(list1.size());//1 将后面的int型数组看作是一个元素了

        List list2 = Arrays.asList(new Integer[]{123, 456});
        System.out.println(list2.size());//2 利用Integer数组写的时候有几个元素就是几个元素

        //9. iterator():返回Iterator接口的实例,用于遍历集合元素。放在IteratorTest.java中测试


    }

}

Person类:

package com.atguigu.java;

import java.util.Objects;

/**
 * @author 
 * @create 2022-03-28-19:38
 */
public class Person {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        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;
    }

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

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

//    @Override
//    public int hashCode() {
//        return Objects.hash(name, age);
//    }
}

Iterator迭代器接口遍历集合元素:

package com.atguigu.java;

import org.junit.Test;

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

/**
 * 集合元素的遍历:使用迭代器Iterator接口
 * 1.内部的方法:hasNext()和next()
 * 2.集合对象每调用iterator()方法都会得到一个全新的迭代器对象,默认游标都在集合第一个元素之前
 * 3.内部定义了remove(),可以在遍历的时候,删除集合中的元素。此方法不同于集合直接调用remove()
 * @author 
 * @create 2022-03-28-20:30
 */
public class IteratorTest {
    @Test
    public void test1() {
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(new Person("Jerry", 20));

        Iterator iterator = coll.iterator();
        //方式一:
//        System.out.println(iterator.next());
//        System.out.println(iterator.next());
//        System.out.println(iterator.next());
//        System.out.println(iterator.next());
//        //报异常:NoSuchElementException
//        //System.out.println(iterator.next());

        //方式二:不推荐
//        for (int i = 0; i < coll.size(); i++) {
//            System.out.println(iterator.next());
//        }

        //方式三:推荐
        while (iterator.hasNext()) {
            //next():①指针下移 ②将下移以后集合位置上的元素返回
            System.out.println(iterator.next());
        }
    }
    @Test
    public void test2(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(new Person("Jerry", 20));
        Iterator iterator = coll.iterator();

//        //错误方式一:输出的next是判断中的next的下一个元素
//        while ((iterator.next())!=null){
//            System.out.println(iterator.next());
//        }
        //错误方式二:每调用coll中的iterator一次相当于重新创建了一个iterator,到coll的开头前一个位置
        while (coll.iterator().hasNext()){
            System.out.println(coll.iterator().next());//死循环,循环输出coll的第一个元素123
        }


    }
    //测试Iterator中的remove()
    //如果还未调用next()或在上一次调用next方法之后已经调用了remove方法
    //再调remove都会报IllegalStateException。
    @Test
    public void test3(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(new Person("Jerry", 20));

        //删除集合中"Tom"
        Iterator iterator = coll.iterator();
        while (iterator.hasNext()){
            //iterator.remove();//IllegalStateException,不能在next()之前调用remove()
            Object obj = iterator.next();
            if ("Tom".equals(obj)){
                iterator.remove();
            }
        }
        //遍历集合
       iterator = coll.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值