`

Java-Collections-经典20题

阅读更多
Collections:



1.Why Map interface doesn’t extend Collection interface?
  • Set is unordered collection and does not allows duplicate elements.
  • List is ordered collection allows duplicate elements.
  • Where as Map is key-value pair.
  • It is viewed as set of keys and collection of values.
  • Map is a collection of key value pairs so by design they separated from collection interface.


2.What is difference between HashMap and Hashtable?
  • Synchronization or Thread Safe
  • Null keys and null values
  • Iterating the values
  • Default Capacity


3.Differences between comparable and comparator?
  • Comparable Interface is actually from java.lang package.
  • It will have a method compareTo(Object obj)to sort objects
  • Comparator Interface is actually from java.util package.
  • It will have a method compare(Object obj1, Object obj2)to sort objects

Comparable
A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances.

Comparator
A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.util.Comparator interface.



4.How can we sort a list of Objects?
  • To sort the array of objects we will use  Arrays.sort() method.
  • If we need to sort collection of object we will use Collections.sort().


5.What is difference between fail-fast and fail-safe?
  • Fail fast is nothing but immediately report any failure. whenever a problem occurs fail fast system fails.
  • In java Fail fast iterator while iterating through collection of objects sometimes concurrent modification exception will come there are two reasons for this.
  • If one thread is iterating a collection and another thread trying to modify the collection.
  • And after remove() method call if we try to modify collection object


6. What is difference between Iterator ,ListIterator and Enumeration?
  • Enumeration interface implemented in java 1.2 version.So Enumeration is legacy interface.
  • Enumeration uses elements() method.
  • Iterator is implemented on all Java collection classes.
  • Iterator uses iterator() method.
  • Iterator can traverse in forward direction only.
  • ListIterator is implemented only for List type classes
  • ListIterator uses listIterator() method.


7.What is difference between Set and List in Java?
  • A set is a collection that allows unique elements.
  • Set having no index.
  • Set allows only one null value.
  • Set having classes like :
  •                                  HashSet
                                     LinkedHashMap
                                     TreeSet
  • List having index.
  • List allows n number of null values.
  • List will display Insertion order with index.
  • List having classes like :
  •                                  Vector
                                     ArrayList
                                     LinkedList


8.Differences between arraylist and vector?
  • Vector was introduced in  first version of java . that's the reason only vector is legacy class.
  • ArrayList was introduced in java version1.2, as part of java collections framework.
  • Vector is  synchronized.
  • ArrayList is not synchronized.


9.What are the classes implementing List interface?
  • ArrayList    
  • LinkedList    
  • Vector


10. Which all classes implement Set interface ?
  • HashSet
  • LinkedHashSet
  • TreeSet


11.How to make a collection thread safe?
  • Vector, Hashtable, Properties and Stack are synchronized classes, so they are thread-safe and can be used in multi-threaded environment.
  • By using java.util.Collections.synchronizedList(list) we can make list classes thread safe.
  • By using java.util.Collections.synchronizedSet(set)  we can make set classes thread safe.


12.Can a null element added to a TreeSet or HashSet?
  • One null element can be added to hashset.
  • TreeSet also allow null value once.


13. Explain Collection’s interface hierarchy?


14.Which design pattern Iterator follows?
  • Iterator design pattern


15.Which data structure HashSet implements
  • Hashset implements hashmap internally.


16.Why doesn't Collection extend Cloneable and Serializable?
  • List and Set and queue extends Collection interface.
  • SortedMap extends Map interface.


17.What is the importance of hashCode() and equals() methods? How they are used in Java?
  • equals() and hashcode() methods defined in "object" class.
  • If equals() method return true on comparing two objects then hashcode() of those two objects must be same.


18.What is difference between array & arraylist?
  • Array is collection of similar type of objects and fixed in size.
  • Arraylist is collection of homogeneous and heterogeneous elements.


19.What is the Properties class?
  • Properties is a subclass of Hashtable. It is used to maintain lists of values in which the key and the value is String.


20.How to convert a string array to arraylist?
  • Using java.util.Arrays.asList();

import java.util.Arrays;  
import java.util.List;  
import java.util.ArrayList;  
public class StringArrayTest  
{  
   public static void main(String[] args)  
   {  
      String[] words = {"ace", "boom", "crew", "dog", "eon"};  

      List<String> wordList = Arrays.asList(words);  

      for (String e : wordList)  
      {  
         System.out.println(e);  
      }  
   }  
}


21.Difference between HashMap and TreeMap?
  • TreeMap is an example of a SortedMap, which means that the order of the keys can be sorted, and when iterating over the keys, you can expect that they will be in order.
  • HashMap on the other hand, makes no such guarantee. Therefore, when iterating over the keys of a HashMap, you can't be sure what order they will be in.
  • HashMap is more time-efficient. A TreeMap is more space-efficient.
  • TreeMap only works with Comparable objects, HashMap only works with objects with a suitable hashCode() implementation.









______________________________________________________________

- 转自:http://www.instanceofjava.com/2015/07/collections-interview-questions-java.html



-
  • 大小: 28.3 KB
  • 大小: 38.2 KB
分享到:
评论

相关推荐

    java-collections-framework1016

    ### Java Collections Framework 1016 #### 一、教程概览 本教程由developerWorks提供,旨在深入探讨Java Collections Framework。它不仅适用于初学者,也适合具有一定经验的开发人员。教程从简单的编程示例开始,...

    commons-collections-3.2.1.jar.zip

    java jar包,亲测试可用 安全,可以对其进行数字签名,只让能够识别数字签名的用户使用里面的东西。 加快下载速度; 压缩,使文件变小,与ZIP压缩机制完全相同。 包封装。能够让JAR包里面的文件依赖于统一版本的...

    google-collections-1.0.jar.zip

    java jar包,亲测试可用 安全,可以对其进行数字签名,只让能够识别数字签名的用户使用里面的东西。 加快下载速度; 压缩,使文件变小,与ZIP压缩机制完全相同。 包封装。能够让JAR包里面的文件依赖于统一版本的...

    commons-collections-3.1.jar.zip

    java jar包,亲测试可用 安全,可以对其进行数字签名,只让能够识别数字签名的用户使用里面的东西。 加快下载速度; 压缩,使文件变小,与ZIP压缩机制完全相同。 包封装。能够让JAR包里面的文件依赖于统一版本的...

    commons-collections-2.1.1.jar.zip

    java jar包,亲测试可用 安全,可以对其进行数字签名,只让能够识别数字签名的用户使用里面的东西。 加快下载速度; 压缩,使文件变小,与ZIP压缩机制完全相同。 包封装。能够让JAR包里面的文件依赖于统一版本的...

    JAVA-Puzzles-section.zip_java笔试题

    Java智力题是考察开发者逻辑思维能力和基础编程素养的重要方式,特别是在面试或笔试环节。这份"JAVA-Puzzles-section.zip"压缩包中包含了一份名为"智力题部分.doc"的文档,很可能是整理了一些与Java相关的逻辑谜题和...

    java-test.zip_java面试题

    "java-test.zip_java面试题"这个压缩包文件正是为此目的而准备的,它包含了多家大型企业的经典面试题目,帮助求职者们在面试前做好充分的准备。 "享受一种生活 java面试题及答案(基础题122道,代码题19道).htm"这...

    JAVA-SSH面试题

    ### JAVA-SSH面试题知识点详解 #### Java基础知识 1. **面向对象的特征** - **抽象** 抽象是面向对象编程的核心概念之一,指的是在设计阶段仅关注对象的关键属性和行为,忽略不必要的细节。它可以帮助我们构建...

    百度地图开发java源码-github-collections:github-collections

    数据挖掘18大算法实现以及其他相关经典DM算法 算法可视化 All Algorithms implemented in Java. All Algorithms implemented in Python. 设计模式 java设计模式 - Design patterns implemented in Java 人工智能 An ...

    java-08下考证试题

    8. **Java API**:熟悉常用的Java类库,如String、Date、Collections、Math等,以及常用的数据结构和算法。 9. **Java Swing或JavaFX**:对于GUI编程,考生可能需要了解如何使用Swing库创建桌面应用程序,或者使用...

    JAVA开发实战经典-课后习题答案-李兴华.pdf

    ### Java 开发实战经典课后习题答案知识点总结 #### Java 简介 - **文件后缀**: Java源程序文件的后缀是`.java`,而编译后生成的Java字节码文件的后缀为`.class`。 - **程序入口**: Java程序的执行总是从`main()`...

    commons-collections-3.1.zip jar包

    java jar包,亲测试可用 安全,可以对其进行数字签名,只让能够识别数字签名的用户使用里面的东西。 加快下载速度; 压缩,使文件变小,与ZIP压缩机制完全相同。 包封装。能够让JAR包里面的文件依赖于统一版本的...

    java 经典试题及答案

    Java编程语言中的知识点涵盖广泛,这里我们主要讨论标题和描述中提到的一些重点: 1. **final, finally, finalize的区别** - `final` 关键字用于声明不可变的类、变量或方法。对于类,一旦声明为final,就不能被...

    2014-蓝桥杯预赛-Java本科-B组真题

    "2014-蓝桥杯预赛-Java本科-B组真题"是针对Java编程语言的一次竞赛,旨在测试参赛者在Java编程、数据结构、算法应用等方面的技能。蓝桥杯比赛通常涵盖多轮,预赛是第一阶段,旨在筛选出在软件开发和问题解决方面有...

    算法、数据结构和编程面试示例 - Java - 下载.zip

    Java语言提供了丰富的库支持,如Collections.sort()用于排序,而我们还需要理解和掌握基本的算法思想,如分治法、动态规划、贪心算法和回溯法。文件中的学习资料可能包括经典的排序算法(冒泡、插入、选择、快速、...

    java-leetcode题解之第972题最接近原点的K个点.zip

    标题中的“java-leetcode题解之第972题最接近原点的K个点”表明这是一份关于使用Java编程语言解决LeetCode在线编程挑战平台上的第972题的解决方案。LeetCode是一个广受欢迎的网站,程序员们在上面通过解决算法问题来...

Global site tag (gtag.js) - Google Analytics