zoukankan      html  css  js  c++  java
  • Collection集合List、Set

    Collection集合,用来保存一组数据的数据结构。

    Collection是一个接口,定义了所有集合都应该包含的特征和行为

    Collection派生出了两类集合

    List和Set

    List接口:List集合的特征是元素是可重复且有序

    Set接口:Set集合的特征是元素是不可重复且无序

    public class TestSet {
        public static void main(String[] args) {
            List l = new ArrayList();
            Set s = new HashSet();
            
            l.add("one");
            l.add("one");
            l.add("two");
            l.add("three");
            s.add("one");
            s.add("one");
            s.add("two");
            s.add("three");
            System.out.println(l.size()+"  元素为"+l);
            System.out.println(s.size()+"  元素为"+s);
            
        }
    
    }
    运行结果如下:
    4  元素为[one, one, two, three]
    3  元素为[two, one, three]
    分析:set中将重复的去除了,且没有顺序
    
    

    List集合:ArrayList和LinkedList最常用的两个子类实现

    Set集合:

    HashSet:使用散列算法实现的Set集合

    TreeSet:使用二叉树算法实现的Set集合

    Collection接口方法的定义

    int  size():返回当前集合中的元素数量

    boolean isEmpty():集合是否是空的

    void clear():清空集合元素

    add(Object obj):向集合中添加元素

    remove(Object obj):

    addAll(Collection c):将给定集合中的所有元素添加到当前集

    removeAll(Collection c):删除当前集合中和给定集合中相同

    Interator iterator():获取用于遍历集合元素的迭代器

    ArrrayList 与 LinkedList

    使用方法是一模一样。都是list的子类

    ArrayList内部有一个数组实现。ArrayList会在需要的时候对数组进行扩容。

    LinkedList使用链表结构实现

  • 相关阅读:
    [转载]memcached完全剖析--1. memcached的基础
    I/O多路复用
    How to install the zsh shell in Linux && how to set it as a default login shell
    深入理解计算机中的 csapp.h和csapp.c
    (一)网络编程基础之套接字入门
    crx
    t
    武汉第一例肺炎病例
    C语言 ## __VA_ARGS__ 宏
    NLP之中文分词cppjieba
  • 原文地址:https://www.cnblogs.com/dieyaxianju/p/5115034.html
Copyright © 2011-2022 走看看