HTML Java

Java Set


Set

  • In Set there is no ordering, if you want ordering then use sorted set and there is an ordering.
  • And who provide the implementation of set interface is hashset and linked hashset.
  • If you have lots of data and need insertion order then use list and with duplicate elements.

Note:- And if don’t require duplicate and no ordering then use set.


HashSet

It’s a concreate class which providing the implementation of set interface an object of this class is responsible to create hash table in memory hashtable is a concept to implement this concept there is a class HashTable.

  • Hashtable is array of linked list and each list is called as bucket.
  • HashSet is a class where all unique elements are there and no ordering.
  • In linkedhash set there is ordering.

Commonly used constructor of hashset

  • public HashSet()
  • public HashSet(int no of bucket)
  • public HashSet(int no of bucket, float loadFactor)
  • public HashSet(Collection c)

How elements are added in hashset?

Whenever an element is going to be added in hashSet it follows the following steps:

  • Find out the bucket index
  • In order to fin out the bucket index we need the hexa value of the objects which is going to be added in hashtable calling the hash code method of Object class in order to find the hash value of current object.
  • Check the existence of elements in hashtable. In order to finout the existence of the element add method internally calls the contains method and contains method calls the equals method and equals method is use to compare the objects in such a way that it doesn’t allow the duplicate element. If element exist then elements is not going to add in hashtable but if element is not there then it will be added.

Example

package com.collection.programs;

import java.util.HashSet;
class Employee{

int age;
String name;

public Employee(int age, String name) {
super();
this.age = age;
this.name = name;
}

@Override
public String toString() {
return "Employee [age=" + age + ", name=" + name + "]";
}
}

public class DemoHashSet {

public static void main(String[] args) {

// TODO Auto-generated method stub

java.util.HashSet h =new java.util.HashSet();
Employee e1 = new Employee(1,"coder");
Employee e2 = new Employee(1,"arts");
Employee e3 = new Employee(1,"com");
h.add(e1);
h.add(e2);
h.add(e3);
System.out.println(h);
}

}
Try it »


Set

Set is an interface and it’s a root interface of collection interface and its child interface name is SortedSet and its implementing class is TreeSet insert order in dictionary order an object of TreeSet class creates a binary search tree in memory and BST which traverse the elements in inorder traversal.

Duplicate element is not allowed but ordering happens here and in sorted order.


TreeSet

  • TreeSet sort the elements but it doesn’t have its own sorting logic it takes this logic from 2 ways either from Comparable or From Comparator.
  • Java.lang.Comparable
  • Java.util.Comparator they both are interfaces.
  • If we talk about functionality wise then both works same if we talk about Comparable interface then many classes which internally by default implements Comparable Interface, like String class.
  • All wrapper class also (Integer, Float, Double) etc.
  • And method inside Comparable is CompareTo method where the logic is written for comparing and compare method in Comparator both are the same then why both built.