Collection集合方法使用练习

package com.company.collection;

import java.util.*;

/**
 * @Author: Zhao
 * @Date: 2020/3/9 11:07
 * @Description:
 * 单列集合根接口:Collection
 * Collection子接口:List(元素有序、可重复)、Set(元素无序、不可重复)
 * List实现类:ArrayList(存储结构是数组结构,增删慢、查找快)、LinkedList(存储结构是链表结构,方便增删)
 * Set实现类(保证元素唯一性的方式依赖于hashCode与equals方法):HashSet
 */
public class CollectionTest {

    public static void main(String[] args){
      // CollectionTest.collectionDemo();
      // CollectionTest.listDemo();
       // CollectionTest.linkedListDemo();
       // CollectionTest.setDemo();
        CollectionTest.hashSetDemo();
    }
    /*
     *单列集合通用方法
     * 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 collectionDemo(){
        Collection<String> col = new ArrayList<>();
        col.add("红的");
        col.add("黄的");
        col.add("绿的");
        //使用迭代器Iterator遍历集合
        Iterator<String> it = col.iterator();
        System.out.print("集合中元素:");
        while(it.hasNext()){
            String s = it.next();
            System.out.print(" " + s);
        }
        System.out.println();

        col.remove("黄的");
        //增强for循环(foreach循环)遍历集合
        System.out.print("集合中元素:");
        for(String s : col){
            System.out.print(" " + s);
        }
        System.out.println();

        System.out.println("集合中是否包含“黄的”对象:" + col.contains("黄的"));
        System.out.println("集合中元素个数:" + col.size());

        Object[] object = col.toArray();
        System.out.print("数组object中元素:");
        for(int i=0;i<object.length;i++){
            System.out.print(" " + object[i]);
        }
        System.out.println();
    }

    /**
     * List接口:存取有序、有索引、可以存储重复元素
     * List接口作为Collection的子接口,继承了Collection接口中的所有方法,
     * 还增加了一些根据元素索引(索引从0开始)来操作集合的方法:
     * public void add(int index,E element):添加元素到指定位置
     * public E get(int index):返回指定位置的元素
     * public E remove(int index):删除并返回指定位置的元素
     * public E set(int index,E element):替换指定位置的元素
     */
    public static void listDemo(){
        List<String> list = new ArrayList<>();
        //向集合尾部添加
        list.add("白的");
        list.add("蓝的");
        list.add("粉的");
        list.add(1,"黑的");
        Iterator<String> it = list.iterator();
        System.out.print("集合list中元素有:");
        while(it.hasNext()){
            String s = it.next();
            System.out.print(" " + s);
        }
        System.out.println();
        System.out.println("集合list中第三位元素是:" + list.get(2));
        System.out.println("集合list中第三位元素更新为“蓝色”:" + list.set(2,"蓝色"));
        list.remove(3);
        System.out.print("集合list中元素有:" );
        for(String s : list){
            System.out.print(" " + s);
        }
    }

    /**
     * LinkedList集合数据的存储结构是链表结构,LinkedList是一个双向链表
     * LinkedList集合也可做堆栈、队列使用
     * LinkedList集合是List的实现类,继承了Collection和list接口中的所有方法,
     * 提供你给了大量首尾操作方法:
     * public void addFirst(E e):插入到表头
     * public void addLast(E e):插入到表尾
     * public E getFirst():返回表头元素
     * public E getLast():返回表尾元素
     * public E removeFirst():移除并返回表头元素
     * public E removeLast():移除并返回表尾元素
     * public E pop():出栈
     * public void push(E e):入栈
     */
    public static void linkedListDemo(){
        LinkedList<String> linkedList = new LinkedList<>();
        linkedList.add("蓝的");
        linkedList.add("绿的");
        linkedList.add("粉的");
        linkedList.addFirst("红的");
        linkedList.addLast("黄的");
        System.out.println(linkedList);
        System.out.println("表头元素为:" + linkedList.getFirst());
        System.out.println("表尾元素为:" + linkedList.getLast());
    }

    /**
     * Set接口:存取无序、不可以存储重复元素
     * Set接口作为Collection的子接口,继承了Collection接口中的所有方法,
     * 没有扩充方法,只是比Collection接口更加严格,不允许元素重复
     */
    public static void setDemo(){
        Set<String> set = new HashSet<>();
        set.add("红的");
        set.add("黄的");
        set.add("绿的");
        set.add("红的");
        System.out.println(set);
    }

    /**
     * HashSet存储自定义对象需要重写equals和hashCode方法
     */
    public static void hashSetDemo(){
        HashSet<Student> hashSet = new HashSet<>();
        Student stu = new Student("张三",21);
        hashSet.add(stu);
        hashSet.add(new Student("李四",23));
        hashSet.add(new Student("王五",22));
        hashSet.add(new Student("李四",23));
        for(Student stu1 : hashSet){
            System.out.println(stu1.getName()+stu1.getAge());
        }
    }
}

package com.company.collection;

import java.util.Objects;

/**
 * @author:Zhaoshuang
 * @date:2020/2/16
 * @description:
 */
public class Student {
    private String name;
    private int age;
    public Student(String name, int age){
        this.age = age;
        this.name = name;
    }
    public String getName(){
        return this.name;
    }
    public void setName(String name){
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o){
        if(this == o)
            return true;
        if(o == null || this.getClass() != o.getClass())
            return false;
        Student s = (Student)o;
        return age == s.age && Objects.equals(name,s.name);
    }
    @Override
    public int hashCode(){
        return Objects.hash(name,age);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值