HashSet :
·
HashSet is a predefined class in Java
·
It is defined in the java.util package.
·
HashSet implements the methods of the Set
interface.
·
It can be used to store a group of values,
objects, or elements
·
HashSet does not store values sequentially;
instead, it stores values randomly. The order in which elements are stored is
not guaranteed.
·
Insertion order is not preserved in hashset.
Inseting order
Displaying order
10,20,30,40 à 40,20,10,30
·
HashSet does not allow duplicate values. If you
attempt to add a duplicate element, it will be ignored.
package CollectionFrameBasics;
import java.util.HashSet;
import java.util.Iterator;
public class HashSetBasics {
public
static void main(String[] args) {
//
create hashset obj and store String
values
HashSet<String>
hs = new HashSet<String>();
//
initially empty
//
HW check hash set is empty
//
add some values into hashset
Ram,Sita,Raju
hs.add("Ram");
hs.add("Sita");
hs.add("Raju");
System.out.println("Hash
set=" + hs);//
//
Hash set=[Sita, Raju, Ram]
//
does not allow duplicate values. add Raju
hs.add("Raju");
System.out.println("Hash
set="+ hs);
//
Hash set=[Sita, Raju, Ram]
//
HW check hash set is empty
//
HW remove Raju
//HW get count of hashset
//
HW check hashset contains Sita or not
//
HW check hashset contains 'Raju' or not
// HW check hashset
contains 'Laskhamn' or not
// get values from hashset
// hs.get(0);//CE"
//
the method get(int) is undefined for the type HashSet<String>
//
Note : In hashset class, we dont have get() method
// 4 ways
// 1. for loop with
index no --> not possible
// 2. for each loop
//
get all values from hash set using for each loop
for(String
eachVal : hs)
{//
// read values from hashset and store
val into variable i.e eachVal
System.out.println("get all values from
hash set using for each loop="+ eachVal);
}
//
3 iterator
Iterator<String>
it = hs.iterator();
while(it.hasNext())
{//
//// if there are any elements/ values to iterate
, ithasnext() true else false
String
val = it.next();
//// return val from collection obj
System.out.println("values
from hash set using Iterator()="+ val);
}
//
4 listIterator
// hs.listItertaor();// CE
//
Note: We dont have listItertaor() in HashSet class
}
}
FAQ what is List ?
FAQ Is List class (or) Interface?
Interface
FAQ Diff b/w List and Set ?
Here is the comparison between List and Set in a tabular
format:
|
Feature |
List
Interface (I) |
Set
Interface (I) |
|
Definition |
Predefined interface in Java |
Predefined interface in Java. |
|
Purpose |
Used to store a collection or group of values. |
Used to store a group of unique values or elements. |
|
Order of Storage |
Stores values sequentially; each element is associated with an index
starting from 0. |
Stores values randomly; does not maintain any specific order. |
|
Example of Order |
Example: Elements 10, 20, 30 are stored as 10, 20, 30. |
Example: Elements 10, 50, 30, 40 might be stored as 30, 10, 50, 40. |
|
Insertion Order |
Insertion order is preserved. |
Insertion order is not preserved. |
|
Duplicates |
Allows duplicate values. |
Does not allow duplicate values. |
|
Example of Duplicates |
Example: List can contain values 10, 20, 10, 30. |
Example: Set will store values 10, 20, 30, ignoring the duplicate 10. |
|
Implementation Classes |
ArrayList, LinkedList, Stack, Vector |
HashSet, LinkedHashSet, TreeSet |
|
Class Implementations |
class ArrayList implements List |
class HashSet implements Set |
This table provides a clear comparison of the List and Set
interfaces in Java, highlighting their key features, behaviors, and
implementations.
** FAQ Diff b/w ArrayList and HashSet ?
Same answer as above
|
Feature |
ArrayList (C) |
HashSet (C) |
|
Definition |
ArrayList is a predefined class in Java that implements the List
interface. |
HashSet is a predefined class in Java that implements the Set
interface. |
|
Order of Storage |
Stores elements in a sequential order (based on insertion order). |
Stores elements in a random order, with no guarantee of the order
being preserved |
HW Create Hashset and add Integer values and get all values using 4 ways?
HashSet<Integer>
HW Create Hashset and add Integer values and get all values using 4
ways?
HashSet<Float>
HW Create Hashset and add character values, get all values using 4
ways?
HashSet<Character> ...
LinkedHashSet:
·
LinkedHashset is predefined class in Java.
·
It can be used to store group of values /
objects/Elements
·
class LinkedHashSet implements Set interface
methods
·
class HashSet implements Set interface methods
Create
LinkedHashSet and store String values:
Note : observe that values are displaying in sequential
order or not
Hash set does not store values
sequentially but Linkedhashet stores
values sequentially and insertion order is preserved.
Ex:
package CollectionFrameBasics;
import java.util.LinkedHashSet;
public class LinkedHashSetBasics
{
public
static void main(String[] args) {
//create
LinkedHashSet of String
LinkedHashSet<String>
lhs = new LinkedHashSet<String>();
// initially it creates linked hash set obj with empty
//
add Ramu,Sita,Sam, Raju
lhs.add("Ramu");
lhs.add("sita");
lhs.add("Sam");
lhs.add("Raju");
System.out.println("lhs="
+ lhs);
//
Note: In LinkedHashSet, it stores values
seqentially
//
lhs=[Ramu, Sita, Sam, Raju]
//
Insertion order is preserved
// does not allow duplicate values . add Raju
lhs.add("Raju");
System.out.println("lhs="+
lhs);
//
lhs=[Ramu, sita, Sam, Raju]
//HW
get all values from Linked hash set
// 4 ways
}
}
HW Create LinkedHash set and
store Integer values and use all above methods?
HW Create LinkedHash set and
store Character values and use all above methods?
TreeSet :
·
Tree Set is
predefined class in java.
·
It can be used to store a group of values.
·
It stores
the values in “ascending” order by default.
·
Implements set interface methods.
package CollectionFrameBasics;
import java.util.TreeSet;
public class TreeSetBasics {
public
static void main(String[] args) {
//
create TreeSet of String
TreeSet<String> ts =
new TreeSet<String>();
//
store apple,cat,ball,dog
ts.add("apple");
ts.add("cat");
ts.add("ball");
ts.add("dog");
// displays sequentially or not --> No
///
check insertion order is preserved or not
--> no
System.out.println("ts="+ts);//
- store the values in ascending order
//ts=[apple,
ball, cat, dog]
//
HW repeat all operations(remove, get count, contains
....etc) on treestet?
// 1. for loop with index no
// ts.get(i); // Error :
get() is not defined treeset
//HW
2 using for each loop
System.out.println("Val
from treeset=");
// Val
from treeste=apple
// Val
from treeste=ball
// Val
from treeste=cat
// Val
from treeste=dog
//HW 3 .iterataor
//
checks that next value / ele is there, if ele is there, it return true else
false
//
get val
// System.out.println("Values from tree
set using iterator="+ val);
// Values
from tree set using iterator=apple
// Values
from treeste using iterator=ball
// Values
from treeste using iterator=cat
// Values
from treeste using iterator=dog
//HW 4
using listIterator()
}
}
HW Tree set store some numbers , check that they are displaying in asc/
desc order and use all above methods?
10,30,40,20
o.p: 10,20,30,40
No comments:
Post a Comment