在Java中类集主要分为两类:Collection集合和Map集合,集合主要用来存数据和取数据。
1、Collection集合定义:
Interface Collection<E>
Collection集合中定义许多可以操作的数据的方法,常用方法主要有:
public boolean add(E e) 向集合中保存数据 ;public void clear() 清空集合 ;
public boolean contains(Object o) 判断数据是否存在 ;
public boolean isEmpty() 判断是否为空集合 ;
public boolean remove(Object o) 删除数据 ;
public int size() 取得集合中元素个数 ;
public Object[] toArray() 将集合中的数据以对象数组的形式返回 ;
public Iterator<E> iterator() 取得Iterator对象。
通常开发中不会直接使用Collection集合,而是使用其子接口:interface List<T>和interface Set<T>。
其中,List集合实现了对Collection集合方法的扩充,主要有如下方法:
public E get(int index) 根据索引取得对应的数据
public E set(int index,E element) 根据索引修改数据
说明:List集合中可以保存重复数据,Set集合中不能保存重复数据,如果数据重复则会覆盖
Set集合实现了Collection接口中的标准定义,没有对其扩充。
(1)、List集合(数据可重复)
List有三个子类:ArrayList<T>、Vector<T>、LinkedList<T>
其中最为常用的是ArrayList子类(90%),通过实例化子类向上转型为List,之后对数据进行操作。
ArrayList与Vector的区别在于:Vector类中的方法使用了synchronized声明,因此,Vector可用于多线程同步处理,属于线程安全操作,性能较低;
ArrayList属于异步处理非线程安全操作,性能高。
ArrayList与LinkedList的区别在于:在ArrayList中保存的是数组,实现了数组包装 ;而LinkedList实现的是链表的包装。
(2)、Set集合(数据不重复)
Set有两个子类:HashSet<T>和TreeSet<T>
其中最为常用的是HashSet,特点是保存的数据无序,而TreeSet保存的是有序数据,之所以有序,是由于要保存数据的类实现了Comparable接口覆写了其中的compareTo()方法。Set集合保存数据的不重复在于:要保存的数据的类实现的Object类中的hashCode()和equals();
例1:使用以上方法操作集合
public class TestDemo {
public static void main(String[] args) {
List<String> all = new ArrayList<String>() ; //定义集合
all.add("hello") ; //向集合中保存数据
all.add("world") ;
all.add("java") ;
all.add("oracle") ;
System.out.println(all.size()); //取得集合长度
System.out.println(all.contains("java")); //判断数据是否存在
System.out.println(all.isEmpty()); //判断集合是否为空集合
System.out.println(all.get(2)); //根据索引取得数据
System.out.println(all);
}
}
2、Map集合定义:
Interface Map<K,V>
Map集合采用了 “key=value”的形式进一步的被包装成interface Map.Entry<K,V>(Map的内部接口)的对象实现保存。在Map.Entry<K,V>中有两个常用方法:
public K getKey() 取得key
public V getValue() 取得value
Map中常用方法:
public V put(K key,V value) 向Map集合中保存数据
public V get(Object key) 根据key取得对应的数据
public Set<Map.Entry<K,V>> entrySet() 将Map集合变成Set集合
Map集合有三个子类:HashMap<K,V>、Hashtable<K,V>和TreeMap<K,V>(不常用)
其中HashMap最为常用(90%),HashMap和Hashtable最大的区别在于Hashtable中使用了synchronized定义了方法,因此,Hashtable数据线程安全操作,性能较低;HashMap是非线程安全操作,性能高。
3、取出集合数据:
集合数据取出的方式有四种:Iterator(95%)、Enumeration(4.96%)、ListIterator、foreach,最为常用的是前两种,Iterator常用方法:
public boolean hasNext() 判断是否有下一个数据
public E next() 取出数据
其中Iterator的对象由Collection集合中的iterator()方法返回Iterator的对象,之后,结合while()循环使用。
Enumeration常用方法:
public boolean hasMoreElements() 判断是否有数据
public E nextElement() 取出数据
Enumeration对象的取得依靠的是List的子类Vector中的elements()方法:public Enumeration<E> elements(),之后结合while()循环使用。
例2:取出List集合数据
public class TestDemo {
public static void main(String[] args) {
List<String> all = new ArrayList<String>() ; //定义集合
all.add("hello") ; //向集合中保存数据
all.add("world") ;
Iterator<String> iter = all.iterator() ; //取得Iterator对象
<span style="color:#ff0000;">while (iter.hasNext()) { //取出集合数据
System.out.println(iter.next());
}</span>
}
}
Map集合取出数据的说明:
1)首先,使用Map中的entySet()方法将Map集合变为Set集合;
2)其次,使用Set接口中的iterator()方法将Set集合中的数据取出,此时数据为Mep.Entry<K,V>的对象;
3)最后,使用Mep.Entry接口中的getKey()和getValue()取出相应的key和value值。
例3:取出Map集合数据
public class TestDemo {
public static void main(String[] args) {
Map<String,String> map = new Hashtable<String,String>() ; //定义Map集合
map.put("apple", "苹果") ; //向Map集合中保存数据
Set<Map.Entry<String, String>> set = map.entrySet() ; //将Map集合变成Set集合
Iterator<Map.Entry<String, String>> iter = set.iterator() ; //取得Iterator对象
while (iter.hasNext()) { //取出数据
Map.Entry<String, String> me = iter.next() ; //取得Map.Entry对象
System.out.println(me.getKey() + " = " + me.getValue()); //分别取出key和value
}
}
}
注意:使用Collection集合的主要目的是为了输出数据,使用Map集合主要是为了查询数据。