java学习日记20230410-Collection

发布时间 2023-04-09 00:29:49作者: 、子夜

Collection接口实现类的特点:

public interface Collection<E> extends Iterable<E>
  • collection实现子类可以存放多个元素,每个元素可以是object
  • 有些Collection的实现类,可以存放重复的元素,有些不可以
  • 有些Collection的实现类,有些是有序的List,有些是无序的Set
  • Collection接口没有直接的实现子类,是通过他的子接口Set和List来实现的

Collection接口和常用方法:

  ArrayList为例:

  

public class CollectionMethod {
    public static void main(String[] args) {

        //add添加元素
        ArrayList arrayList = new ArrayList();
        arrayList.add("jack");
        arrayList.add(0);
        arrayList.add(true);
        System.out.println(arrayList);
        //remove删除元素
        arrayList.remove(true);
        System.out.println(arrayList);
        //contains查找元素是否存在,返回值为boolean
        System.out.println(arrayList.contains("jack"));
        //size获取元素个数
        System.out.println(arrayList.size());
        //isEmpty判断是否为空
        System.out.println(arrayList.isEmpty());
        //clear清空
        arrayList.clear();
        System.out.println(arrayList.isEmpty());
        //addAll添加多个元素
        ArrayList arrayList1 = new ArrayList();
        arrayList1.add("三国演义");
        arrayList1.add("红楼梦");
        arrayList.addAll(arrayList1);
        System.out.println(arrayList);
        //containsAll 查找多个元素是否都存在
        System.out.println(arrayList.containsAll(arrayList1));
        //removeAll 删除多个元素
        arrayList.removeAll(arrayList1);
        System.out.println(arrayList);
    }
}

  Collection接口遍历元素的方式1-使用迭代器Iterator

    • Iterator对象称为迭代器,主要用于遍历Collection集合中的元素
    • 所有实现了Collection接口的集合类都有一个iterator()方法,用于返回一个实现了Iterator接口的对象,即可以返回一个迭代器
    • Iterator的结构
    • Iterator仅用于遍历集合,Iterator本身不存放对象
    • 迭代器执行原理:
      • Iterator iterator = coll.iterator();//得到一个集合的迭代器
      • hasNext():判断是否还有下一个元素
      • while(iterator.hasNext()) 
      • next()作用1.指针下移2.将下移以后集合位置傻姑娘的元素返回
    • 迭代器方法:
      • hasNext();
      • next();
      • remove();
    • 在调用iterator.next()方法之前必须要调用iterator.hasNext()进行检测,若不调用,且下一条记录无效,直接调用it.next()会抛出NoSuchElementException异常
  •   

    @SuppressWarnings({"all"})
    public class CollectionIterator {
        @SuppressWarnings({"all"})
        public static void main(String[] args) {
            Collection colletion = new ArrayList();
            colletion.add(new Book("三国演义", "罗贯中", 10.1));
            colletion.add(new Book("小李飞刀", "古龙", 5.1));
            colletion.add(new Book("红楼梦", "曹雪芹", 34.6));
            Iterator iterator = colletion.iterator();
            //itit 快捷键  所有快捷键ctrl + j
            while (iterator.hasNext()) {
                Object next = iterator.next();
    
            }
            iterator = colletion.iterator();//重置迭代器
            while (iterator.hasNext()) {
                Object obj = iterator.next();//向上转型
                System.out.println(obj);
            }
            //iterator.next();
            //当退出while循环后,这时iterator指向最后的元素
            //如果需要再次遍历,需要重置迭代器
        }
    }
    
    @SuppressWarnings({"all"})
    class Book {
        private String name;
    
        @Override
        public String toString() {
            return "Book{" +
                    "name='" + name + '\'' +
                    ", author='" + author + '\'' +
                    ", price=" + price +
                    '}';
        }
    
        private String author;
        private double price;
    
        public Book(String name, String author, double price) {
            this.name = name;
            this.author = author;
            this.price = price;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getAuthor() {
            return author;
        }
    
        public void setAuthor(String author) {
            this.author = author;
        }
    
        public double getPrice() {
            return price;
        }
    
        public void setPrice(double price) {
            this.price = price;
        }
    }

    Collection接口遍历对象方式2-for循环增强

    增强for循环,可以代替iterator迭代器,特点:增强for就是简化版的iterator,本质一样,只能用于遍历集合和数组

    基本语法:

    for(元素类型 元素名:集合名/数组名){
        访问元素;
    }
    public class CollectionFor {
        public static void main(String[] args) {
            Collection colletion = new ArrayList();
            colletion.add(new Book("三国演义", "罗贯中", 10.1));
            colletion.add(new Book("小李飞刀", "古龙", 5.1));
            colletion.add(new Book("红楼梦", "曹雪芹", 34.6));
            //底层也是使用迭代器
            for(Object book:colletion){
                System.out.println(book);
            }
            //快捷键 I
            for (Object o :colletion) {
    
            }
    
        }
    }