您好,欢迎访问代理记账网站
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

java--Collection接口与List、Set接口详解

一、几种接口或类的关系

在这里插入图片描述

二、常用方法概览

1、Collection接口常用方法:

add(Object obj)addAll(Collection c)
remove(Object obj)removeAll(Collection c)clear()
遍历增强for循环:for (Object obj:list) {System.out.println(obj);}迭代器:Iterator iterator = list.iterator();while (iterator.hasNext()) {System.out.println(iterator.next());}
判断contains(Object obj)containsAll(Collection c) equals(Collection c)isEmpty()retainAll(Collection c) 交集是否为空
转换toArray() Arrays.asList()
长度size()

2、List接口常用方法(加粗为基于Collection的扩展):

add(Object obj)addAll(Collection c)add(int index,Object obj)add(int index,Collection c)
remove(Object obj)removeAll(Collection c)clear()remove(int index)
set(int index,Object obj)
get(int index)subList()
遍历增强for循环:for (Object obj:list) {System.out.println(obj);}迭代器:Iterator iterator = list.iterator();while (iterator.hasNext()) {System.out.println(iterator.next());}普通循环for (int i = 0; i < list.size(); i++) {System.out.println(list.get(i));}
判断contains(Object obj)containsAll(Collection c) equals(Collection c)isEmpty()retainAll(Collection c) 交集是否为空
转换toArray() Arrays.asList()
长度size()

3、Set 与其实现类的方法全部来自Collection,无扩展方法

三、Set实现原理

HashSet:
在这里插入图片描述
LinkedHashSet:
在这里插入图片描述
TreeSet:左大右小
在这里插入图片描述

四、示例代码:

Collection接口:

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

class JavaStudy {
    public static void main(String[] args) {
        CollectionTest value = new CollectionTest("yunweixiaocai", 29);
        Collection coll = new ArrayList();
        System.out.println("************");
        coll.add(value);                                // add()添加单个元素
        System.out.println("add\t\t"+value.getName());
        System.out.println("size\t\t"+coll.size());                // size()元素个数

        Collection coll01 = new ArrayList();
        System.out.println("************");

        coll01.addAll(coll);                            //addAll()添加其他集合内所有元素
        System.out.println("isEmpty\t\t"+coll01.isEmpty());           //isEmpty()判断是否为空
        coll01.clear();                                 //clear()清空集合
        System.out.println("clear\t\t"+coll01.isEmpty());

        System.out.println("************");
        coll01.add(value);
        System.out.println("contains\t\t"+coll01.contains("value01")); //contains()判断是否包含,通过参数对象的equals()方法判断
        System.out.println("containsAll\t\t"+coll01.containsAll(coll));   //containsAll()判断是否包含集合参数内所有的对象
        System.out.println("equals\t\t"+coll01.equals(coll));         //equals()判断是所有对象斗鱼参数集合内对象相同

        System.out.println("************");
        Boolean b = coll01.remove(new CollectionTest("yunweixiaocai", 29));       //remove()移除元素,没有该元素返回false,需重写equals()
        System.out.println("remove\t\t\t"+coll01.contains(value));
        Boolean b1 = coll01.removeAll(coll);                                                   //()移除集合参数的所有元素,没有该元素返回false

        coll01.add(1);
        coll01.add(2);
        coll01.add(3);
        coll.add(2);
        coll.add(3);
        coll.add(4);
        System.out.println("retainAll\t\t"+coll.retainAll(coll01)); //retainAll(),交集是否非空,集合参数的交集赋值给对象coll(保留与参数相同的值)
        System.out.println("coll\t\t\t"+coll);
        Collection coll02 = new ArrayList();
        coll02.add(4);
        coll02.add(5);
        coll02.add(6);
        System.out.println("retainAll\t\t"+coll02.retainAll(coll));

        System.out.println("hashCode\t\t"+coll01.hashCode()); //hashCode()

        System.out.println("************");
        Object[] objects = coll.toArray();      // toArray()转换为数组
        for (Object object : objects) {
            System.out.println("toArray\t\t\t"+object);
        }
        List<CollectionTest> collectionTests = Arrays.asList(new CollectionTest[]{new CollectionTest("linjunjie", 39),new CollectionTest("zhoujielun", 40)});
        System.out.println("Arrays.asList\t"+collectionTests);    //Arrays.asList()数组转换为集合
        List<String> strings = Arrays.asList(new String[]{"AA", "BB", "CC"});
        System.out.println("Arrays.asList\t"+strings);            //Arrays.asList()数组转换为集合
        List<int[]> ints = Arrays.asList(new int[]{1, 2, 3});       //基本类型数组会被识别为一个元素
        System.out.println("基本类型数组\t\t"+ints);
    }
}

class CollectionTest{
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

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


    @Override
    public boolean equals(Object obj) {
        CollectionTest collectionTest;
        if (obj instanceof CollectionTest){
            collectionTest = (CollectionTest)obj;
            return this.name == collectionTest.getName();
        }
        return false;
    }
}
// 输出
************
add				yunweixiaocai
size			1
************
isEmpty			false
clear			true
************
contains		false
containsAll		true
equals			true
************
remove			false
retainAll		true
coll			[2, 3]
retainAll		true
hashCode		30817
************
toArray			2
toArray			3
Arrays.asList	[CollectionTest@1b2c6ec2, CollectionTest@4edde6e5]
Arrays.asList	[AA, BB, CC]
基本类型数组		[[I@70177ecd]

集合遍历(iterator与增强for循环)

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

class JavaStudy {
    public static void main(String[] args) {
        CollectionTest value0 = new CollectionTest("yunweixiaocai0", 9);
        CollectionTest value1 = new CollectionTest("yunweixiaocai1", 19);
        CollectionTest value2 = new CollectionTest("yunweixiaocai2", 29);
        CollectionTest value3 = new CollectionTest("yunweixiaocai3", 39);

        List list = new ArrayList<>();
        list.add(value0);
        list.add(value1);
        list.add(value2);
        list.add(value3);
		// 获取迭代器iterator
        Iterator iterator = list.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        System.out.println("**********");
        iterator = list.iterator();
        for (int i = 0; i < list.size(); i++) {
            System.out.println(iterator.next());
        }

		iterator = list.iterator();
        CollectionTest value4;
        while (iterator.hasNext()){
            Object next = iterator.next();
            if(next instanceof CollectionTest) {
                value4 = (CollectionTest) next;
                if (value4.getName().equals("yunweixiaocai1")) {
                    iterator.remove(); // 迭代器的remove删除当前对象
                }
            }
        }
        System.out.println("**********");
        System.out.println(list);
        
        System.out.println("*****增强for循环*****");
        for (Object obj:list) {
            System.out.println(obj);
        }
    }
    }
}

class CollectionTest{
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

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

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


}
// 输出:
CollectionTest{name='yunweixiaocai0', age=9}
CollectionTest{name='yunweixiaocai1', age=19}
CollectionTest{name='yunweixiaocai2', age=29}
CollectionTest{name='yunweixiaocai3', age=39}
**********
CollectionTest{name='yunweixiaocai0', age=9}
CollectionTest{name='yunweixiaocai1', age=19}
CollectionTest{name='yunweixiaocai2', age=29}
CollectionTest{name='yunweixiaocai3', age=39}
**********
[CollectionTest{name='yunweixiaocai0', age=9}, CollectionTest{name='yunweixiaocai2', age=29}, CollectionTest{name='yunweixiaocai3', age=39}]
*****增强for循环*****
CollectionTest{name='yunweixiaocai0', age=9}
CollectionTest{name='yunweixiaocai2', age=29}
CollectionTest{name='yunweixiaocai3', age=39}

List接口:

import java.util.ArrayList;
import java.util.List;

class JavaStudy {
    public static void main(String[] args) {
        List list = new ArrayList<>();
        list.add(10);
        list.add(0);
        list.add(true);
        list.add("abc");
        list.add(123);

        System.out.println(list.isEmpty());
        list.add(2,9996); //add 在index位置插入
        System.out.println("add: \t\t\t"+list);
        System.out.println("get: \t\t\t"+list.get(3)); // get获取元素
        System.out.println("indexOf: \t\t"+list.indexOf("abc")); // indexOf()返回元素所在位置
        System.out.println("lastIndexOf: \t"+list.lastIndexOf(123)); // lastIndexOf()返回元素最后一次出现位置
        System.out.println("remove: \t\t"+list.remove(4));  // remove()删除指定索引位置的元素
        System.out.println("remove: \t\t"+list);
        list.set(0, 999);
        System.out.println("set: \t\t\t"+list);
        System.out.println("subList: \t\t"+list.subList(1, 3)); // subList()

        for (Object o : list) {
            System.out.println("for\t\t"+o);
        }
    }
}
// 输出:
false
add: 			[10, 0, 9996, true, abc, 123]
get: 			true
indexOf: 		4
lastIndexOf: 	5
remove: 		abc
remove: 		[10, 0, 9996, true, 123]
set: 			[999, 0, 9996, true, 123]
subList: 		[0, 9996]
for		999
for		0
for		9996
for		true
for		123

分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进