by Kunal Sir
Set Interface from Collection:
What is Set?
Set in java collection framework is a collection of unique
elements, where duplicate elements are not allowed.
The set interface contains only methods inherited from
collection and adds the restriction that duplicate elements
are prohibited.
Set interface is the child interface of collection.
Set interface is found in java.util package.
Object Creation of Set Interface:
Set<Obj> set = new HashSet<Obj>( );
Where: - Set Interface
<Obj> Generic (inside angular bracket)
set Reference variable / object
new Keyword
HashSet Implemented Class
() Parentheses
Note – Obj is the type of the object to be stored in Set.
Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk
Road, Karve Nagar, Pune, Maharashtra 411052, Mobile No.- 8888022204
by Kunal Sir
Difference between List and Set:
There are following difference between list and set which is
given below –
List Interface Set Interface
List allows duplicates values Set can’t have duplicate values
Lists can be indexed and Sets cannot be indexed and
positionally accessed. cannot be accessed based on
their positions.
Multiple null elements can be Only one null element can be
stored. stored at a time.
List implementations are Set implementations are
ArrayList, LinkedList and Vector HashSet, LinkedHashSet and
etc. TreeSet etc.
The insertion order is It doesn’t maintain the insertion
maintained by the list. order of elements.
List is often used when we need It is used when we need to store
to frequently access the distinct elements.
elements using their indices.
Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk
Road, Karve Nagar, Pune, Maharashtra 411052, Mobile No.- 8888022204
by Kunal Sir
What is an Enhanced For Loop in Java?
This loop provides way for traversing the array or
collections and hence it is mainly used for traversing array
or collections.
It was introduced in java 1.5 and it is also a control flow
statements that iterates a part of the program multiple
times.
This loop also makes the code more readable.
Syntax of an Enhanced For Loop:
for (datatype item : array){
Here array – An array or a collection.
item – Each item of array/collection is assigned to this variable.
datatype – The data type of the array/collection
Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk
Road, Karve Nagar, Pune, Maharashtra 411052, Mobile No.- 8888022204