Java-Collection集合

1.Collection集合

1.1 集合概述:

集合可以根据存放各种各样的数据,每种数据的长度,大小以及自身的特点都是不一样的 所以,java中集合也不应该是单一的,我们应该针对不同的需要,使用java提供的不同的集合类这么的集合,他们的底层数据结构也是不同的,不同没有关系,我们只要知道集合是用来存储元素的,不光可以进行存储,还可以进行查找,删除,判断,等等。

++集合继承体系++集合继承体系:不同的集合不断地向上提取,这些不同集合应该有着某种共性,我们根据共性向上提取,最终会得到一个集合继承体系

1.2集合的各种功能:各功能的第一个为集合中单个的修改,第二个为集合对集合的操作;

  • 1、添加功能:
    boolean add(Object e) 确保此集合包含指定的元素(可选操作)。
    boolean addAll(Collection c) 将指定集合中的所有元素添加到此集合(可选操作)。
  • 2、删除功能:
    boolean remove(Object o) 从该集合中删除指定元素的单个实例(如果存在)(可选操作)。
    boolean removeAll(Collection c) 删除指定集合中包含的所有此集合的元素(可选操作)。
    void clear() 从此集合中删除所有元素(可选操作)。
  • 3、获取功能:
    Iterator iterator() 返回此集合中的元素的迭代器。
  • 4、判断功能:
    boolean contains(Object o) 如果此集合包含指定的元素,则返回 true 。
    boolean containsAll(Collection c) 如果此集合包含指定 集合中的所有元素,则返回true。
    boolean isEmpty() 如果此集合不包含元素,则返回 true 。
  • 5、int size() 返回此集合中的元素数。
  • 6、求交集
    boolean retainAll(Collection c) 仅保留此集合中包含在指定集合中的元素(可选操作)。
  • 7、将集合转换成数组
    Object[] toArray() 返回一个包含此集合中所有元素的数组。
package com.bigdat.java.day19;
import java.util.ArrayList;
import java.util.Collection;
/*
            1、添加功能:
                boolean add(Object e) 确保此集合包含指定的元素(可选操作)。
                boolean addAll(Collection c) 将指定集合中的所有元素添加到此集合(可选操作)。
            2、删除功能:
                boolean remove(Object o) 从该集合中删除指定元素的单个实例(如果存在)(可选操作)。
                boolean removeAll(Collection c) 删除指定集合中包含的所有此集合的元素(可选操作)。
                void clear() 从此集合中删除所有元素(可选操作)。
            3、获取功能:
                Iterator iterator() 返回此集合中的元素的迭代器。
            4、判断功能:
                boolean contains(Object o) 如果此集合包含指定的元素,则返回 true 。
                boolean containsAll(Collection c) 如果此集合包含指定 集合中的所有元素,则返回true。
                boolean isEmpty() 如果此集合不包含元素,则返回 true 。
            5、int size() 返回此集合中的元素数。
            6、求交集
                boolean retainAll(Collection c) 仅保留此集合中包含在指定集合中的元素(可选操作)。
            7、将集合转换成数组
                Object[] toArray() 返回一个包含此集合中所有元素的数组。
         本次练习中只练习各个功能的第一个:
 */

public class CollectionDemo1 {
    public static void main(String[] args) {
        //由于Collection是一个接口,我们需要借助一个实现子类来创建对象,这里先用ArrayList举例使用Collection里面的方法
        //接口多态
        Collection c = new ArrayList();
        //添加功能
        // 通过调用两次添加相同的元素方法,发现都是true,大致上我们推测出ArrayList集合是允许元素重复的
        System.out.println(c.add("hello"));//返回值为true
        c.add("java");
        c.add("world");
        c.add("hadoop");
        c.add("hive");
        c.add("hive");
        c.add(34);
        c.add(343.55);
        System.out.println(c);// 重写了toString() 可以看出当前集合中存的元素
        //删除功能
        c.remove("java");
        System.out.println(c);
        //全部清除功能
//        c.clear();
//        System.out.println(c);
        //判断功能
        boolean j = c.contains("hadoop");
        System.out.println(j);
        boolean e = c.isEmpty();//如果此集合包含指定的元素,则返回 true 。
        System.out.println(e);
        int size = c.size();//获取元素个数
        System.out.println(size);
          /*
                java.lang.Object
                    java.util.AbstractCollection(最终确定在该抽象类中进行方法的重写)
                            public String toString()
                            返回此集合的字符串表示形式。
                            字符串表示由集合的元素的列表按照它们的迭代器返回的顺序包含在方括号( "[]" )中。
                            相邻元素由字符", " (逗号和空格)分隔。
                        java.util.AbstractList<E>
                            java.util.ArrayList<E>
         */
    }
}

1.3集合对集合之间的操作

  • boolean addAll(Collection c) 将指定集合中的所有元素添加到此集合(可选操作)。
  • boolean removeAll(Collection c) 删除指定集合中包含的所有此集合的元素(可选操作)。////在C中删除了c和c1的并集
  • boolean containsAll(Collection c) 如果此集合包含指定 集合中的所有元素,则返回true。
  • boolean retainAll(Collection c) 仅保留此集合中包含在指定集合中的元素(可选操作)。//即保留并集

总结:总结:对两个集合进行操作时,哪个集合在前,就是对哪个集合操作。造作结果返回到前面的集合中,后面的不变。

package com.bigdat.java.day19;
import java.util.ArrayList;
import java.util.Collection;

/*
    boolean addAll(Collection c) 将指定集合中的所有元素添加到此集合(可选操作)。
    boolean removeAll(Collection c) 删除指定集合中包含的所有此集合的元素(可选操作)。
    boolean containsAll(Collection c) 如果此集合包含指定 集合中的所有元素,则返回true。
    boolean retainAll(Collection c) 仅保留此集合中包含在指定集合中的元素(可选操作)。
 */
public class CollectionDemo2 {
    public static void main(String[] args) {
        Collection c = new ArrayList();
        Collection c1 = new ArrayList();
        c.add("hello");
        c.add("world");
        c.add("java");
        c.add("hadoop");
        c.add("hive");
        c.add("bigdata");

        c1.add("hello");
        c1.add("world");
        c1.add("java");
        c1.add("hadoop");
        c1.add("spark");
        c1.add("flink");
        //boolean addAll(Collection c) 将指定集合中的所有元素添加到此集合(可选操作)。
//        c.addAll(c1);
        System.out.println("c中的结果为:"+c);//c中的结果为:[hello, world, java, hadoop, hive, bigdata, hello, world, java, hadoop, spark, flink]
        System.out.println("c1中的结果为:"+c1);//c1中的结果为:[hello, world, java, hadoop, spark, flink]
        System.out.println("=====================================================================");
        //boolean removeAll(Collection c) 删除指定集合中包含的所有此集合的元素(可选操作)。
//        c.removeAll(c1);
        System.out.println("c中的结果为:"+c);//在C中删除了c和c1的并集  c中结果为[hive, bigdata]
        System.out.println("c1中的结果为:"+c1);//c1中结果不变
        System.out.println("=======================================================================");
        //boolean containsAll(Collection c) 如果此集合包含指定 集合中的所有元素,则返回true。
        System.out.println(c.containsAll(c1));//即c中包含c1的所有元素;就返回ture
        //boolean retainAll(Collection c) 仅保留此集合中包含在指定集合中的元素(可选操作)。
        c.retainAll(c1);
        System.out.println("c中的结果为:"+c);//即保留并集
        System.out.println("c1中的结果为:"+c1);

        //总结:对两个集合进行操作时,哪个集合在前,就是对哪个集合操作。造作结果返回到前面的集合中,后面的不变。
    }
}

1.4遍历集合Collection的三种方法

  • toArray()
  • 迭代器(iterator遍历)
  • 增强for循环
    1.toArray()
package com.bigdat.java.day19;
import java.util.ArrayList;
import java.util.Collection;
/*
        需求:遍历集合获取集合中每一个元素并使用
        Collection集合遍历有三种方式:
            1、toArray()
            2、迭代器遍历
            3、增强for循环
       1、Object[] toArray()返回一个包含此集合中所有元素的数组。
       如果此集合对其迭代器返回的元素的顺序做出任何保证,则此方法必须以相同的顺序返回元素。
 */
public class CollectionDwmo3 {
    public static void main(String[] args) {
        Collection c = new ArrayList();
        //向集合中增加元素
        c.add("刘德华");
        c.add("郭富城");
        c.add("张学友");
        c.add("黎明");
        //1. toArray()方法:
        Object[] a = c.toArray();
        System.out.println(a);
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
            //因为获取的元素是Object类型的
            //而Object类型中没有length方法,所以会报错
            //想要使用String类型中的Length方法,就必须向下转型;
            String s = (String)a[i];
            System.out.println(s+"-的长度为:"+s.length());
        }
    }
}

2

package com.bigdat.java.day19;
import java.util.ArrayList;
import java.util.Collection;
/*
        向Collection集合中添加学生对象并遍历使用
 */
class Student1{
    private String name;
    private int age;

    public Student1() {
    }
    public Student1(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", 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;
    }
}
public class CollectionDemo4 {
    public static void main(String[] args) {
        Collection c = new ArrayList();
        Student1 n = new Student1("小花花",19);
        Student1 n1 = new Student1("灰太狼",49);
        Student1 n2 = new Student1("红太狼",38);
        c.add(n);
        c.add(n1);
        c.add(n2);
        Object[] object =c.toArray();
        for (int i = 0; i < object.length; i++) {
           Student1 s = (Student1) object[i];//向下转型为Student,就可以使用Student1类中的方法
            System.out.println(object);//[Ljava.lang.Object;@1b6d3586 没有重写,只是一串地址值
            System.out.println(s);//Student{name='小花花', age=19}
            System.out.println(s.getName()+"''''''''''"+s.getAge());//小花花''''''''''19
        }

    }

}

2迭代器iteraror

package com.bigdat.java.day19;

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

/*
    遍历Collection集合的第二种方式:迭代器遍历
 */
public class CollectionDemo5 {
    public static void main(String[] args) {
        Collection c = new ArrayList();
        c.add("安徽");
        c.add("北京");
        c.add("南京");
        c.add("湖北");
        c.add("云南");
        c.add("杭州");
        //创建一个 iterator 的迭代器
        Iterator iterator = c.iterator();
        while(iterator.hasNext()) {
            Object obj = iterator.next();
            //转型这一步为了获取每个数据的长度
            String s = (String)obj;
            System.out.println(s+" 的长度为:"+s.length());
        }
    }
}

3.增强for循环

package com.bigdat.java.day19;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class CollectionDwmo6 {
    public static void main(String[] args) {
        Collection c = new ArrayList();
        Student1 s1 = new Student1("黑小虎",29);
        Student1 s2 = new Student1("沸羊羊",20);
        Student1 s3 = new Student1("双面龟",18);
        c.add(s1);
        c.add(s2);
        c.add(s3);
//        System.out.println(c);
        //创建迭代器对象
        Iterator iterator = c.iterator();
        while (iterator.hasNext()) {
//            System.out.println(next.next());
            Object obj = iterator.next();
            System.out.println(obj);
            Student1 s = (Student1) obj;
            System.out.println(s);
            System.out.println(s.getName()+"============"+s.getAge());
        }
//        Iterator iterator1 = c.iterator();
//        while(iterator1.hasNext()){
//            Object obj = iterator1.next();
//            Student1 s5 = (Student1) obj;
//            System.out.println(s5.getName()+"==========="+s5.getAge());
//        }
                /*
                for(初始化语句;判断条件语句;控制条件语句){
                    循环体内容;
                }
         */
    }
}

posted @ 2022-03-30 09:22  a-tao必须奥利给  阅读(36)  评论(0)    收藏  举报