常用数据结构 List set map

来源:互联网 发布:行知天下参考答案2017 编辑:程序博客网 时间:2024/04/27 22:45

常用数据结构 List set map

<div style="color: rgb(75, 75, 75);">转载地址:http://www.blogjava.net/EvanLiu/archive/2007/11/12/159884.html</div><div style="color: rgb(75, 75, 75);"></div><p style="color: rgb(75, 75, 75);"><img alt="" src="http://p.blog.csdn.net/images/p_blog_csdn_net/EvanLiu/map.bmp" style="border: none; max-width: 100%;" /></p><p style="color: rgb(75, 75, 75);">下面是我自己画的,关系画得没上面好,但我自己看着清楚些</p><p style="color: rgb(75, 75, 75);"><img alt="" src="http://p.blog.csdn.net/images/p_blog_csdn_net/EvanLiu/map2.JPG" style="border: none; max-width: 100%;" />还有一张下载来的:<img alt="" src="http://www.blogjava.net/images/blogjava_net/evanliu/f.jpg" border="0" height="419" width="905" style="border: none; max-width: 100%;" /></p><table border="1" cellpadding="0" cellspacing="0"><tbody><tr><td colspan="2" valign="top" width="147"> </td><td valign="top"><p><span style="font-size: 14px;">有序否</span></p></td><td valign="top"><p><span style="font-size: 14px;">允许元素重复否</span></p></td></tr><tr><td colspan="2" valign="top" width="147"><p><span style="font-family: 'Times New Roman'; font-size: 14px;">Collection</span></p></td><td valign="top"><p><span style="font-size: 14px;">否</span></p></td><td valign="top"><p><span style="font-size: 14px;">是</span></p></td></tr><tr><td colspan="2" valign="top" width="147"><p><span style="font-family: 'Times New Roman'; font-size: 14px;">List</span></p></td><td valign="top"><p><span style="font-size: 14px;">是</span></p></td><td valign="top"><p><span style="font-size: 14px;">是</span></p></td></tr><tr><td rowspan="3"><p><span style="font-family: 'Times New Roman'; font-size: 14px;">Set</span></p></td><td valign="top"><p><span style="font-family: 'Times New Roman'; font-size: 14px;">AbstractSet</span></p></td><td rowspan="2"><p><span style="font-size: 14px;">否</span></p></td><td rowspan="3"><p><span style="font-size: 14px;">否</span></p></td></tr><tr><td valign="top"><p><span style="font-family: 'Times New Roman'; font-size: 14px;">HashSet</span></p></td></tr><tr><td valign="top"><p><span style="font-family: 'Times New Roman'; font-size: 14px;">TreeSet</span></p></td><td valign="top"><p><span style="font-size: 14px;">是(用二叉树排序)</span></p></td></tr><tr><td rowspan="3" valign="top"><p><span style="font-family: 'Times New Roman'; font-size: 14px;">Map</span></p></td><td valign="top"><p><span style="font-family: 'Times New Roman'; font-size: 14px;">AbstractMap</span></p></td><td rowspan="2"><p><span style="font-size: 14px;">否</span></p></td><td rowspan="3" valign="top"><p><span style="font-size: 14px;">使用<span style="font-family: 'Times New Roman';">key-value</span>来映射和存储数据,<span style="font-family: 'Times New Roman';">Key</span>必须惟一,<span style="font-family: 'Times New Roman';">value</span>可以重复</span></p></td></tr><tr><td valign="top"><p><span style="font-family: 'Times New Roman'; font-size: 14px;">HashMap</span></p></td></tr><tr><td valign="top"><p><span style="font-family: 'Times New Roman'; font-size: 14px;">TreeMap</span></p></td><td valign="top"><p><span style="font-size: 14px;">是(用二叉树排序)</span></p></td></tr></tbody></table><p style="color: rgb(75, 75, 75);">几个面试常见问题:1.Q:ArrayList和Vector有什么区别?HashMap和HashTable有什么区别?   A:Vector和HashTable是线程同步的(synchronized)。性能上,ArrayList和HashMap分别比Vector和Hashtable要好。2.Q:大致讲解java集合的体系结构   A:List、Set、Map是这个集合体系中最主要的三个接口。      其中List和Set继承自Collection接口。      Set不允许元素重复。HashSet和TreeSet是两个主要的实现类。      List有序且允许元素重复。ArrayList、LinkedList和Vector是三个主要的实现类。      Map也属于集合系统,但和Collection接口不同。Map是key对value的映射集合,其中key列就是一个集合。key不能重复,但是value可以重复。HashMap、TreeMap和Hashtable是三个主要的实现类。      SortedSet和SortedMap接口对元素按指定规则排序,SortedMap是对key列进行排序。3.Q:Comparable和Comparator区别    A:调用java.util.Collections.sort(List list)方法来进行排序的时候,List内的Object都必须实现了Comparable接口。        java.util.Collections.sort(List list,Comparator c),可以临时声明一个Comparator 来实现排序。        Collections.sort(imageList, new Comparator() {            public int compare(Object a, Object b) {                int orderA = Integer.parseInt( ( (Image) a).getSequence());                int orderB = Integer.parseInt( ( (Image) b).getSequence());                return orderA - orderB;           }        });        如果需要改变排列顺序        改成return orderb - orderA 即可。4.Q:简述equals()和hashCode()    A:...不知道。下回分解public interface <span style="color: rgb(0, 128, 128);">Collection </span>            extends Iterablepublic interface <span style="color: rgb(0, 128, 128);">List</span>            extends Collectionpublic abstract class <span style="color: rgb(0, 128, 128);">AbstractList </span>            extends AbstractCollection            implements Listpublic class<span style="color: rgb(0, 128, 128);">Vector </span>            extends AbstractList             implements List,                                    RandomAccess,                                    java.lang.Cloneable,                                    java.io.Serializable<strong>基于Array是“<span style="color: rgb(255, 102, 0);">sychronized</span>”的public class <span style="color: rgb(0, 128, 128);">ArrayList</span>         </strong>extends <strong>AbstractList        </strong>implements <strong>List,                           RandomAccess,                           Cloneable,                           java.io.Serializable基于Array</strong>ArrayList是<span style="color: rgb(255, 102, 0);">非同步</span>的。所以在<span style="color: rgb(255, 102, 0);">性能</span>上要比Vector优越一些public class <span style="color: rgb(0, 128, 128);">LinkedList</span>        extends AbstractSequentialList        implements List,                           Queue,                           Cloneable,                           java.io.Serializable不基于Array<strong>基于Array的List(Vector,ArrayList)适合查询,而LinkedList(链表)适合添加,删除操作List基本上都是以Array为基础。但是Set则是在HashMap的基础上来实现的,这个就是Set和List的根本区别public abstract class <span style="color: rgb(0, 128, 128);">AbstractSet </span>    extends AbstractCollection     implements Set</strong>public class <span style="color: rgb(0, 128, 128);">HashSet</span>    extends AbstractSet    implements Set, Cloneable, java.io.Serializable<strong>HashSet的存储方式是把<span style="color: rgb(255, 102, 0);">HashMap</span>中的Key作为Set的对应存储项</strong>public class <span style="color: rgb(0, 128, 128);">LinkedHashSet</span>    extends HashSet    implements Set, Cloneable, java.io.Serializablepublic class <span style="color: rgb(0, 128, 128);">TreeSet</span>    extends AbstractSet    implements SortedSet, Cloneable, java.io.Serializable<strong>它是通过<span style="color: rgb(255, 102, 0);">SortedMap</span>来实现的</strong>public interface <span style="color: rgb(0, 128, 128);">Map</span><K,V>public abstract class <span style="color: rgb(0, 128, 128);">AbstractMap</span><K,V>     implements Map<K,V>public class <span style="color: rgb(0, 128, 128);">HashMap</span><K,V>    extends AbstractMap<K,V>    implements Map<K,V>, Cloneable, Serializablepublic class <span style="color: rgb(0, 128, 128);">TreeMap</span><K,V>    extends AbstractMap<K,V>    implements SortedMap<K,V>, Cloneable, java.io.SerializableHashMap通过hashcode对其内容进行快速查找,而TreeMap中所有的元素都保持着某种固定的顺序,如果你需要得到一个有序的结果你就应该使用TreeMap(HashMap中元素的排列顺序是不固定的)</p>
</pre><pre id="best-content-60134578" class="best-text mb-10" name="code" style="white-space: pre-wrap; word-wrap: break-word; margin-top: 0px; margin-bottom: 10px; padding: 0px;"><span style="font-size: 18px;"><strong>看到百度知道里面有个回复比较的好,先复制过来:地址为http://zhidao.baidu.com/question/16113509.html</strong></span><strong></strong>
<strong>提问是:</strong><h1 class="mb-5" style="margin: 0px 0px 5px; padding: 0px; font-size: 16px; line-height: 26px; font-family: 'Microsoft YaHei', SimHei, arial; word-break: break-all;"><span class="ask-title " style="display: inline-block; width: 595px; overflow: hidden;">谁能说说Java中的Set List Map存储方式个各有什么不同?</span></h1>
<strong><span style="white-space: pre;"></span>List接口对Collection进行了简单的扩充,它的具体实现类常用的有ArrayList和LinkedList。你可以将任何东西放到一个List容器中,并在需要时从中取出。ArrayList从其命名中可以看出它是一种类似数组的形式进行存储,因此它的随机访问速度极快,而LinkedList的内部实现是链表,它适合于在链表中间需要频繁进行插入和删除操作。在具体应用时可以根据需要自由选择。前面说的Iterator只能对容器进行向前遍历,而ListIterator则继承了Iterator的思想,并提供了对List进行双向遍历的方法。   <span style="white-space: pre;"></span>Set接口也是Collection的一种扩展,而与List不同的时,在Set中的对象元素不能重复,也就是说你不能把同样的东西两次放入同一个Set容器中。它的常用具体实现有HashSet和TreeSet类。HashSet能快速定位一个元素,但是你放到HashSet中的对象需要实现hashCode()方法,它使用了前面说过的哈希码的算法。而TreeSet则将放入其中的元素按序存放,这就要求你放入其中的对象是可排序的,这就用到了集合框架提供的另外两个实用类Comparable和Comparator。一个类是可排序的,它就应该实现Comparable接口。有时多个类具有相同的排序算法,那就不需要在每分别重复定义相同的排序算法,只要实现Comparator接口即可。集合框架中还有两个很实用的公用类:Collections和Arrays。Collections提供了对一个Collection容器进行诸如排序、复制、查找和填充等一些非常有用的方法,Arrays则是对一个数组进行类似的操作。<span style="white-space: pre;"></span>Map是一种把键对象和值对象进行关联的容器,而一个值对象又可以是一个Map,依次类推,这样就可形成一个多级映射。对于键对象来说,像Set一样,一个Map容器中的键对象不允许重复,这是为了保持查找结果的一致性;如果有两个键对象一样,那你想得到那个键对象所对应的值对象时就有问题了,可能你得到的并不是你想的那个值对象,结果会造成混乱,所以键的唯一性很重要,也是符合集合的性质的。当然在使用过程中,某个键所对应的值对象可能会发生变化,这时会按照最后一次修改的值对象与键对应。对于值对象则没有唯一性的要求。你可以将任意多个键都映射到一个值对象上,这不会发生任何问题(不过对你的使用却可能会造成不便,你不知道你得到的到底是那一个键所对应的值对象)。Map有两种比较常用的实现:HashMap和TreeMap。HashMap也用到了哈希码的算法,以便快速查找一个键,TreeMap则是对键按序存放,因此它便有一些扩展的方法,比如firstKey(),lastKey()等,你还可以从TreeMap中指定一个范围以取得其子Map。键和值的关联很简单,用pub(Object key,Object value)方法即可将一个键与一个值对象相关联。用get(Object key)可得到与此key对象所对应的值对象。</strong>
0 0
原创粉丝点击