码迷,mamicode.com
首页 > 其他好文 > 详细

集合知识点整理

时间:2017-06-03 23:25:44      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:clear   先进后出   线程   可变   重写   keyset   wrap   list   title   

Collection接口

1综述:集合接口Collection 接口java.util

Collecton接口常用的子接口有:List接口(列表,序列)、Set接口(集)

List接口常用的子类有:ArrayList类(数组列表)、LinkedList类(链表)

Set接口常用的子类有:HashSet类(哈希表)、LinkedHashSet类(基于链表的哈希表)

boolean

add(E e)
确保此 collection 包含指定的元素(可选操作)。

 boolean

addAll(Collection<? extends E> c)
将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。

 void

clear()
移除此 collection 中的所有元素(可选操作)。

 boolean

contains(Object o)
如果此 collection 包含指定的元素,则返回 true

 boolean

containsAll(Collection<?> c)
如果此 collection 包含指定 collection 中的所有元素,则返回 true

 boolean

equals(Object o)
比较此 collection 与指定对象是否相等。

 int

hashCode()
返回此 collection 的哈希码值。

 boolean

isEmpty()
如果此 collection 不包含元素,则返回 true

 Iterator<E>

iterator()
返回在此 collection 的元素上进行迭代的迭代器。

 boolean

remove(Object o)
从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。

 boolean

removeAll(Collection<?> c)
移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。

 boolean

retainAll(Collection<?> c)
仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。

 int

size()
返回此 collection 中的元素数。

 Object[]

toArray()
返回包含此 collection 中所有元素的数组。

<T> T[]

toArray(T[] a)
返回包含此 collection 中所有元素的数组;返回数组的运行时类型与指定数组的运行时类型相同。

2.集合的遍历

 (1)Iterator迭代器

//2.获取容器的迭代器对象。通过iterator方法。

Iterator it = coll.iterator();

 

//3,使用具体的迭代器对象获取集合中的元素。参阅迭代器的方法

while(it.hasNext()){

   System.out.println(it.next());

}

(2)增强for遍历

/*

   * 增强 for 遍历对象的时候,可以调用对象特有的方法

   */

   privatestaticvoidmethod_02() {

       String[] arr = {"abc","defw","itcast"};

      for(String s : arr){

           System.out.println(s.length());

       }

   }

3.集合通配符问题,可以将同种泛型的集合进行转化,用一种集合创建另一种集合

List<Employee> list=new ArrayList<>();

       list.add(new Employee("孙红雷","",20));

       list.add(new Employee("葛优","",30));

        list.add(new Employee("黄渤","",25));

        list.add(new Employee("孙俪","",18));

        list.add(1, new Employee("王骏迪","",17));

   Set<Employee> s=new HashSet<Employee>(list);//list集合创建set集合

       System.out.println(s);

       s.add(new Employee("葛优","",30));

       System.out.println(s);

 

List集合

特点:有序重复有索引

数据结构: 堆栈:先进后出

队列:先进先出

数组:查询快,增删慢

链表:查询慢,增删快

List集合中的特有方法

void add(int index, Object element) 将指定的元素,添加到该集合中的指定位置上

Object get(int index)返回集合中指定位置的元素。

Object remove(int index) 移除列表中指定位置的元素, 返回的是被移除的元素

Object set(int index, Object element)用指定元素替换集合中指定位置的元素,返回值的更新前的元素

ArrayList:

底层数据结构是数组,查询快,增删慢

线程不安全,效率高

LinkedList:

底层数据结构是链表,查询慢,增删快

线程不安全,效率高

 

 

二、Set集合

(1)特点:不允许重复,没有索引

HashSet集合用哈希表,数组加链表模式.

(2)说出判断集合元素唯一的原理

ArrayList:存储元素前使用contains方法

HashSet:重写hashCodeequals方法

HashSet:

       元素唯一不能重复

       底层结构是哈希表结构

       元素的存与取的顺序不能保证一致

LinkedHashSet:

       元素唯一不能重复

       底层结构是哈希表结构 + 链表结构

       元素的存与取的顺序一致

 

三、Map集合

(1)Map集合的特点:

1.key不允许重复,value可以重复

2.一个key只能对应一个value(一夫一妻)

3.keyvalue数据类型,可以相同,也可以不同

4.Map中常用的集合为HashMap集合、LinkedHashMap集合。LinkedHashMap,存储数据采用的哈希表结构+链表结构。通过链表结构可以保证元素的存取顺序一致;通过哈希表结构可以保证的键的唯一、不重复,需要重写键的hashCode()方法、equals()方法。

 

(2)使用Map集合添加方法保存数据

map.put(key,value);

key重复返回被替换的值

key不重复返回nulll

(3)使用”键找值”的方式遍历Map集合(keyset)

//1.通过Map集合中的方法 keySet()获取Map集合中所有的健,把所有的健存储在Set集合中Set<K>

       Set<Integer> set = map.keySet();

       //2.遍历Set集合,获取Map集合中每一个的健

      //迭代器遍历Set集合

      Iterator<Integer> it = set.iterator();

      while(it.hasNext()){

          //it.next方法返回的是什么,就是Map集合中的key

          Integer key = it.next();

          //3.通过Map集合的方法get(Key),获取所有的value

          String value = map.get(key);

          System.out.println(key+"..."+value);

      }

     

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

      //采用增强for

      //for(Integer key : set){

       for(Integer key: map.keySet()){

          String value = map.get(key);

           System.out.println(key+"..."+value);

       }

 

 

(4)使用”键值对”的方式遍历Map集合

   //1.通过Map集合中的方法entrySet()获取所有的对应关系,把对应关系存储到Set集合中Set<Map.Entry<K,V>>

       Set<Map.Entry<String, String>> set = map.entrySet();

      //2.遍历存储映射关系的Set集合,获取每一个映射关系Map.Entry<K,V>

      //迭代器

      Iterator<Map.Entry<String, String>> it = set.iterator();

      while(it.hasNext()){

          //it.next返回的是什么?返回的就是Map集合中的映射关系

          Map.Entry<String, String> entry = it.next();

          //3.通过获取的每一个映射关系的方法getKeygetValue获取对应健和值 

          String key = entry.getKey();

          String value = entry.getValue();

          System.out.println(key+"..."+value);

      }

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

      //增强for

      for(Map.Entry<String, String> entry : map.entrySet()){

          /*String key = entry.getKey();

          String value = entry.getValue();

          System.out.println(key+"..."+value);*/

           System.out.println(entry.getKey()+"..."+entry.getValue());

       }

 

(5)能够使用HashMap存储自定义键值对的数据

自定义类型作为值:不需要重写hashCode和equals方法,值可以重复(同名,同年龄)

自定义类型作为健:需要重写hashCode和equals方法,保证健(同名,同年龄)视为同一个人

(6)可变参数的作用

可变参数作为方法的参数,可以传递任意多个参数(0,1,多个)

(7)能够使用集合工具类

Sort:排序

Shuffle:打乱顺序

集合知识点整理

标签:clear   先进后出   线程   可变   重写   keyset   wrap   list   title   

原文地址:http://www.cnblogs.com/outsidersblogs/p/6938753.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!