Skip to main content

All difference Question in java(common difference) (Collections)



1)Difference Between Interfaces And Abstract class?

  1. All the methods declared in the Interface are Abstract,where as abstract class must have atleast one abstract method and others may be concrete.
  2. In abstract class keyword abstract must be used for method, where as in Interface we need not use the keyword for methods.
  3. Abstract class must have Sub class, where as Interface can’t have sub classes. 
  4. An abstract class can extend only one class, where as an Interface can extend more than one.
2)Difference between Arrays and Vectors?           

  • Because Vectors are dynamically-allocated, they offer a relatively memory-efficient way of handling lists whose size could change drastically. Arrays are fixed in size and it's wastage of memory problem.
  • A Vector might be a good choice for such an arrangement. Moreover, Vectors have some useful member functions that make coding simpler. While in array is the bad choice of the arrangement and the code become more complex.
  • Vector is the double the size of the memory. Array is the 50percent of the size of the memory.
3)Difference between ArrayList and LinkedList?                

  • ArrayList is faster than LinkedList it means while we randomly accessing the elements.
  • LinkedList is faster than ArrayList for deletion. I understand this one. ArrayList's slower since the internal backing-up array needs to be reallocated.

4)Difference between Hashmap and Treemap?

  • HashMap is implemented as a hash table, and there is no ordering on keys or values.
  • TreeMap is implemented based on red-black tree structure, and it is ordered by the key.
  • Hashmap is based on equals() and hashcode();
  • Treemap is sorted by keys, the objects of the keys are able to compare with each other.
5)Difference between LinkedHashMap and HashTable?
  • LinkedHashMap preserves the insertion order
  • Hashtable is synchronized, in contrast to HashMap.
  • LinkedHashMap is a subclass of HashMap. That means it inherits the features of HashMap.
  • The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls(only one null).

6)Difference between Comparable and Comparator?

Comparable:

                        A comparable object is capable of comparing itself with another object. The class itself must implement 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.

7)Difference between Iterator and ListIterator?

1) An iterator is used for traversing List and Set both. ListIterator to traverse List only, we cannot traverse Set using ListIterator.

2) We can traverse in only forward direction using Iterator. ListIterator, we can traverse a List in both the directions (forward and Backward).

3) We cannot obtain indexes while using Iterator. We can obtain indexes at any point of time while traversing a list using ListIterator. The methods nextIndex() and previousIndex() are used for this purpose.

4) We cannot add an element to the collection while traversing it using Iterator, it throws ConcurrentModificationException when you try to do it. We can add an element at any point in time while traversing a list using ListIterator.

5) We cannot replace the existing element value when using Iterator.


8)Difference between Queue and Priority queue?

         A queue is a data structure that contains some elements, and the only way the contents of the structure change are by popping elements off of the front of the queue, or by pushing elements onto the back. Elements are popped off in the order that they’re pushed, so,  Example:the first element that’s pushed will be the first element that’s popped, and the last will be the last. 

          In a priority queue, each element has some type of value that can be ordered with respect to other values of the same type (like a number, or a string [strings can be ordered lexicographically]). As elements are pushed onto the back or popped off of the front, the priority queue re-orders itself to make the front of the queue contain the element with the smallest value (or the largest, if the priority is defined to be in descending order).

Comments

Popular posts from this blog

Commonly asked Top 26 Java Interview Questions

1.what is a transient variable? A transient variable is a variable that may not be serialized. If you don't want to serialize any objects then we have to use Transient Keyword. 2. Why do threads block on I/O? Threads block on i/o (that enters the waiting state) so that other threads may execute while the i/o Operation is performed. So your blocked thread of execution blocks only that: the thread of execution. 3. How are Observer and Observable used? Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects(refer 1.4). 4. What is synchronization and why is it important?                      With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Withou

MYSQL Interview question..

MySQL is an open source relational database management system RDBMS based on Structured Query Language SQL MySQL runs on virtually all platforms, including Linux, Unix, Windows. Although it can be used in a wide range of applications, MySQL is most often associated with web-based applications and online publishing and is an important component of an open source enterprise. 1) What is SQL? SQL stands for Structured Query Language. SQL was initially developed at IBM in 1970s. SQL is the standard language to communicate with relational database management systems like Oracle, MS Access, MS SQL Server, MySQL, DB2 etc. It is used to connect the objects with the database . 2)what is the Purpose of SQL? • SQL is used to Create New Databases. • SQL is used to Create New Tables in a Database. • SQL is used to Insert, update, Delete records in a Database. • SQL is used to Retrieve data from a Database. • SQL is used to execute queries against a Database. • SQL can set permis

Top Most Important Multithreading interview question and answers

1) What is Thread in java? Threads consumes CPU in best possible manner, hence enables multi processing. Multi threading reduces idle time of CPU which improves performance of application. Thread are light weight process. It belongs to java.lang package. It can run mutiple threads simutaneously .                                                               2) How to implements threads in java?         By   implementing java.lang.Runnable interface or extending java.lang.Thread class and then extending run method.   Thread creation by implementing java.lang.Runnable interface.      And then create Thread object by calling constructor and passing reference of Runnable interface i.e. runnable object          Thread thread = new Thread(runnable); 3) We should implement Runnable interface or extend Thread class. What are differences between implementing Runnable and extending Thread?                 you must extend Thread only when you are look