Uploaded by Sivashankari R

Collections in JAVA

advertisement
Collections in JAVA
• Any group of individual objects which are represented as a
single unit is known as the collection of the objects
• The Collection interface (java.util.Collection) and Map
interface (java.util.Map) are the two main “root” interfaces of
Java collection classes.
• A framework is a set of classes and interfaces which provide a
ready-made architecture.
• The Java Collections Framework is a collection of interfaces
and classes which helps in storing and processing the data
efficiently.
• A list in Java is a collection for storing elements in sequential order.
Sequential order means the first element, followed by the second element,
followed by the third element, and so on.
• List in Java provides the facility to maintain the ordered collection. It
contains the index-based methods to insert, update, delete and search the
elements. It can have the duplicate elements also. We can also store the null
elements in the list.
• The List interface is found in the java.util package and inherits the
Collection interface.
• List is an interface whereas ArrayList is the implementation class of List.
• public interface List<E> extends Collection<E>
ArrayList
LinkedList
1) ArrayList internally uses LinkedList
internally
uses
a dynamic array to store the a doubly linked list to store the
elements.
elements.
2) Manipulation with ArrayList
is slow because it internally uses
an array. If any element is
removed from the array, all the
other elements are shifted in
memory.
Manipulation
with
LinkedList
is faster than ArrayList because it
uses a doubly linked list, so no bit
shifting is required in memory.
3) An ArrayList class can act as a LinkedList class can act as a list
list only because it implements and queue both because it
List only.
implements List and Deque
interfaces.
4) ArrayList is better for storing LinkedList
is
better
and accessing data.
manipulating data.
for
5) The memory location for the The location for the elements of a
elements of an ArrayList is linked list is not contagious.
contiguous.
6) Generally, when an ArrayList is There is no case of default
initialized, a default capacity of 10 capacity in a LinkedList. In
is assigned to the ArrayList.
LinkedList, an empty list is created
when a LinkedList is initialized.
7) To be precise, an ArrayList is a LinkedList implements the doubly
resizable array.
linked list of the list interface.
• When the rate of addition or removal rate is more than the read scenarios,
then go for the LinkedList. On the other hand, when the frequency of the
read scenarios is more than the addition or removal rate, then ArrayList
takes precedence over LinkedList.
• Since the elements of an ArrayList are stored more compact as compared to
a LinkedList; therefore, the ArrayList is more cache-friendly as compared
to the LinkedList. Thus, chances for the cache miss are less in an ArrayList
as compared to a LinkedList. Generally, it is considered that a LinkedList is
poor in cache-locality.
• Memory overhead in the LinkedList is more as compared to the ArrayList.
It is because, in a LinkedList, we have two extra links (next and previous)
as it is required to store the address of the previous and the next nodes, and
these links consume extra space. Such links are not present in an ArrayList.
•
• The ArrayList class implements the List interface. It uses a dynamic array
to store the duplicate element of different data types. The ArrayList class
maintains the insertion order and is non-synchronized. The elements stored
in the ArrayList class can be randomly accessed
• import java.util.*;public class MyClass { public static void main(String
args[]) { List <Integer>l=new ArrayList<Integer>(); l.add(2); l.add(30);
l.add(400); Iterator li=l.iterator(); while(li.hasNext())
System.out.println(li.next()); l.remove(2); for(int i:l)
System.out.println(i); }}
• The LinkedList class is almost identical to the ArrayList:
• The LinkedList class has all of the same methods as the ArrayList class
because they both implement the List interface. This means that you can
add items, change items, remove items and clear the list in the same way.
• The ArrayList class has a regular array inside it. When an element is added,
it is placed into the array. If the array is not big enough, a new, larger array
is created to replace the old one and the old one is removed.
• The LinkedList stores its items in "containers." The list has a link to the
first container and each container has a link to the next container in the list.
To add an element to the list, the element is placed into a new container and
that container is linked to one of the other containers in the list.
• For many cases, the ArrayList is more efficient as it is common to need
access to random items in the list, but the LinkedList provides several
methods to do certain operations more efficiently:
•
•
•
•
•
•
addFirst()
addLast()
removeFirst()
removeLast()
getFirst()
getLast()
Adds an item to the beginning of the list.
Add an item to the end of the list
Remove an item from the beginning of the list.
Remove an item from the end of the list
Get the item at the beginning of the list
Get the item at the end of the list
HashMap
• A HashMap, store items in "key/value" pairs, and it can access them by an
index of another type.
• One object is used as a key (index) to another object (value).
• It can store different types: String keys and Integer values, or the same
type, like: String keys and String values
set
• Java HashSet class is used to create a collection that uses a hash table for
storage. It inherits the AbstractSet class and implements Set interface.
The important points about Java HashSet class are:
•HashSet stores the elements by using a mechanism called
hashing.
•HashSet contains unique elements only.
•HashSet allows null value.
•HashSet class is non synchronized.
•HashSet doesn't maintain the insertion order. Here,
elements are inserted on the basis of their hashcode.
•HashSet is the best approach for search operations.
• Difference between List and Set
• A list can contain duplicate elements whereas Set contains unique elements
only.
Map
• The Map interface present in java.util package represents a mapping
between a key and a value. The Map interface is not a subtype of the
Collection interface. Therefore it behaves a bit differently from the rest of
the collection types. A map contains unique keys.
• A map contains values on the basis of key, i.e. key and value pair. Each key
and value pair is known as an entry. A Map contains unique keys.
Download