学习笔记-集合框架

集合

1.集合Collection是Java JDK提供的一个工具类,用于存储任意数量具有相同属性的对象。一个Collection就是一个Object,包含这个对象中的所有元素。
如下所示,Collection提供了一个根接口,Set和List都继承Collection接口,说明Set和List是一种集合。

这里写图片描述

Set
public interface Set<E> extends Collection<E>
在Java 中Set继承自Collection集合。是一个不包含重复元素的Collection,并且最多包含一个 null 元素。

  1. HashSet
    public class HashSet extends AbstractSet<E>
    implements Set<E>, Cloneable, Serializable
    HashSet是Set接口的实现类,是以哈希表的形式存储数据,在HashSet中对象的顺序无法保证,如下说明:
    public class Main {

            public static void main(String[] args) {
                Set<String> testSet = new HashSet<String>();
                for(int i=0;i<10;i++){
                    testSet.add(String.valueOf(i));
                }
                System.out.println(testSet);
            }
        }

运行结果:

        [3, 2, 1, 0, 7, 6, 5, 4, 9, 8]
  • HashSet的最常用的作用是:去重,如下说明:
public class Main {

    public static void main(String[] args) {
        TestList testList1 = new TestList();//创建一个TestList对象
        Set<String> testSet1 = new HashSet<String>();//创建一个Set对象
        for(int i = 0;i<testList1.storeList().size();i++){//取List中的对象放到Set中
            testSet1.add(String.valueOf(testList1.storeList().get(i)));
            System.out.print(testSet1);
        }
        System.out.println(testList1.storeList());
        System.out.println(testSet1);

    }
}

运行结果:

[0, 1, 2, 0, 1, 2, 0, 1, 2, 0]
[2, 1, 0]

2.TreeSet
待日后学习..嘿嘿

List
public interface List<E> extends Collection<E>
在Java 中List继承自Collection集合。是一个有序的集合,可以通过对象的位置(索引)去访问对象。允许集合中存在重复的元素,并且允许存在重复的null元素。
1. ArrayList
public class ArrayList<E> extends AbstractList<E>
implements List<E>,RandomAccess,Cloneable,Serializable
ArrayList是List接口的实现类。实现了List接口的大小可变的数组,列表的容量随着元素的增加而增加,并允许包括 null 在内的所有元素。可以通过一些方法自定义列表的数组大小,如下所示:

public class TestList {
    public List<String> storeList(){
        List<String> testList = new ArrayList<String>();
        for(int i=0;i<10;i++){
            int j = i%3;
            testList.add(String.valueOf(j));
        }
        return testList;
    }
}

2.LinkedList
public class LinkedList extends AbstractSequentialList<E>
implements List<E>,Deque<E>,Cloneable, Serializable
LinkedList也是List接口的一个实现类。实现了链接列表,允许null在内的所有元素,并且元素是有序的。并且可将链表用作队列、堆栈、双向队列。
练习如下:

public class TestLinkedList {
    public static void main(String[] args){
        System.out.print("H");
        LinkedList testLinkedList = new LinkedList();//定义一个默认长度为10的LinkedList列表
        for(int i = 0;i<10;i++){
            testLinkedList.add(i);//将元素添加到列表的末尾
        }
        testLinkedList.add(3,10); //表示在位置3,添加元素10
        testLinkedList.addFirst(11);//在列表的首位添加元素
        testLinkedList.addLast(12);//在列表的末位添加元素
        System.out.println(testLinkedList);
        Object i = testLinkedList.get(3);//get()方法是得到某一位置的元素
        System.out.println(i);
    }
}

运行结果:

[11, 0, 1, 2, 10, 3, 4, 5, 6, 7, 8, 9, 12]
2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值