0% found this document useful (0 votes)
32 views3 pages

Collections

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views3 pages

Collections

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Collections : java.

util
The Collection in Java is a framework that provides an architecture to store and
manipulate the group of objects.

Java collections can achieve the all the operations on data such as sorting,
searching, manipulating, insertion and deleting etc.
Java Collection framework provides many interfaces (Set, List, Queue, Deque) and
classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet,
TreeSet).

What are the two ways to iterate the elements of a collection?

There are two methods to iterate the elements:


next(): the method returns the next element of the iteration
hasNext(): the method will check if there are any element remaining in the list

remove(): It removes the last elements returned by the iterator. It is less used.

Difference Between ArrayList and LinkedList?

ArrayList

Java ArrayList class can contain duplicate elements.


Java ArrayList class maintains insertion order.
Java ArrayList class is non synchronized.
Java ArrayList allows random access because the array works on an index basis.

In ArrayList, manipulation is a little bit slower than the LinkedList in Java


because a lot of shifting needs to occur if any element is removed from the array
list.
We can not create an array list of the primitive types, such as int, float, char,
etc.
It is required to use the required wrapper class in such cases.

1) ArrayList internally uses a dynamic array to store the 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.
3) An ArrayList class can act as a list only because it implements List only.
4) ArrayList is better for storing and accessing data.

5) ArrayList is not synchronized.


6) ArrayList increments 50% of current array size if the number of elements exceeds
from its capacity.
7) ArrayList is not a legacy class. It is introduced in JDK 1.2.
8) ArrayList is fast because it is non-synchronized.
9) ArrayList uses the Iterator interface to traverse the elements.

LinkedList
-->LinkedList class uses a doubly linked list to store the elements.
-->It provides a linked-list data structure. It inherits the AbstractList class and
implements List and Deque interfaces.
-->LinkedList internally uses a doubly linked list to store the elements.
-->Manipulation with LinkedList is faster than ArrayList because it uses a doubly
linked list, so no bit shifting is required in memory.
-->LinkedList class can act as a list and queue both because it implements List and
Deque interfaces.
-->LinkedList is better for manipulating data.
-->Java LinkedList class can contain duplicate elements.
-->Java LinkedList class maintains insertion order.
-->Java LinkedList class is non synchronized.
-->In Java LinkedList class, manipulation is fast because no shifting needs to
occur.
-->Java LinkedList class can be used as a list, stack or queue.

Vector
--> Vector is synchronized.
--> Vector increments 100% means doubles the array size if the total number of
elements exceeds than its capacity.
--> Vector is a legacy class.
--> Vector is slow because it is synchronized, i.e., in a multithreading
environment, it holds the other threads in runnable or non-runnable state until
current thread releases the lock of the object.
--> A Vector can use the Iterator interface or Enumeration interface to traverse
the elements.

Difference between List and Set

List
Set
1. The List is an indexed sequence.
1. The Set is an non-indexed sequence.

2. List allows duplicate elements


2. Set doesn’t allow duplicate elements.

3. Elements by their position can be accessed. 3.


Position access to elements is not allowed.

4. Multiple null elements can be stored. 4.


Null element can store only once.

5. List implementations are ArrayList,LinkedList,Vector,Stack 5. Set


implementations are HashSet, LinkedHashSet.

HashSet

-->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.
-->The initial default capacity of HashSet is 16, and the load factor is 0.75.

Internal Working of HashMap:--

->HashMap is a class that implements the Map interface, which stores key-value
pairs.
->It uses a hash table data structure to store the data, where each key is mapped
to an index in the array using a hash function.
->When a key-value pair is added to the HashMap, the hash code of the key will be
compute using the key's hashCode() method.
->This hash code is then used to find the index in the array where the key-value
pair will be stored.
->If there is already a key-value pair at that index, then the HashMap uses a
linked list to store the new pair as the next node in the list.
->This linked list implementation allows for efficient handling of collisions,
which occur when two keys have the same hash code.
->When retrieving a value from the HashMap, the hash code of the key is computed
again, and the index in the array will find.
->If there is a linked list at that index, the HashMap iterates over the list to
find the value associated with the key.
->If the key is not found, the HashMap returns null.

I am Ashwini G Naikar, i have completed my graduation in Information Science and


Engg, i started my career as software engineer in financial domain was involed in
Support and development at Slk thier i closely worked in the banking Application
is FirstHorizon which is american 1st Tennessee bank that was our client SLK
softwares international client project and that was legacy application which
developed on java & JPA servelets,After that i moved to educational Application
that was interernational educational institute which updating student progress to
thiere parents. Where i worked as backend engineer, later i got chance to upgrade
my skill set so learnt python and i moved to ML as ml backend engineer thiere i
worked healthcare project which is insurance recommendation model. then after
marriage i moved to limassol so i started freealancing in python projects as well
so looking for the new oppertunities in java since i had experience

You might also like