Java基础之Collection

1.collection集合的概述

collection集合的概述

  • 集合是java中用来存储数据的容器
  • collection是java集合中按照存储结构分类的单列集合:java.util.Collection
  • Collection是单列集合的根接口,用于存储一系列符合规则的数据。Collection集合下有两个子接口:分别是java.util.Listjava.util.Set.
  • List中存储数据有序,可重复
  • list的实现类主要有java.util.ArrayListjava.util.LinkedList
  • Set中存储的数据无序,不可重复
  • set的实现类主要有:java.util.HashSetjava.util.TreeSet

collection大致图例

java中collection是接口 java的collection_迭代

2.Collection常用方法
  • 由于collection是list和set的父接口,所以collection中的定义了一些list和set都会有共有方法,用来操作集合中的元素。
  • public boolean add(E e): 把给定的对象添加到当前集合中 。
  • public void clear() :清空集合中所有的元素。
  • public boolean remove(E e) : 把给定的对象在当前集合中删除。
  • public boolean contains(E e) : 判断当前集合中是否包含给定的对象。
  • public boolean isEmpty() : 判断当前集合是否为空。
  • public int size() : 返回集合中元素的个数。
  • public Object[] toArray() : 把集合中的元素,存储到数组中。
public static void main(String[] args) {
        Collection<Integer> coll = new ArrayList<>();
        coll.add(1);
        coll.add(2);
        coll.add(3);
        //public boolean add(E e) : 把给定的对象添加到当前集合中 。
        boolean b= coll.add(2);
        System.out.println(b);//true
        //public boolean remove(E e) : 把给定的对象在当前集合中删除。
        boolean remove = coll.remove(3);
        System.out.println(remove);//true
        System.out.println(coll);//1,2,2
        //public boolean contains(E e) : 判断当前集合中是否包含给定的对象。
        boolean contains = coll.contains(4);
        System.out.println(contains);//false
        //public boolean isEmpty() : 判断当前集合是否为空。
        boolean empty = coll.isEmpty();
        System.out.println(empty);//false
        //public int size() : 返回集合中元素的个数。
        int size = coll.size();
        System.out.println(size);//3
        //public void clear() :清空集合中所有的元素。
        coll.clear();
        System.out.println(coll);//[ ]
    }
3.Iterator接口(迭代器)

在集合中往往由于添加了大量的元素,我们需要遍历这些元素,就需要用到Iterator这个接口。Iterator接口主要用来循环遍历Collection集合的元素。

  • 迭代
  • collection集合通过迭代的方法获取所有的元素。每次在去元素之前需要判断是否还有下一个元素,有的话就取出,直到取出所有的元素就停止。
  • Iterator常用方法
  • public E next() :返回迭代的下一个元素。
  • public boolean hasNext() :如果仍有元素可以迭代,则返回 true。
public static void main(String[] args) {
        Collection coll = new ArrayList();
        coll.add("熊大");
        coll.add("熊二");
        coll.add("熊三");
        coll.add("熊小小");
        //声明Iterator对象
        Iterator<String> iterator = coll.iterator();
        //迭代获取
        while (iterator.hasNext()){
            String next = iterator.next();
            System.out.println(next);
        }
    }
  • 迭代器实现原理
  • 使用迭代器遍历集合的过程中,首先先用集合对象调用集合的iterator()方法获取迭代器对象
  • 然后使用hasNext()判断是否有下一个元素
    如果存在使用next()方法取出元素,直到没有新的元素然后结束。
4.增强for循环实现集合遍历
  • 增强for循环格式
for (集合元素数据类型 变量: 集合或数组名称
             ) {
            //实现代码
       }
public static void main(String[] args) {
        Collection<String> coll = new ArrayList<>();
        coll.add("熊大");
        coll.add("熊二");
        coll.add("熊三");
        coll.add("熊小小");

        for ( String name: coll
             ) {
            //实现代码
            System.out.println(name);

        }
    }