Java Interview Questions - Collection Framework Part 3

Java Interview Questions - Collection Framework Part 3

Original   2017-09-02 Amuxia   Java   Companionship


21. The difference between ArrayList and Vector

        Both of these two classes implement the List interface (the List interface inherits the Collection interface), and they are both ordered collections, that is, the positions of the elements stored in the two collections are ordered, which is equivalent to a dynamic array , we can take out an element according to the position index number in the future, and the data in it is allowed to be repeated. This is the biggest difference from a collection such as HashSet. A collection such as HashSet cannot retrieve its elements according to the index number. , and no duplicate elements are allowed.

         The difference between ArrayList and Vector mainly includes two aspects:.
(1) Synchronization:

       Vector is thread-safe, which means that its methods are thread-synchronized, while ArrayList is thread-unsafe, and its methods are not thread-synchronized. If only one thread will access the collection, it is best to use ArrayList, because it does not consider thread safety, and the efficiency will be higher; if multiple threads will access the collection, it is best to use Vector, because we do not need our own Then think about and write thread-safe code.

(2) Data growth:

       Both ArrayList and Vector have an initial capacity size. When the number of elements stored in them exceeds the capacity, the storage space of ArrayList and Vector needs to be increased. Every time the storage space needs to be increased, not only one storage unit is added. , but to increase multiple storage units. The number of storage units added each time must achieve a certain balance between memory space utilization and program efficiency. Vector grows to twice the original size by default, while the growth strategy of ArrayList is not clearly specified in the document (from the source code, it is seen that the growth is 1.5 times the original size). Both ArrayList and Vector can set the initial space size, and Vector can also set the growth space size, while ArrayList does not provide a method to set the growth space.

    Summary: That is, Vector doubles the original size, and ArrayList increases 0.5 times the original size.


22. The difference between HashMap and Hashtable

        HashMap is a lightweight implementation of Hashtable (non-thread-safe implementation), they all complete the Map interface, the main difference is that HashMap allows null (null) keys (key), due to non-thread-safety, in the case of only one thread access , the efficiency is higher than Hashtable.

        HashMap allows null as the key or value of an entry, while Hashtable does not.

HashMap removes the contains method of Hashtable and changes it to containsvalue and containsKey. Because the contains method can be misleading.

        Hashtable inherits from Dictionary class, and HashMap is an implementation of Map interface introduced by Java 1.2.

        The biggest difference is that Hashtable's method is Synchronize, while HashMap is not. When multiple threads access Hashtable, you do not need to synchronize its methods yourself, but HashMap must provide synchronization for it.

     HashMap and HashTable are mainly from three aspects.
        1. Historical reasons: Hashtable is based on the old Dictionary class, and HashMap is an implementation of the Map interface introduced in Java 1.2
        . 2. Synchronization: Hashtable is thread-safe, that is to say, it is synchronized, while HashMap is thread-insecure. Yes , not synchronized
        3. Values: only HashMap lets you use nulls as the key or value of a table entry


23. What is the difference between List and Map?

        One is a collection that stores single-column data, and the other is a collection that stores double-column data such as keys and values. The data stored in List is ordered and allowed to be repeated; the data stored in Map is not ordered, and its keys cannot be Repeated, its value can be repeated.


24. Do List, Set, Map inherit from the Collection interface?

   List,Set是,Map不是

 

25、List、Map、Set三个接口,存取元素时,各有什么特点?

(这样的题比较考水平,两个方面的水平:一是要真正明白这些内容,二是要有较强的总结和表述能力。)

        首先,List与Set具有相似性,它们都是单列元素的集合,所以,它们有一个共同的父接口,叫Collection。Set里面不允许有重复的元素,即不能有两个相等(注意,不是仅仅是相同)的对象,即假设Set集合中有了一个A对象,现在我要向Set集合再存入一个B对象,但B对象与A对象equals相等,则B对象存储不进去,所以,Set集合的add方法有一个boolean的返回值,当集合中没有某个元素,此时add方法可成功加入该元素时,则返回true,当集合含有与某个元素equals相等的元素时,此时add方法无法加入该元素,返回结果为false。Set取元素时,不能细说要取第几个,只能以Iterator接口取得所有的元素,再逐一遍历各个元素。

       List表示有先后顺序的集合,注意,不是那种按年龄、按大小、按价格之类的排序。当我们多次调用add(Obje)方法时,每次加入的对象就像火车站买票有排队顺序一样,按先来后到的顺序排序。有时候,也可以插队,即调用add(intindex,Obj e)方法,就可以指定当前对象在集合中的存放位置。一个对象可以被反复存储进List中,每调用一次add方法,这个对象就被插入进集合中一次,其实,并不是把这个对象本身存储进了集合中,而是在集合中用一个索引变量指向这个对象,当这个对象被add多次时,即相当于集合中有多个索引指向了这个对象,如图x所示。List除了可以用Iterator接口取得所有的元素,再逐一遍历各个元素之外,还可以调用get(index i)来明确说明取第几个。

       Map与List和Set不同,它是双列的集合,其中有put方法,定义如下:put(obj key,obj value),每次存储时,要存储一对key/value,不能存储重复的key,这个重复的规则也是按equals比较相等。取则可以根据key获得相应的value,即get(Object key)返回值为key所对应的value。另外,也可以获得所有的key的结合,还可以获得所有的value的结合,还可以获得key和value组合成的Map.Entry对象的集合。

 List以特定次序来持有元素,可有重复元素。Set无法拥有重复元素,内部排序。Map保存key-value值,value可多值。


26、说出ArrayList,Vector,LinkedList的存储性能和特性

        ArrayList和Vector都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引元素,但是插入元素要涉及数组元素移动等内存操作,所以索引数据快而插入数据慢,Vector由于使用了synchronized方法(线程安全),通常性能上较ArrayList差。而LinkedList使用双向链表实现存储,按序号索引数据需要进行前向或后向遍历,索引就变慢了,但是插入数据时只需要记录本项的前后项即可,所以插入速度较快。

        LinkedList也是线程不安全的,LinkedList提供了一些方法,使得LinkedList可以被当作堆栈和队列来使用。


27、去掉一个Vector集合中重复的元素

Vector newVector = new Vector();

For (int i=0;i<vector.size();i++)

{

Object obj = vector.get(i);

       if(!newVector.contains(obj);

             newVector.add(obj);

}

还有一种简单的方式,利用了Set不允许重复元素:

HashSetset = new HashSet(vector);


28、Collection和Collections的区别。

        Collection是集合类的上级接口,继承他的接口主要有Set和List.

        Collections是针对集合类的一个帮助类,他提供一系列静态方法实现对各种集合的搜索、排序、线程安全化等操作。


29、Set里的元素是不能重复的,那么用什么方法来区分重复与否呢?是用==还是equals()?它们有何区别?

        Set里的元素是不能重复的,元素重复与否是使用equals()方法进行判断的。

        ==和equal区别也是考烂了的题,这里说一下:

        ==操作符专门用来比较两个变量的值是否相等,也就是用于比较变量所对应的内存中所存储的数值是否相同,要比较两个基本类型的数据或两个引用变量是否相等,只能用==操作符。

        equals方法是用于比较两个独立对象的内容是否相同,就好比去比较两个人的长相是否相同,它比较的两个对象是独立的。 

        比如:两条new语句创建了两个对象,然后用a/b这两个变量分别指向了其中一个对象,这是两个不同的对象,它们的首地址是不同的,即a和b中存储的数值是不相同的,所以,表达式a==b将返回false,而这两个对象中的内容是相同的,所以,表达式a.equals(b)将返回true。


30、你所知道的集合类都有哪些?主要方法?

        最常用的集合类是 List 和 Map。 List的具体实现包括 ArrayList和 Vector,它们是可变大小的列表,比较适合构建、存储和操作任何类型对象的元素列表。 List适用于按数值索引访问元素的情形。

        Map 提供了一个更通用的元素存储方法。 Map集合类用于存储元素对(称作"键"和"值"),其中每个键映射到一个值。

        它们都有增删改查的方法。

        对于set,大概的方法是add,remove, contains等

        对于map,大概的方法就是put,remove,contains等

        List类会有get(int index)这样的方法,因为它可以按顺序取元素,而set类中没有get(int index)这样的方法。List和set都可以迭代出所有元素,迭代时先要得到一个iterator对象,所以,set和list类都有一个iterator方法,用于返回那个iterator对象。map可以返回三个集合,一个是返回所有的key的集合,另外一个返回的是所有value的集合,再一个返回的key和value组合成的EntrySet对象的集合,map也有get方法,参数是key,返回值是key对应的value,这个自由发挥,也不是考记方法的能力,这些编程过程中会有提示,结合他们三者的不同说一下用法就行。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324603897&siteId=291194637