Showing posts with label hashset examples. Show all posts
Showing posts with label hashset examples. Show all posts

Wednesday, 27 May 2015

How to count number of element in HashSet

 Size() Method return number of element in the HashSet.

import java.util.HashSet;
import java.util.Set;

public class CountElement {
   
    public static void main(String[] args) {
       
        Set<String> hs = new HashSet<String>();
        hs.add("java");
        hs.add("php");
        hs.add("html");
        hs.add("javascript");
        hs.add("mysql");

        //print hashset
        System.out.println("hashset = "+hs);
       
       
        /*
         * Returns the number of elements in set. If this set contains more than
         * Integer.MAX_VALUE elements, returns  Integer.MAX_VALUE.
         */
       
        System.out.println("Size of hashset = "+hs.size());
    }

}





Output:


hashset = [javascript, php, html, java, mysql]

Size of hashset = 5

How To Convert Hashset To Array

import java.util.HashSet;
import java.util.Set;

public class HashSetToArray {
   
    public static void main(String[] args) {
       
        Set<String> hs = new HashSet<String>();
        hs.add("java");
        hs.add("php");
        hs.add("html");
        hs.add("javascript");
        hs.add("mysql");

        //print hashset
        System.out.println("hashset = "+hs);
       
        String [] arr = new String[hs.size()];
       
        /*
         * Returns an array containing all of the
         * elements in hs set
         */
        hs.toArray(arr);
       
        System.out.println("Array is ");
       
        for (String str : arr) {
            System.out.println("Element is = "+str);
        }
       
    }

}

Output:

hashset = [javascript, php, html, java, mysql]

Array is

Element is = javascript

Element is = php

Element is = html

Element is = java

Element is = mysql

How to Remove Set From HashSet

package com.javapro.hashset;

import java.util.HashSet;
import java.util.Set;

public class RemoveAll {

    public static void main(String[] args) {

        Set<String> hs = new HashSet<String>();
        hs.add("java");
        hs.add("php");
        hs.add("html");
        hs.add("javascript");
        hs.add("mysql");

        // print hashset
        System.out.println("Before Remove Element hs = " + hs);

        HashSet<String> hs2 = new HashSet<String>();
        hs2.add("java");
        hs2.add("html");
        hs2.add("two");

        System.out.println("hs2 is = " + hs2);

        /*
         * Removes from hs set all of its elements that are contained in the hs2
         * collection
         */

        hs.removeAll(hs2);

        System.out.println("After Remove All hs = " + hs);

    }

}

Output:

Before Remove Element hs = [javascript, php, html, java, mysql]


hs2 is = [two, html, java]

After Remove All hs = [javascript, php, mysql]



How to copy all element of a Hashset to other

import java.util.HashSet;
import java.util.Set;

public class AddAll {

    public static void main(String[] args) {

        Set<String> hs = new HashSet<String>();
        hs.add("java");
        hs.add("php");
        hs.add("html");
        hs.add("javascript");
        hs.add("mysql");

        // print hashset
        System.out.println("hs is = " + hs);
       
        HashSet<String> hashSet = new HashSet<String>();
       
        /*
         * Adds all of the elements in the specified
         *  collection to hashset collection
         */
       
        hashSet.addAll(hs);
       
        System.out.println("hashset is="+hashSet);

    }

}





Output:


hs is = [javascript, php, html, java, mysql]

hashset is=[javascript, php, java, html, mysql]

How to convert array to HashSet

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class ArrayToHashSet {
    public static void main(String[] args) {

        String[] arr = { "one", "two", "third" };
       

        Set<String> hs = new HashSet<String>(Arrays.asList(arr));

        // print hashset

        System.out.println("hashset is =" + hs);

    }
}

Output:

hashset is =[two, one, third]

How to Convert a HashSet to an ArrayList

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class HashSetToList {


    public static void main(String[] args) {
       
        Set<String> hs = new HashSet<String>();
        hs.add("java");
        hs.add("php");
        hs.add("html");
        hs.add("javascript");
        hs.add("mysql");
       
        //print hashset
        System.out.println("hashset = "+hs);
       
      List<String> list = new ArrayList<String>(hs);
       
      System.out.println("list from hashset = "+list);
     
    }
   
}

Output:

hashset = [javascript, php, html, java, mysql]


list from hashset = [javascript, php, html, java, mysql]

Tuesday, 26 May 2015

How to storing Java Object In HashSet


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

public class HashSetOfObject {
    public static void main(String[] args) {
       
        Set<User> hs = new HashSet<User>();

        hs.add(new User("shivam",1213));
        hs.add(new User("yogesh",8383));
        hs.add(new User("ashwani", 8287));
       
        Iterator<User> itr =hs.iterator();
        while (itr.hasNext()) {
            User user = (User) itr.next();
            System.out.println("userName ="+user.getName()+" userId = "+user.getUserId());
        }
       
       
    }
}

class User{
       
    private String name;
    private int userId;
   
    public User(String name, int userId) {
        super();
        this.name = name;
        this.userId = userId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }
    
}



Output:

userName =ashwani userId = 8287

userName =shivam userId = 1213

userName =yogesh userId = 8383

Java HashSet Examples