zoukankan      html  css  js  c++  java
  • Java学习笔记Day5 集合


    集合只能存放对象(对象的引用)

    Set接口extends collection接口

    HashSet实现类;
    Set set = new HashSet();
    无法保证元素的排列顺序(元素位置由HashCode决定)
    不可重复(指的是HashCode)
    add(增添);remove(移除);contains(判断存在);clear(集合清空);size(获取集合元素个数);
    遍历:
    1.使用迭代器遍历集合

    1 Iterator it = set.iterator();
    2 while(it.hasNext()){ 
    3   syso(it.next());
    4 }

    2.使用for-each遍历集合

    1 for(Object obj : set){
    2   //把set的每一个值取出赋值给obj,直至循环完毕
    3   syso(obj);
    4 }


    泛型: 指定集合存入对象的类型
    Set<String> set = new HashSet<String>();
    Set<Object> set = new HashSet<Object>();



    TreeSet实现类:
    储存同一类对象
    可保证集合元素的有序排列(自然排序和定制排序)
    Set<Integer> set = new TreeSet<Integer>();
    定制排序
    //Person类实现了Comparator接口并且重写接口中compare方法
    public class Person implements Comparator<Person>
    //借助泛型限制对象类型
    Set<Person> set = new TreeSet<Person>(new Person());

    List接口与ArrayList实现类:
    有序,可重复,有索引
    List<String> list = new ArrayList<String>();
    indexOf();lastIndexOf();subList(包含,不包含);
    遍历同Set

    Map接口

    HashMap实现类:

    Map<integer, String> map = new HashMap<integer,String>();键值对
    containsKey();containsValue();//返回boolean类型
    keySet();//获取map集合的key集合
    values();//获取map集合的value集合
    遍历map集合:
    keySet() + foreach + get(Key)
    entrySet() + foreach + getKey() + getValue()
    TreeMap实现类:
    根据Key对键值对进行排序

    Collections:
    操作集合的工具类,提供了很多方法
    reverse(List);shuffle(List)随机排序;
    sort()升序;sort(xx,comparator);swap(xx,index,index)
    frequency():

     

  • 相关阅读:
    函数名的使用-闭包-迭代器
    函数
    文件操作
    基础数据补充
    python基础-数据类型(2)
    python基础-数据类型(1)
    PHP 缓存技术(一)
    linux学习笔记整理(九)
    linux学习笔记整理(八)
    linux学习笔记整理(七)
  • 原文地址:https://www.cnblogs.com/ziyang1060/p/13588405.html
Copyright © 2011-2022 走看看