Thursday, 30 April 2015

#04 Easy way to understand HashSet of Java Collection.

What is HashSet ?

java.util.HashSet implements Set interface interface and it use hash table to store data in memory. Hash Table uses hashing mechanism to store data in memory.  It makes no guarantees as to the iteration order of the set; in particular, it is unordered collection. It does not guarantee that the order will remain constant over time. Later on we will discuss hashing mechanism using by HashSet.

Use Of HashSet

1. HashSet does not allow duplicate elements, So whenever we want to store unique elements we use Hash Set. A common use for a set is to remember which items in a list have 'already been seen' so that they are not processed more than once.

2.  Another advantage of hashing is that it allows the execution time of basic operation, such as add(), contains(), remove(), and size() to remain constant even for large sets.

Different declaration of HashSet

1. Simple Declaration 



2. Declare HashSet of Specific class



3. Declare HashSet with approximate capacity or size


4. Declare HashSet with initial capacity and load factor


Here load factor means when the size of hashset will increase automatically. Suppose initial capacity is 100 and load factor is 0.75. So when set contains 75 elements then size of set will increase, internally new set of more capacity will generate.

5. Declare HashSet with existing collection








Important methods of HashSet

1. boolean add(Object obj)

   It adds object to set if it is not exist



2. boolean contains(Object obj)

   It return true if object exists in set




3. boolean remove(Object obj)

   It remove object if exists and return true, if obj is not exist in set it return false.





4. int size()

   It returns the number of objects in the set.




5. boolean isEmpty()

   It returns true if  set is empty otherwise returns false.


6. void clear()

   This method remove all elements exist in set.



7. Iterator iterator()

This method returns iterator over the elements in the set.








What happens when you try to add duplicate element?

On every add operation hash set interanally call equals() method and hashCode() method. If it find same element already exist in the set. it will not add duplicate.







Can we add null value into set?

This question is mostly asked by beginners. your answer is Yes you can add null, but as per rule you can add only one null value.

  






Complete code : 




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.javasuitor.demo;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

public class HashSetDemo {

 public static void main(String[] args) {
  
  // Declare simple hashset, its default capacity is 16.  
  HashSet hashSet1 = new HashSet();
  
  // Declare hash set of Particular class
  HashSet<String> hashSet2 = new HashSet<String>();
  
  // Declare hash set using initial capacity
  HashSet<String> hashSet3 =  new HashSet<String>(20);
  
  // Declare hash set using initial capacity and Load factor
  HashSet<String> hashSet4 =  new HashSet<String>(20, 0.75f);
  
  ArrayList <String> countryList = new ArrayList<String>();
  countryList.add("India");
  countryList.add("Australia");
  countryList.add("South Africa");
  
  //Declare hash set using pre built Array List
  
  HashSet<String> hashSet5 = new HashSet<String>(countryList);
  
  //Add values in set
  hashSet3.add("Ashok");
  hashSet3.add("Akanksha");
  hashSet3.add("Sumit");
  hashSet3.add("Shivi");
  hashSet3.add("Bhanu");
  
  // Iterator of Set
  Iterator<String> iterator = hashSet3.iterator();
  
  System.out.println("Elements of set");
  while(iterator.hasNext()){
   System.out.println(iterator.next());
  }
  
  // Try to add duplicate element 
  hashSet3.add("Ashok");
  System.out.println("\nSet elements after adding duplicate object\n"+hashSet3);
  
  // HashSet allow to add null value 
  hashSet3.add(null);
  System.out.println("\nSet elements after adding null\n"+hashSet3);
  
  // Check whether the element present in set or not
  System.out.println("\nIs Ashok present in HashSet: "+hashSet3.contains("Ashok"));
  System.out.println("Is Riddhi present in HashSet: "+hashSet3.contains("Riddhi"));
  
  // Remove particular element from set
  hashSet3.remove(null);
  System.out.println("\nSet elements after removing null \n"+hashSet3);
  
  // Get the number of element in hash set  
  System.out.println("\nNumber of elements in hashSet "+hashSet3.size());
  
  // Check whether the HashSet is empty or not
  System.out.println("\nIs hashSet3 is empty : "+hashSet3.isEmpty());
  System.out.println("Is hashSet1 is empty : "+hashSet1.isEmpty());
  
  // Remove all elements from set
  
  hashSet3.clear();
  System.out.println("\n After Clear method values in hashset : "+ hashSet3);
 }
}

Output :



I hope this contents may helps to move one step forward towards learning. Your precious feedback will surely help me to improve contents. Please feel free to contact me.

Saturday, 11 April 2015

# 03 Important functions of ArrayList

Dear Readers, 

In my last post I presented overview of ArrayList. Now I am writing some common functions which are often use while using ArrayList.

1.add() 
We can add element by using add(Object object) method.

Example:


2. add(int index,Object object)
This method add object at given index number.

Example :


3. remove(int index)

This method removes element from specified index.

Example:




4. remove(Object object)

This method will search specified object in list if object exist in list then it will remove it and return true otherwise it return false.

Example:

5. Sorting of ArrayList 

To sort arrayList, we can use java.util.Collections class inbulit method sort.

Example :



6. size()
This method will return the no. of elements in arrayList.

Example :

7. get(int index)

This method will return the element from given index.

Example :

8. contains(Object object)

This method will return true if object present in arrayList otherwise return false.

Example :



Complete Code 




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.javasuitor.examples;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class ArrayListDemo { 
 public static void main(String[] args) {
  List<String> countryList = new ArrayList<String>();  
  // add() method
  countryList.add(new String("India"));
  countryList.add(new String("England"));
  countryList.add(new String("Australia"));
  countryList.add(new String("Japan"));
  countryList.add(new String("China"));
  // We can add object at specified index.
  // Here Iraq will add on index 2 of countryList.
  countryList.add(2,new String("Iraq"));  
  System.out.println("After add operations");
  displayList(countryList);  
  // remove(int index) method removes element from given index
  // this will remove England from countryList.  
  countryList.remove(1);  
  // remove (Object object) method
  countryList.remove(new String ("Japan"));  
  System.out.println("\nAfter remove operation ");
  displayList(countryList);  
  // To perform sorting on ArrayList we can use Collection class's inbuilt method 
  Collections.sort(countryList);  
  System.out.println( "\nAfter sorting ");
  displayList(countryList);  
  // To get number of elements exist in list we use size() method.
  System.out.println("\ncountryList size = "+countryList.size());  
  // To get element from desired index we can use get method of ArrayList  
  System.out.println("\nElement at index 3 : "+countryList.get(3));  
  // contains() method to check whether element is present in list or not  
  System.out.println("Is India present in list : "+countryList.contains(new String("India")));
  
 }
 
 // method shows the contents of ArrayList
 public static void displayList(List<String> list){
  // We are using iterator to display contents of list
  Iterator<String> iterator = list.iterator();
  while(iterator.hasNext()){
   System.out.println(iterator.next());
  }
  
  
 }

}

Output