Tuesday, 13 February 2018

Collections Java

https://beginnersbook.com/java-collections-tutorials/

https://beginnersbook.com/2013/12/hashset-class-in-java-with-example/


-------------
HashSet


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


public class SetExample {


public static void main(String args[]){
HashSet<String> hset = new HashSet<String>();

hset.add("apple");
hset.add("oragne");

//System.out.println(hset);
hset.add("apple");
hset.add("banana");
//System.out.println(hset);



  Iterator<String> it =hset.iterator();
  while(it.hasNext()){
  System.out.println(it.next());
  }
 
}
}


------------------------------------

String array[]=new String[hset.size()];
hset.toArray(array);
for(String temp: array){
System.out.println(temp);
}


--------------------------------------------------------



Hash Map


 Set set = hmap.entrySet();
      Iterator iterator = set.iterator();
      while(iterator.hasNext()) {
         Map.Entry mentry = (Map.Entry)iterator.next();
         System.out.print("key is: "+ mentry.getKey() + " & Value is: ");
         System.out.println(mentry.getValue());
      }




--------------------------------------------------------------------------------------


Hash Table



Hashtable<String, String> hashtable = 
              new Hashtable<String, String>();

Enumeration names = hashtable.keys();
   while(names.hasMoreElements()) {
      key = (String) names.nextElement();
      System.out.println("Key: " +key+ " & Value: " +
      hashtable.get(key));
   }



No comments:

Post a Comment