Java集合类

Java集合类

使用JDK1.7版本

集合类型主要有3种:set(集)、list(列表)和map(映射)
但是不得不提的是:Collection接口
强调一下:以上三种均为接口

总的说来,Java API中所用的集合类,都是实现了Collection接口,他的一个类继承结构如下:
Collection<–List<–Vector
Collection<–List<–ArrayList
Collection<–List<–LinkedList
Collection<–Set<–HashSet
Collection<–Set<–HashSet<–LinkedHashSet
Collection<–Set<–SortedSet<–TreeSet

另外,Map继承结构如下
Map<–AbstractMap<–HashMap
Map<–AbstractMap<–TreeMap
Map<–SortedMap

Collection几个常用的方法:

boolean         add(E e)
void            clear()
boolean         equals(Object o)
int             hashCode()
boolean         isEmpty()
Iterator<E>     iterator()
Boolean         remove(Object o)
Int             size()

Set(集)

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.
Set集合不包含重复的元素。

常用方法:

boolean         add(E e)
void            clear()
boolean         equals(Object o)
int             hashCode()
boolean         isEmpty()
Iterator<E>     iterator()
Boolean         remove(Object o)
Int             size()

Set常用实现类

HashSet

public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable
Note that this implementation is not synchronized.线程不同步
一般使用HashSet实现类,其带有Map特性,利用Map中Key有唯一值特性,实现Set方法。

TreeSet

public class TreeSet<E> extends AbstractSet<E> implements
NavigableSet<E>, Cloneable, Serializable

Note that this implementation is not synchronized.线程不同步
带有排序功能

E   pollFirst()
E   pollLast()

list(列表)

An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.
队列列表,使用者能够自由控制列表元素的放置和根据序号进行搜索。

常用方法:

boolean         add(E e)
void            clear()
boolean         equals(Object o)
int             hashCode()
boolean         isEmpty()
Iterator<E>     iterator()
Boolean         remove(Object o)
Int             size()

E               get(int index)
int             indexOf(Object o)
void            add(int index, E element)
E               set(int index, E element)
List<E>         subList(int fromIndex, int toIndex)
ListIterator<E> listIterator()

List常用实现类

ArrayList

Note that this implementation is not synchronized.
采用数组形式保存list对象,能根据下标快速找到对应的元素值。
缺点是:保存的时候麻烦,需要搬移周边元素。

LinkedList

Note that this implementation is not synchronized.
LinkedList 采用的将对象存放在独立的空间中,而且在每个空间中还保存下一个链接的索引 。
缺点是:查找非常麻烦,要从第一个索引开始。

map(映射)

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
对象拥有关联着key和values的表。map对象不能拥有重复的key,但是每个key可以拥有多个value。

void        clear()
boolean     containsKey(Object key)
boolean     containsValue(Object value)
boolean     equals(Object o)
V           get(Object key)
boolean     isEmpty()
Set<K>      keySet()
V           put(K key, V value)
V           remove(Object key)
int         size()

Map常用实现类

HashMap
TreeMap
HashMap和HashTabled的区别
  1. 继承不同。
public class Hashtable extends Dictionary implements Map
public class HashMap  extends AbstractMap implements Map
  1. Hashtable 中的方法是同步的,而HashMap中的方法在缺省情况下是非同步的。在多线程并发的环境下,可以直接使用Hashtable,但是要使用HashMap的话就要自己增加同步处理了。
  2. Hashtable中,key和value都不允许出现null值。
    在HashMap中,null可以作为键,这样的键只有一个;可以有一个或多个键所对应的值为null。当get()方法返回null值时,即可以表示 HashMap中没有该键,也可以表示该键所对应的值为null。因此,在HashMap中不能由get()方法来判断HashMap中是否存在某个键, 而应该用containsKey()方法来判断。
  3. 两个遍历方式的内部实现上不同。Hashtable、HashMap都使用了 Iterator。而由于历史原因,Hashtable还使用了Enumeration的方式 。
  4. 哈希值的使用不同,HashTable直接使用对象的hashCode。而HashMap重新计算hash值。
  5. Hashtable和HashMap它们两个内部实现方式的数组的初始大小和扩容的方式。HashTable中hash数组默认大小是11,增加的方式是 old*2+1。HashMap中hash数组的默认大小是16,而且一定是2的指数。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值