YJSUN 发表于 2013-2-3 10:08:45

集合类

集合类是Java基础技术中十分重要的内容。Java集合类封装了集合中常用数据结构和算法,它犹如哪咤的烽火轮,功能强大,而且不用亲自制造(Made in America)。
首先看下面这张表,本文即通过它展开相关内容。


                                        
            
                                    Implementations
                                                Hash Table
                                    Resizable Array
                                    Balanced Tree
                                    Linked List
                                    Hash Table + LinkedList
                                                          Interfaces
                                    Set
                                    HashSet
                                    
            
                                    TreeSet
                                    
            
                                    LinkedHashSet
                                                List
                                    
            
                                    ArrayList
                                    
            
                                    LinkedList
                                    
            
                                                Map
                                    HashMap
                                    
            
                                    TreeMap
                                    
            
                                    LinkedHashMap
                      图1


Java集合类的种类
注意图1第二列,Java在设计集合结构时,把集合划成3类:第一种Set,普通的无序集;第二种List,偏序集;第三种Map,有序对的集合。其中Set和List都继承自Collection接口,Map没有从Collection继承。这样做也是没办法的事,因为Map并不是一个通常意义的集合,它的元素是一个key-value-pair,而Collection根本就不是Map,它还没有key的概念。虽然Collection和Map彼此没有继承关系,但它们有大量同名的方法。值得注意的是,Map提供了keySet()和values()两个方法,分别返回一个key的集合和值的集合。

示例1:通过对一个HashSet对象和一个ArrayList对象中元素使用跌代器可以显示Set与List中元素之间的顺序关系:Set中元素是无序的,List不是。
  
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.util.ArrayList;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.util.HashSet;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.util.Iterator;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.util.List;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.util.Set;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gifpublic class ElementOrder ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    public static void main(String[] args) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        Iterator it = null;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        Set ht = new HashSet();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        ht.add("htA");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        ht.add("htB");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        ht.add("htC");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        ht.add("htD");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        it = ht.iterator();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif        while (it.hasNext()) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            System.out.println(it.next());
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        List al = new ArrayList();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        al.add("alA");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        al.add("alB");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        al.add("alC");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        al.add("adD");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        it = al.iterator();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif        while (it.hasNext()) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            System.out.println(it.next());
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
页: [1]
查看完整版本: 集合类