Collection Framework
Collection:
- A collection is simply an object that represents a group of objects.
Collection framework:
- It provides a set of classes and interfaces that helps in managing a
group of objects.
What is the difference between Collection and Collections?
Collection is an “interface” which can be used to represent a group of
objects as a single entity.
Whereas “Collections is an utility class” present in java.util package to
define several utility methods for Collection objects.
Difference between Arrays and Collections ?
Arrays Collections
Arrays are fixed in size. Collections are growable in nature.
Memory point of view arrays are not Memory point of view collections
recommended are recommended
Performance point of view are Performance point of view are
arrays are recommended to use. arrays are recommended to use.
There is no underlying data Every collection class is
structure for arrays and hence there implemented based on some
is no readymade method support. standard data structure and hence
readymade method support is
available.
Arrays can hold both primitive and Collections can only hold object but
object types. not primitive types
LIst
====
ArrayList :
jIt is a resizable array implementation of the List interface.Unlike arrays,
which has fixed size, an ArrayList can change its size dynamically as
elements are added or removed. this makes it a popular choice hwen the
number of elements in a list isn’t known in advance.
Difference between ArrayList and Vector?
ArrayList Vector
No method is synchronized Every Method is synchronized
At a time multiple threads are At a time multiple threads are not
allowed to operate on ArrayList allow to operate on Vector object
object and hence AL obj is not and hence Vector obj is Thread
Thread safe. safe.
Relatively performance is high Relatively performance is low
because Threads are not required because Threads are required ot
to wait. wait.
It is non legacy and introduced in It is legacy and introduced in 1.0v.
1.2v.
Difference between Enumeration ,Iterator and ListIterator?
Property Enumeration Iterator ListIterator
Legacy Yes No NO
Applicable For Only Legacy Any Collection Only List
classes Object Objects
Movement Single Directional Single Bi-directional
Cursor(forward) Directional cursor
Cursor(forward)
How to get it ? By using By using iterator By using
v.elements() method listIterator()
method. method
Accessibility only read read and remove read/remove/re
place/add
Methods 1.hasMoreElement 1. hasNext() 9 methods
() 2. next()
2. nextElement() 3. remove()
Difference bw Comparable and Comparator ?
Comparable Comparator
Comparable interface present Comparator interface present in
in java.lang package java.util package.
It is meant for default natural It is meant for customized sorting order
sorting order.
contains only one method i.e. defines the following 2 methods.
compareTo() method. 1. public int compare(Object o1,Object
o2);
2. public boolean equals(Objectobj);
Whenever we are implementing
Comparator interface we have to
provide implementation only for
compare() methodImplementing
equals() method is optional because it
is already available from Object
What is comparTo method ?
- returns -ve if and only if obj1 has come before obj2
- returns +ve if and only if obj1 has to come after obj2
- returns 0 if and only if obj1 and obj2 are equal.
Set
====
Difference between HashSet, LInkedHashSet and TreeSet?
Property HashSet LinkedHashSet TreeSet
Data Structure Hashtable LinkedList + Balanced Tree
Hashtable
Insertion Order Not Preserved Preserved. Not preserved by
default.
Sorting Order NA NA Applicable.
Duplicate Not Allowed. Not Allowed. Not Allowed.
Objects
Heterogeneous Allowed. Allowed. Not Allowed.
Objects
Null Insertion Allowed Allowed For the empty TS
at the 1st
element, null
insertion is
possible in all
other cases we
get NPE.
Map
===
Difference between HashMap and LinkedHashMap?
HashMap LInkedHashMap
Hashtable LinkedList + Hashtable
Insertion order not preserved Insertion order ispreserved
introduced in 1.2 v introduced in 1.4 v
Difference between HashMap and Hashtable?
HashMap Hashtable
No method is synchronized Everymethod is synchronized
At a time multiple threads are At a time multiple threads are not
allowed to operate on allowed to operate on
HashMpaobject and hence hashtableobject and hence
HashMapobj is not Thread safe. Hashtable obj is Thread safe.
Relatively performance is high. Relatively performance is high.
Null is allowed for key and value. Null is not allowed for both key and
value otherwise we will get NPE
Non legacy and introduced in 1.2 v legacy
Explain the concept of Generics ?
To overcome the above problems of collections(type-safety, type
casting)sun people introduced generics concept in 1.5v hence the main
objectives of generics are:
1. To provide type safety to the collections.
2. To resolve type casting problems.
Generics concept is applicable only at compile time, at runtime there is no
such type of concept. Hence the following declarations are equal.
● ArrayList l=new ArrayList<String>();
● ArrayList l=new ArrayList<Integer>(); All are equal.
● ArrayList l=new ArrayList();
What is ENUM ?
- We can use enum to define a group of named constants.Enum
concept introduced in 1.5 versions.
- When compared with old languages enum java’s enum is more
powerful. By using enum we can define our own data types which are
also come enumerated data types.
Explain Internal implementation of enum?
• Internally enum’s are implemented by using class concept. Every enum
constant is a reference variable to that enum type object.
• Every enum constant is implicitly public static final always.
Enum vs switch statement:
• Until 1.4 versions the allowed types for the switch statement are byte,
short, char int. But from 1.5 version onwards in addition to this the
corresponding wrapper classes and enum type also allowed. That is from
1.5 version onwards we can use enum type as argument to switch
statement.
Enum vs inheritance:
• Every enum in java is the direct child class of java.lang.Enum class hence
it is not possible to extends any other enum.
• Every enum is implicitly final hence we can’t create child enum.
• Because of above reasons we can conclude inheritance concept is not
applicable for enum’s explicitly.
• It is abstract class and it is direct child class of “Object class” it
implements Serializable and Comparable.
values() method: Every enum implicitly contains a static values() method to
list all constants of enum.
Example: Beer[] b=Beer.values();
ordinal() method: Within enum the order of constants is important we can
specify by its ordinal value.
• We can find ordinal value(index value) of enum constant by using ordinal()
method.
Example: public int ordinal();
Example:
Regular expression
• A Regular Expression is a expression which represents a group of Strings
according to a
particular pattern.