7.Java中的集合

本文深入探讨了Java中的集合框架,包括Collection和Iterator的基本概念,List接口的遍历和练习,并发修改异常的原因及解决方案,增强for循环的使用,以及Set和Map集合的特性与实践操作。特别关注了HashSet的唯一性保证和Map的遍历方法。
摘要由CSDN通过智能技术生成

Java中的集合

集合类概述

为什么出现集合类?
	我们学习的是面向对象的编程语言,面向对象的编程语言对事物的描述都是通过对象体现的,
为了方便对多个对象进行操作,我们就必须把这多个对象进行存储,而要想存储多个对象,就不能
是基本的变量了,应该是一个容器类型的变量。

回顾我们学习过的知识,有哪些容器类型的呢?
	数组,StringBuilder
	首先说StringBuilder,它的结果是一个字符串,不一定满足我们的需求
	数组的长度

	而数组的长度固定,不能适应变化的需求,在这种情况下Java提供集合类给我们使用
	由此可见集合类的长度是可变的。

集合类的特点:长度可变。
	Collection:是单列集合的顶层接口。
	Collection表示一组对象,这些对象也称为collection的元素。
	一些colloection允许有重复的元素,而另一些则是无序的。
	JDK 不提供此接口的任何实现:它提供更加具体的子接口(Set,List)

创建Collection集合的对,我们采用的是多态的方式,使用的是具体的ArrayList类
因为这个类是最常用的集合类
ArrayList()

Collection<E>:
	<E>:是一种特殊的数据类型,泛型,这里我们会使用就可以了
	如何使用?
		在出现E的地方使用引用数据类型替换即可’、
		举例Collection<String>/Collection<Student>
		
集合类体系结构

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200314034022202.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3JpbmltYTQzOA==,size_16,color_FFFFFF,t_70

Collection与迭代器

Collection集合概述:
	是单例集合的顶层接口,Collection表示一组对象,这些对象也称为collection的元素
	JDK不提供此接口的任何直接实现:它提供更具体的子接口(Set和List)实现。
	创建Collection集合对象
		多态的方式
		具体类的实现类ArrayList()
	添加元素
		add(E e)
public class CollectionDemo {
   
    public static void main(String[] args) {
   
        //创建Collection集合对象
        //JDK7的新特性,看懂就可以
        //Collection<String> c = new ArrayList<>();//多态的方式(不建议使用)
        Collection<String> c = new ArrayList<String>();//多态的方式
        //boolean add(E e) 添加元素
        c.add("hello");
        c.add("world");
        c.add("java");
        System.out.println(c);
        // 输出了集合中的元素按照指定格式拼接的内容,说明ArrayList重写了toString()方法
    }
}

Collection集合的成员方法

public class CollectionDemo {
   
    public static void main(String[] args) {
   
        //创建集合对象
        Collection<String> c = new ArrayList<String>();

        1.    boolean add(E e):添加元素
        System.out.println("add:"+c.add("hello"));
        //通过查看ArrayList源码,我们知道了add(E e)方法返回值永远是true
        c.add("world");
        c.add("java");

        2.    boolean remove(Object o):从集合中移除元素
        //System.out.println("remove:"+c.remove("world"));
        //System.out.println("remove:"+c.remove("111")); //失败返回false

        3.    void clear():清空集合中的元素
        //c.clear();

        4.    boolean contains(Object o):判断集合中是否存在指定的元素
        //System.out.println("contains:"+c.contains("world")); //true
        //System.out.println("contains:"+c.contains("111")); //false

        5.    boolean isEmpty():判断集合是否为空
        System.out.println("isEmpty:"+c.isEmpty()); //false

        6.    int size():集合的长度,也就是集合中元素的个数
        System.out.println("siez:"+c.size());

        //输出集合对象
        System.out.println(c);
    }
}

Collection集合的遍历

Collection集合的遍历

在Collection中的iterator()方法中,
Iterator<E> iterator():返回的是一个collection的元素上进行迭代的迭代器

通过集合对象调用iterator()方法得到迭代器对象,他是Iterator<E>的实现类对象

Iterator:
	E  next();:返回迭代的下一个元素
	boolean hasNext():如果有元素则可以迭代,返回true
	void remove():
public class CollectionDemo {
   
    public static void main(String[] args) {
   
        //创建集合对象
        Collection<String> c = new ArrayList<String>();
        //添加元素
        c.add("hello");
        c.add("world");
        c.add("java");
        //遍历集合
        Iterator<String> it = c.iterator(); //Iterator是接口,所以返回的肯定是实现类的对象
        //System.out.println(it.next());
        //System.out.println(it.next());
        //System.out.println(it.next());
        //java.util.NoSuchElementException
        //System.out.println(it.next());
        //boolean hasNext()
        while (it.hasNext()){
   
            //System.out.println(it.next());
            String s = it.next();
            System.out.println(s);
        }
    }
}

在这里插入图片描述

Collection集合的练习

需求:Collection集合存储自定义对象并遍历
自定义学生类:给出成员变量name和age,遍历集合的时候,在控制台输出对象的成员变量值
package com.scy11;

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

public class CollectionDemo {
   
    public static void main(String[] args) {
   
        //创建集合对象
        Collection<Student> c = new ArrayList<Student>();
        //创建元素对象
        Student s1 = new Student("林青霞",20);
        Student s2 = new Student("张曼玉",35);
        Student s3 = new Student("王祖贤",50);
        //把元素添加到集合
        c.add(s1);
        c.add(s2);
        c.add(s3);
        //遍历集合
        Iterator<Student> it = c.iterator();
        while (it.hasNext()){
   
            Student s = it.next();
            System.out.println(s.getName()+"---"+s.getAge());
        }
    }
}

List

List:
	有序的 collection(也称为序列)。此接口的用户可以对列表中每个元素的插入位置进行精确
	地控制。用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素。

	与 set 不同,列表通常允许重复的元素。

List集合特点:
	a:有序(存储和取出的元素的顺序一致)
	b:存储的元素可以重复
public class ListDemo {
   
    public static void main(String[] args) {
   
        List<String> list = new ArrayList<String>();
        list.add("hello");
        list.add("world");
        list.add("java");
        list.add("world");
        Iterator<String> it = list.iterator();
        while (it.hasNext()){
   
            String s = it.next();
            System.out.println(s);
        }
    }
}

List集合的遍历

List集合的遍历:
	a:迭代器;
	b:普通for循环遍历
List<String> list = new ArrayList<String>();
        list.add("hello");
        list.add("world");
        list.add("java");
        list.add("world");
for (int i=0;i<list.size();i++){
   
			String s = list.get(i)
            System.out.println(s);
        }

List集合的练习

需求:List集合存储自定义对象并遍历
提示:自定义一个学生类,给出成员变量name和age,遍历集合的时候,在控制台输出学生对象的
成员变量值(用两种方式遍历)
public class ListDemo {
   
    public static void main(String[] args) {
   
        //创建List集合对象
        List<Student> list = new ArrayList<Student>();
        //创建元素对象
        Student s1 = new Student("林青霞",20);
        Student s2 = new Student("张曼玉",10);
        Student s3 = new Student("王祖贤",25);
        list.add(s1);
        list.add(s2);
        list.add(s3);
        Iterator<Student> it = list.iterator();
        while (it.hasNext()){
   
            Student s = it.next();
            System.out.println(s.getName()+"---"+s.getAge());
        }
        System.out.println("-------------------");
        for (int i=0;i<list.size(<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值