Tuesday 2 June 2015

How to count number of element in LinkedHashSet


import java.util.LinkedHashSet;
import java.util.Set;

public class CountElement {
   
    public static void main(String[] args) {
       
        Set<String> lhs = new LinkedHashSet<String>();
       
        lhs.add("mumbai");
        lhs.add("delhi");
        lhs.add("kolkata");
        lhs.add("chandigarh");
        lhs.add("dehradun");
       
        //print linkedhashset
        System.out.println("lhs is = "+lhs);
       
        /*
         * Returns the number of elements in this set (its
           cardinality). If this set contains more than
           Integer.MAX_VALUE elements, returns
           Integer.MAX_VALUE.
         */
       
        System.out.println("element in lhs = "+lhs.size());
       
    }

}


Output:

lhs is = [mumbai, delhi, kolkata, chandigarh, dehradun]

element in lhs = 5
 

No comments:

Post a Comment