Wednesday, April 10, 2013

Java HashSet Example

Java HashSet Example

HashSet is the class, which is available in java.util package.  HashSet implements the set interface.

HashSet does not preservs the order, suppose if we inert the elments in the HashSet in some order, while
 Iterating we can not guarantee to get the same order.

HashSet allows the null values.

HashSet has the methods like add,remove,size,isEmpty...etc.

Java HashSet Sample Code : 

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

//HashSet Example Java
public class JavaHashSetExample {

    public static void main(String[] args) {
        // The advantage of HashSet is it sorts the elements.
        HashSet<String> hashSet = new HashSet<String>();
        hashSet.add("spring");
        hashSet.add("hibernate");
        hashSet.add("struts");
        hashSet.add(null);
        hashSet.add("webservices");
        System.out.println("size : " + hashSet.size());
        Iterator<String> iterator = hashSet.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        hashSet.remove("struts");
        System.out.println("After removing struts");
        // after removing jsp
        iterator = hashSet.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        if (hashSet.contains("struts")) {
            System.out.println("struts available");
        } else {
            System.out.println("struts not available");
        }

    }

}



Output of the code :

size : 5

null
webservices
hibernate
spring
struts

After removing struts

null
webservices
hibernate
spring
struts not available


Java ListIterator Example

Java List Iterator Example:

List Iterator is the interface in java.util package.
ListIterator has lot of methods which are very much useful based on our real time scenerios.

ListIterator has methods which we can iterate in the forward / reverse direction.
and ListIterator also have the methods like prviousIndex and nextIndex methods.
and it also has set,remove and add methods.

I am giving the below very basic ListIterator Sample.

 Java List Iterator sample code :


import java.util.ArrayList;
import java.util.ListIterator;

/*
 * Java ListIterator Example
 */
public class JavaListIteratorExample {

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static void main(String[] args) {

        // Storing the elements into the arraylist.
        ArrayList aList = new ArrayList();
        aList.add("val-1");
        aList.add("val-2");
        aList.add("val-3");

        // Retrieving the elements from the ArrayList by using ListIterator
        ListIterator listIterator = aList.listIterator();

        String value = "";
        System.out.println("List Iterator in the forward direction");
        while (listIterator.hasNext()) {
            value = (String) listIterator.next();
            System.out.println(value);
        }
        System.out.println("List Iterator in the Reverse direction");
        while (listIterator.hasPrevious()) {
            value = (String) listIterator.previous();
            System.out.println(value);
        }
        System.out.println("List Iterator Previous Index : "
                + listIterator.previousIndex());
        System.out.println("List Iterator Next Index : "
                + listIterator.nextIndex());
    }

}



Output of the above code ;

List Iterator in the forward direction
val-1
val-2
val-3
List Iterator in the Reverse direction
val-3
val-2
val-1
List Iterator Previous Index : -1
List Iterator Next Index : 0



Sunday, April 7, 2013

Java TreeSet Example

Java TreeSet Example

TreeSet is the class which is available in java.util package.

The main advantage of TreeSet is, it automatically sorts the elements.

TreeSet implements the Set interface, it doesn't allow the Null values and allows unique elements.

TreeSet has lot of methods which are very much useful.

TreeSet isEmpty() method which checks whether the TreeSet is empty or not.

TreeSet methods are remove,removeall, add,addall,contails,contailsall...etc

 Java TreeSet Example Code


import java.util.Iterator;
import java.util.TreeSet;
//TreeSet Example Java
public class TreeSetExampleJava {

    public static void main(String[] args) {
        //TreeSet, it sorts the elements.
        TreeSet<String> treeSet = new TreeSet<String>();
        treeSet.add("corejava");
        treeSet.add("jsp");
        treeSet.add("jsp");
        //treeSet.add(null);
        treeSet.add("servlets");
        treeSet.add("jdbc");
        
        Iterator<String> iterator = treeSet.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        treeSet.remove("jsp");
        System.out.println("After removing jsp");
        // after removing jsp
        iterator = treeSet.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        if (treeSet.contains("jdbc")) {
            System.out.println("jdbc available");
        }

    }

}


Output :
 
corejava
jdbc
jsp
servlets
After removing jsp
corejava
jdbc
servlets
jdbc available


Java Enhanced For Loop Example

Java Enhanced For Loop Example :

Enhanced For Loop is a more easy way and convenient way to iterate the elements from the collections or Arrays.

I am giving very basic Enhanced For Loop Example below.

The below code is very straight forward how you iterate the elements over the collections/arrays by using
Enhanced For Loop.

Enhanced For Loop Example Code 

import java.util.ArrayList;
//Java Enhanced for Loop Example
public class EnhancedForLoopExample {
    public static void main(String[] args) {
        String[] array = new String[] { "v1", "v2", "v3" };
        //Normal for loop iterate elements from the Array
        for (int i = 0; i < array.length; i++) {
            System.out.println("Normal For Loop : " + array[i]);
        }
        //Enhanced for Loop iterate elements from the Array
        for (String value : array) {
            System.out.println("Enhanced For Loop : " + value);
        }
        //Using generics storing the elements into the ArrayList
        ArrayList<String> aList = new ArrayList<String>();
        aList.add("v-1");
        aList.add("v-2");
        aList.add("v-3");
        //Normal for loop iterate elements from the ArrayList
        for (int i = 0; i < aList.size(); i++) {
            System.out.println("Normal For Loop : " + aList.get(i));
        }
        //Enhanced for Loop iterate elements from the ArrayList
        for (String value : aList) {
            System.out.println("Enhanced For Loop : " + value);
        }
    }
}


Output of the Program :


Normal For Loop : v1
Normal For Loop : v2
Normal For Loop : v3


Enhanced For Loop : v1
Enhanced For Loop : v2
Enhanced For Loop : v3


Normal For Loop : v-1
Normal For Loop : v-2
Normal For Loop : v-3


Enhanced For Loop : v-1
Enhanced For Loop : v-2
Enhanced For Loop : v-3





 

Saturday, April 6, 2013

Java Iterator Example

Java Iterator Example

I am giving the very straight forward example how you Iterate the elements from the ArrayList.

Iterator is the interface which is there in the package java.util.

You can iterate the elements from the Iterator by using the methods hashNext() and next().

By using remove() method we can remove the elements from the Iterator.

Sample code for the Java Iterator Example :


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

/*
 * JavaIterator Example
 */
public class JavaIteratorExample {
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static void main(String[] args) {
        
        //Storing the elements into the arraylist.
        ArrayList aList = new ArrayList();
        aList.add("val-1");
        aList.add("val-2");
        aList.add("val-3");
        
        //Retrieving the elements from the ArrayList by using Iterator
        Iterator iterator = aList.iterator();
        String value = "";
        while(iterator.hasNext()){
            value = (String)iterator.next();
            System.out.println(value);            
        }                
    }

}


Output :

val-1
val-2
val-3




 
Disclaimer : If you find any mistakes / corrections / feedback Please let us know...This website author shall not accept liability or responsibility for any errors, mistakes or misstatements found in this website.