Java核心API-集合

集合



前言

了解Java集合框架,使用ArrayList、ListedList存取数据,了解ArrayList与LinkedList的区别


一、Java集合框架的概念

Java集合框架提供了一套性能优良、使用方便的接口和类,它们位于java.util包中

二、Java集合框架的内容

1、Collection 接口存储一组不唯一,无序的对象

2、List 接口存储一组不唯一,有序(插入顺序)的对象

3、Set 接口存储一组唯一,无序的对象

4、Map接口存储一组键值对象,提供key到value的映射

三、List接口的实现类

List 接口存储一组不唯一,有序(插入顺序)的对象

1、ArrayList

ArrayList实现了长度可变的数组,在内存中分配连续的空间,遍历元素和随机访问元素的效率比较高

2、LinkedList

LinkedList采用链表存储方式,插入、删除元素时效率比较高

四、ArrayList集合类

1、确定存储方式

1)ArrayList类是List接口的一个具体实现类

2)ArrayList对象实现了可变大小的数组

3)随机访问和遍历元素时,它提供更好的性能

2、确定存储对象

1)创建类型

2)包含属性

3、具体实现

见如下案例代码

4、ArrayList常用方法

在这里插入图片描述

五、LinkedList集合类

1、确定存储方式

1)LinkedList类是List接口的一个具体实现类

2)LinkedList 类用于创建链表数据结构

3)插入或者删除元素时,它提供更好的性能

2、具体实现

见如下案例代码

3、LinkedList常用方法

在这里插入图片描述

六、案例代码

public class NewsTitle {

    private int id;
    private String name;
    private String author;

    public NewsTitle() {
    }

    public NewsTitle(int id, String name, String author) {
        this.id = id;
        this.name = name;
        this.author = author;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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;
    }

    @Override
    public String toString() {
        return "NewsTitle{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}

遍历集合

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

public class ArrayListTest01 {
    /*遍历集合*/
    public static void main(String[] args) {

        // 创建5个NewsTitle对象

        NewsTitle newsTitle1 = new NewsTitle(101,"统一遥控器","作者1");
        NewsTitle newsTitle2 = new NewsTitle(102,"火箭发射器","作者2");
        NewsTitle newsTitle3 = new NewsTitle(103,"洗衣机控制","作者3");
        NewsTitle newsTitle4 = new NewsTitle(104,"自动驾驶系统","作者4");
        NewsTitle newsTitle5 = new NewsTitle(105,"小米SU7发布","作者5");

        // 准备容器 使用ArrayList
        // 查看ArrayList构造方法
        // 使用ArrayList的无参构造方法创建对象
        ArrayList arrayList = new ArrayList();

        // 调用方法将数据存储到容器中
        // public boolean add(E e)将指定的元素添加到此列表的尾部
        arrayList.add(newsTitle1);
        arrayList.add(newsTitle2);
        arrayList.add(newsTitle3);
        arrayList.add(newsTitle4);
        arrayList.add(newsTitle1);

        // 查看集合中元素的个数
        // public int size()返回此列表中的元素数
        int size = arrayList.size();
        System.out.println("集合中的元素个数:"+size); // 集合中的元素个数:5

        // 获取集合中第一个元素
        // public E get(int index)返回此列表中指定位置上的元素
        Object obj1 = arrayList.get(0);
        // 将obj转化为实际类型NewsTitle,使用向下转型实现
        NewsTitle nt1 = (NewsTitle) obj1;
        System.out.println(nt1); // NewsTitle{id=101, name='统一遥控器', author='作者1'}
        // System.out.println(obj1); // NewsTitle{id=101, name='统一遥控器', author='作者1'}
        // 输出结果一样是因为重写toString方法

        // 获取集合中第二个元素
        Object obj2 = arrayList.get(1);
        NewsTitle nt2 = (NewsTitle) obj2;
        System.out.println(nt2);

        // 获取集合中第三个元素
        Object obj3 = arrayList.get(2);
        NewsTitle nt3 = (NewsTitle) obj3;
        System.out.println(nt3);

        // 获取集合中第四个元素
        Object obj4 = arrayList.get(3);
        NewsTitle nt4 = (NewsTitle) obj4;
        System.out.println(nt4);

        // 获取集合中第五个元素
        Object obj5 = arrayList.get(4);
        NewsTitle nt5 = (NewsTitle) obj5;
        System.out.println(nt5);

        System.out.println("------ ------ ------");

        // 通过获取集合中的元素,可以使用遍历方式输出集合中所有元素
        for (int i = 0; i < arrayList.size(); i++) {
            Object object = arrayList.get(i);
            NewsTitle newsTitle = (NewsTitle) object;
            System.out.println(newsTitle);
        }

        System.out.println("------ ------ ------");

        // 使用增强for循环遍历集合
        for (Object obj : arrayList) {
            NewsTitle nTitle = (NewsTitle) obj;
            System.out.println(nTitle);
        }

        System.out.println("------ ------ ------");

        // 使用迭代器遍历集合
        // Iterator iterator():返回以恰当顺序在此列表的元素上进行迭代的迭代器
        // iterator()方法是ArrayList类的父类AbstractList类中的方法
        Iterator iterator = arrayList.iterator();
        // 通过iterator调用hasNext()方法判断迭代器中是否有元素,如果有则返回true
        while (iterator.hasNext()) {
            // 取出迭代器中的元素
            Object object = iterator.next();
            // 将数据转换为真实类型NewsTitle
            NewsTitle newsTitle = (NewsTitle) object;
            System.out.println(newsTitle);
        }

        // 输出结果
        // NewsTitle{id=101, name='统一遥控器', author='作者1'}
        // NewsTitle{id=102, name='火箭发射器', author='作者2'}
        // NewsTitle{id=103, name='洗衣机控制', author='作者3'}
        // NewsTitle{id=104, name='自动驾驶系统', author='作者4'}
        // NewsTitle{id=101, name='统一遥控器', author='作者1'}


    }
}

添加、删除集合中的元素

import java.util.ArrayList;

public class ArrayListTest02 {
    /*添加、删除集合中的元素*/
    public static void main(String[] args) {

        // 创建5个NewsTitle对象

        NewsTitle newsTitle1 = new NewsTitle(101,"统一遥控器","作者1");
        NewsTitle newsTitle2 = new NewsTitle(102,"火箭发射器","作者2");
        NewsTitle newsTitle3 = new NewsTitle(103,"洗衣机控制","作者3");
        NewsTitle newsTitle4 = new NewsTitle(104,"自动驾驶系统","作者4");
        NewsTitle newsTitle5 = new NewsTitle(105,"小米SU7发布","作者5");

        // 准备容器:使用ArrayList类的无参构造方法创建对象
        ArrayList arrayList = new ArrayList();

        // 调用方法将数据存储在容器中
        arrayList.add(newsTitle1);
        arrayList.add(newsTitle2);
        arrayList.add(newsTitle3);
        arrayList.add(newsTitle4);
        arrayList.add(newsTitle5);

        // 遍历集合
        for (int i = 0; i < arrayList.size(); i++) {
            Object object = arrayList.get(i);
            // ArrayList arrayList1 = arrayList.get(i); 报错
            NewsTitle newsTitle = (NewsTitle) object;
            System.out.println(newsTitle);
        }
        // 输出结果
        // NewsTitle{id=101, name='统一遥控器', author='作者1'}
        // NewsTitle{id=102, name='火箭发射器', author='作者2'}
        // NewsTitle{id=103, name='洗衣机控制', author='作者3'}
        // NewsTitle{id=104, name='自动驾驶系统', author='作者4'}
        // NewsTitle{id=105, name='小米SU7发布', author='作者5'}

        System.out.println("------ ------ ------");

        // 将newTitle5对象添加到集合的第一个位置(下标为0的位置)
        /*
        * public void add(int index,E element)将指定的元素插入此列表中的指定位置
        * 向右移动当前位于该位置的元素(如果有)以及所有后续元素(将其索引加 1)
        * */
        arrayList.add(0,newsTitle5);

        // 元素添加完之后,遍历集合
        // 使用增强for循环遍历集合
        for (Object obj : arrayList) {
            NewsTitle nTitle = (NewsTitle) obj;
            System.out.println(nTitle);
        }
        // 输出结果
        // NewsTitle{id=105, name='小米SU7发布', author='作者5'}
        // NewsTitle{id=101, name='统一遥控器', author='作者1'}
        // NewsTitle{id=102, name='火箭发射器', author='作者2'}
        // NewsTitle{id=103, name='洗衣机控制', author='作者3'}
        // NewsTitle{id=104, name='自动驾驶系统', author='作者4'}
        // NewsTitle{id=105, name='小米SU7发布', author='作者5'}

        System.out.println("------ ------ ------");

        /*
        * public boolean contains(Object o)如果此列表中包含指定的元素,则返回 true
        * 更确切地讲,当且仅当此列表包含至少一个满足 (o==null ? e==null : o.equals(e)) 的元素 e 时,则返回 true
        * */
        boolean result1 = arrayList.contains(newsTitle5);
        System.out.println("集合中包含元素newTitle5:"+result1); // true

        System.out.println("------ ------ ------");

        /*
        * public E remove(int index)移除此列表中指定位置上的元素。向左移动所有后续元素(将其索引减 1)
        * */
        Object object1 = arrayList.remove(0);
        System.out.println("删除元素是:"+object1); // 删除元素是:NewsTitle{id=105, name='小米SU7发布', author='作者5'}
        // 遍历集合
        for (Object obj : arrayList) {
            NewsTitle nTitle = (NewsTitle) obj;
            System.out.println(nTitle);
        }
        // NewsTitle{id=101, name='统一遥控器', author='作者1'}
        // NewsTitle{id=102, name='火箭发射器', author='作者2'}
        // NewsTitle{id=103, name='洗衣机控制', author='作者3'}
        // NewsTitle{id=104, name='自动驾驶系统', author='作者4'}
        // NewsTitle{id=105, name='小米SU7发布', author='作者5'}

        System.out.println("------ ------ ------");

        /*
        * public boolean remove(Object o)移除此列表中首次出现的指定元素(如果存在)
        * 如果列表不包含此元素,则列表不做改动
        * */
        arrayList.remove(newsTitle3);

        for (Object obj : arrayList) {
            NewsTitle nTitle = (NewsTitle) obj;
            System.out.println(nTitle);
        }
        // NewsTitle{id=101, name='统一遥控器', author='作者1'}
        // NewsTitle{id=102, name='火箭发射器', author='作者2'}
        // NewsTitle{id=104, name='自动驾驶系统', author='作者4'}
        // NewsTitle{id=105, name='小米SU7发布', author='作者5'}

    }
}

判断集合是否为空

import java.util.ArrayList;
public class ArrayListTest03 {
    /*判断集合是否为空*/
    public static void main(String[] args) {

        // 创建5个NewsTitle对象

        NewsTitle newsTitle1 = new NewsTitle(101,"统一遥控器","作者1");
        NewsTitle newsTitle2 = new NewsTitle(102,"火箭发射器","作者2");
        NewsTitle newsTitle3 = new NewsTitle(103,"洗衣机控制","作者3");
        NewsTitle newsTitle4 = new NewsTitle(104,"自动驾驶系统","作者4");
        NewsTitle newsTitle5 = new NewsTitle(105,"小米SU7发布","作者5");

        ArrayList arrayList = new ArrayList();

        // 调用方法将数据存储到容器中
        arrayList.add(newsTitle1);
        arrayList.add(newsTitle3);
        arrayList.add(newsTitle4);
        arrayList.add(newsTitle2);
        arrayList.add(newsTitle3);

        // 判断一个集合是否有元素,可以通过集合中的元素个数来判断,如果集合中个数为0,说明集合为空
        int size = arrayList.size();
        System.out.println("集合个数为:" + size); // 集合个数为:5
        // 除了使用size()方法获取集合元素个数判断集合是否为空,还可以使用isEmpty()方法来判断
        /*
        * public boolean isEmpty()如果此列表中没有元素,则返回 true
        * */
        boolean result1 = arrayList.isEmpty();
        System.out.println("集合为空:"+result1); // 集合为空:false

        /*
        * public void clear()移除此列表中的所有元素。此调用返回后,列表将为空
        * */
        arrayList.clear();
        // 判断集合是否为空结果返回为布尔值
        System.out.println("集合为空:"+arrayList.isEmpty()); // 集合为空:true
        System.out.println("集合中元素个数:"+arrayList.size()); // 集合中元素个数:0


    }
}
import java.util.ArrayList;
import java.util.Arrays;

public class ArrayListTest04 {
    /*集合返回值为数组、查找集合元素下标*/
    public static void main(String[] args) {

        NewsTitle newsTitle1 = new NewsTitle(101,"统一遥控器","作者1");
        NewsTitle newsTitle2 = new NewsTitle(102,"火箭发射器","作者2");
        NewsTitle newsTitle3 = new NewsTitle(103,"洗衣机控制","作者3");
        NewsTitle newsTitle4 = new NewsTitle(104,"自动驾驶系统","作者4");
        NewsTitle newsTitle5 = new NewsTitle(105,"小米SU7发布","作者5");

        ArrayList arrayList = new ArrayList();

        arrayList.add(newsTitle5);
        arrayList.add(newsTitle4);
        arrayList.add(newsTitle3);
        arrayList.add(newsTitle2);
        arrayList.add(newsTitle1);

        /*
        * public Object[] toArray()按适当顺序(从第一个到最后一个元素)返回包含此列表中所有元素的数组
        * */
        Object[] objects = arrayList.toArray(); // 返回值为数组
        // 遍历数组
        for (int i = 0; i < objects.length; i++) {
            System.out.println(objects[i]);
        }
        // 输出结果
        // NewsTitle{id=105, name='小米SU7发布', author='作者5'}
        // NewsTitle{id=104, name='自动驾驶系统', author='作者4'}
        // NewsTitle{id=103, name='洗衣机控制', author='作者3'}
        // NewsTitle{id=102, name='火箭发射器', author='作者2'}
        // NewsTitle{id=101, name='统一遥控器', author='作者1'}

        System.out.println("------ ------ ------");

        System.out.println(Arrays.toString(objects));

        System.out.println("------ ------ ------");

        /*
        * public int indexOf(Object o)返回此列表中首次出现的指定元素的索引,或如果此列表不包含元素,则返回 -1
        * */
        int index1 = arrayList.indexOf(newsTitle3);
        System.out.println("集合中第一次出现newTitle3元素下标:"+index1); // 下标为2
        int index2 = arrayList.indexOf(newsTitle5);
        System.out.println("集合中第一次出现newTitle5元素下标:"+index2); // 下标为0

        /*
        * public int lastIndexOf(Object o)返回此列表中最后一次出现的指定元素的索引,或如果此列表不包含索引,则返回 -1
        * */
        int index3 = arrayList.lastIndexOf(newsTitle3);
        System.out.println("集合中最后一次出现newsTitle3元素下标:"+index3); // 下标为2
        int index4 = arrayList.lastIndexOf(newsTitle5);
        System.out.println("集合中最后一次出现newsTitle5元素下标:"+index4); // 下标为0

    }
}

LinkedList集合

public class NewsTitle {

    private int id;
    private String name;
    private String author;

    public NewsTitle() {
    }

    public NewsTitle(int id, String name, String author) {
        this.id = id;
        this.name = name;
        this.author = author;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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;
    }

    @Override
    public String toString() {
        return "NewsTitle{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}
import java.util.Iterator; // 接口
import java.util.LinkedList;

public class LinkedListTest {

    public static void main(String[] args) {

        // 准备容器
        // 向上转型:将接口引用指向实现类的实例
        // List list = new LinkedList();
        // 向上转型中接口的引用无法调用实现类中特有的方法
        // list.addFirst();

        // 不使用多态形式创建对象
        LinkedList linkedList = new LinkedList();

        // 准备数据:创建5个NewsTitle对象
        NewsTitle newsTitle1 = new NewsTitle(101,"国家广电总局:逐步实现一个遥控器看电视","邱伟");
        NewsTitle newsTitle2 = new NewsTitle(102,"俄罗斯的本质:不问东西天地宽,摇摆西东无是处","曹兴教授");
        NewsTitle newsTitle3 = new NewsTitle(103,"震惊,此物竟有如此神奇效果","张阿姨");
        NewsTitle newsTitle4 = new NewsTitle(104,"通知:公司过年放假安排","行政处");
        NewsTitle newsTitle5 = new NewsTitle(105,"美国体系崩溃后的世界格局","有趣探花");

        // 将元素添加到集合中
        linkedList.add(newsTitle1);
        linkedList.add(newsTitle3);
        linkedList.add(newsTitle4);
        linkedList.add(newsTitle3);
        linkedList.add(newsTitle2);

        System.out.println("集合中元素个数:"+linkedList.size());

        // 遍历集合
        for(int i = 0;i < linkedList.size();i++){
            Object object =linkedList.get(i);
            NewsTitle newsTitle = (NewsTitle)object;
            System.out.println(newsTitle);
        }

        System.out.println("---------------------------------");

        // 将元素newsTitle5插入到集合的第一个位置
        // linkedList.add(0,newsTitle5);
        // 使用LinkedList类中特有的方法实现将元素newsTitle5插入集合的第一个位置
        linkedList.addFirst(newsTitle5);
        linkedList.addLast(newsTitle1);

        for(Object object :linkedList){
            NewsTitle newsTitle = (NewsTitle)object;
            System.out.println(newsTitle);
        }
        // 输出结果
        // NewsTitle{id=105, name='美国体系崩溃后的世界格局', author='有趣探花'}
        // NewsTitle{id=101, name='国家广电总局:逐步实现一个遥控器看电视', author='邱伟'}
        // NewsTitle{id=103, name='震惊,此物竟有如此神奇效果', author='张阿姨'}
        // NewsTitle{id=104, name='通知:公司过年放假安排', author='行政处'}
        // NewsTitle{id=103, name='震惊,此物竟有如此神奇效果', author='张阿姨'}
        // NewsTitle{id=102, name='俄罗斯的本质:不问东西天地宽,摇摆西东无是处', author='曹兴教授'}
        // NewsTitle{id=101, name='国家广电总局:逐步实现一个遥控器看电视', author='邱伟'}

        System.out.println("---------------------------------");

        // 获取集合中的第一个元素和最后一个元素
        // linkedList.get(0);
        // linkedList.get(linkedList.size()-1);
        Object obj1 = linkedList.getFirst();
        NewsTitle newsTitle11 =(NewsTitle) obj1;
        System.out.println(newsTitle11);
        // NewsTitle{id=105, name='美国体系崩溃后的世界格局', author='有趣探花'}

        Object obj2 =linkedList.getLast();
        NewsTitle newsTitle22 =(NewsTitle) obj2;
        System.out.println(newsTitle22);
        // NewsTitle{id=101, name='国家广电总局:逐步实现一个遥控器看电视', author='邱伟'}

        System.out.println("---------------------------------");

        // 移除集合中的第一个元素和最后一个元素
        // linkedList.remove(0);
        // linkedList.remove(linkedList.size()-1);
        linkedList.removeFirst();
        linkedList.removeLast();

        // 删除完元素之后遍历集合
        Iterator iterator = linkedList.iterator();
        while(iterator.hasNext()){
            //取出迭代器中的元素
            Object object =iterator.next();
            NewsTitle newsTitle = (NewsTitle) object;
            System.out.println(newsTitle);
        }
        // NewsTitle{id=101, name='国家广电总局:逐步实现一个遥控器看电视', author='邱伟'}
        // NewsTitle{id=103, name='震惊,此物竟有如此神奇效果', author='张阿姨'}
        // NewsTitle{id=104, name='通知:公司过年放假安排', author='行政处'}
        // NewsTitle{id=103, name='震惊,此物竟有如此神奇效果', author='张阿姨'}
        // NewsTitle{id=102, name='俄罗斯的本质:不问东西天地宽,摇摆西东无是处', author='曹兴教授'}

    }
}

七、Set接口与HashSet类

Set接口存储一组唯一,无序对象

HashSet是Set接口常用的实现类

import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;

public class HashSetDemo01 {

    public static void main(String[] args) {

        // 准备容器
        HashSet hashSet = new HashSet();

        // 准备数据
        String str1 = "1大湖名城";
        String str2 = "2创新高低";
        String str3 = "3科教支撑";
        String str4  = "4新能源";

        // 将数据存储到集合中
        hashSet.add(str1);
        hashSet.add(str2);
        hashSet.add(str3);
        hashSet.add(str4);
        hashSet.add(str1);

        // 获取元素个数
        System.out.println("集合中元素个数:"+hashSet.size()); // 输出结果 集合中元素个数:4
        // 说明HashSet集合中的元素不能重复

        // HashSet集合中元素是无序的,所以没有下标值,不能通过get()获取
        // 使用增强for循环遍历
        for (Object object : hashSet){
            String string = (String) object;
            System.out.println(string);
        }

        System.out.println("------ ------ ------");

        // 通过迭代器遍历集合
        Iterator iterator = hashSet.iterator();
        while(iterator.hasNext()) {
            Object object = iterator.next();
            String string = (String)object;
            System.out.println(string);
        }

        System.out.println("------ ------ ------");

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

        // 删除集合中str2
        hashSet.remove(str2);
        System.out.println("集合中元素个数:"+hashSet.size()); // 集合中元素个数:3

        // 将集合中的元素转换成数组
        Object[] objects = hashSet.toArray();
        System.out.println(Arrays.toString(objects)); // [1大湖名城, 3科教支撑, 4新能源]

        // 清空集合中的元素
        hashSet.clear();
        System.out.println("集合中元素个数:"+hashSet.size()); // 集合中元素个数:0
        System.out.println("集合为空:"+hashSet.isEmpty()); // 集合为空:true
        
    }
}
import java.util.HashSet;
import java.util.Set;

public class HashSetDemo02 {
/*Set是接口 实现类HashSet*/
    public static void main(String[] args) {

        Set set = new HashSet();
        String s1 = new String("java");
        String s2 = s1;
        String s3 = new String("JAVA");
        set.add(s1);
        set.add(s2);
        set.add(s3);
        System.out.println(set.size()); // 输出结果为2
        // Set 接口存储一组唯一,无序的对象

    }
}
import java.util.HashSet;
import java.util.Set;

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

        Set set = new HashSet();
        String s1 = new String("java");
        String s2 = s1;
        String s3 = new String("java");
        set.add(s1);
        set.add(s2);
        set.add(s3);
        System.out.println(set.size()); // 输出结果为1

    }
}
  • 21
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值