Tutorialspoint

Check if a given key exists in Java HashMap

import java.util.*;
public class Demo {
   public static void main(String args[]) {
      // Create a hash map
      HashMap hm = new HashMap();
      // Put elements to the map
      hm.put("Bag", new Integer(1100));
      hm.put("Sunglasses", new Integer(2000));
      hm.put("Frames", new Integer(800));
      hm.put("Wallet", new Integer(700));
      hm.put("Belt", new Integer(600));
      // Get a set of the entries
      Set set = hm.entrySet();
      System.out.println("Elements in HashMap...");
      // Get an iterator
      Iterator i = set.iterator();
      // Display elements
      while(i.hasNext()) {
         Map.Entry me = (Map.Entry)i.next();
         System.out.print(me.getKey() + ": ");
         System.out.println(me.getValue());
      }
      System.out.println();
      System.out.println("Does Bag exist in the HashMap = "+hm.containsKey("Bag"));
   }
}

Find maximum element of HashSet in Java

import java.util.*;
public class Demo {
   public static void main(String args[]) {
      // create hash set
      Set<Integer> hs = new HashSet<Integer>();
      hs.add(29);
      hs.add(879);
      hs.add(88);
      hs.add(788);
      hs.add(456);
      System.out.println("Elements in set = "+hs);
      // finding maximum element
      Object obj = Collections.max(hs);
      System.out.println("Maximum Element = "+obj);
   }
}

Clone an ArrayList in Java

import java.util.ArrayList;
import java.util.List;
public class Demo {
   public static void main(String[] args) {
      List aList1 = new ArrayList();
      aList1.add("Apple");
      aList1.add("Mango");
      aList1.add("Guava");
      aList1.add("Orange");
      aList1.add("Peach");
      List aList2 = ((List) ((ArrayList) aList1).clone());
      System.out.println("Elements in aList1: " + aList1);
      System.out.println("Elements in aList2: " + aList2);
   }
}

Get the size of an ArrayList in Java

import java.util.ArrayList;
import java.util.List;
public class Demo {
   public static void main(String[] args) {
      List aList = new ArrayList();
      aList.add("Apple");
      aList.add("Mango");
      aList.add("Guava");
      aList.add("Orange");
      aList.add("Peach");
      System.out.println("The size of the ArrayList is: " + aList.size());
   }
}

Check whether two HashSet are equal in Java

import java.util.*;
public class Demo {
   public static void main(String args[]) {
      // create hash set 1
      HashSet hs1 = new HashSet();
      hs1.add("G");
      hs1.add("H");
      hs1.add("I");
      hs1.add("J");
      hs1.add("K");
      System.out.println("Elements in set 1 = "+hs1);
      // create hash set 2
      HashSet hs2 = new HashSet();
      hs2.add("G");
      hs2.add("H");
      hs2.add("I");
      hs2.add("J");
      hs2.add("K");
      System.out.println("Elements in set 2 = "+hs2);
      System.out.println("Are both the HashSet equal? "+hs1.equals(hs2));
   }
}

Convert elements in a HashSet to an array in Java

import java.util.*;
public class Demo {
   public static void main(String args[]) {
      // create a hash set
      HashSet hs = new HashSet();
      // add elements to the hash set
      hs.add("B");
      hs.add("A");
      hs.add("D");
      hs.add("E");
      hs.add("C");
      hs.add("F");
      hs.add("K");
      hs.add("M");
      hs.add("N");
      System.out.println("Elements in the set: "+hs);
      Object[] ob = hs.toArray();
      System.out.println("Converting elements to array...");
      for (int i = 0; i < ob.length; i++) {
        System.out.println(ob[i]);
      }
   }
}

Java Program to Check whether a HashSet is empty or not

import java.util.*;
public class Demo {
   public static void main(String args[]) {
      HashSet hs = new HashSet();
      // empty set
      System.out.println("Is the set empty? = "+hs.isEmpty());
   }
}

Check whether a HashSet is empty or not in Java

import java.util.*;
public class Demo {
   public static void main(String args[]) {
      // create a hash set
      HashSet hs = new HashSet();
      // add elements to the hash set
      hs.add("B");
      hs.add("A");
      hs.add("D");
      hs.add("E");
      hs.add("C");
      hs.add("F");
      hs.add("K");
      hs.add("M");
      hs.add("N");
      System.out.println("Elements: "+hs);
      System.out.println("Is the set empty? = "+hs.isEmpty());
   }
}

Find the size of a HashMap in Java

import java.util.*;
public class Demo {
   public static void main(String args[]) {
      // Create a hash map
      HashMap hm = new HashMap();
      // Put elements to the map
      hm.put("Bag", new Integer(1100));
      hm.put("Sunglasses", new Integer(2000));
      hm.put("Franes", new Integer(800));
      hm.put("Wallet", new Integer(700));
      hm.put("Belt", new Integer(600));
      Set set = hm.entrySet();
      System.out.println("Elements in HashMap...");
      // Get an iterator
      Iterator i = set.iterator();
      // Display elements
      while(i.hasNext()) {
         Map.Entry me = (Map.Entry)i.next();
         System.out.print(me.getKey() + ": ");
         System.out.println(me.getValue());
      }
      System.out.println();
      System.out.println("Size of HashMap = "+set.size());
   }
}

Fill elements in a Java float array in a specified range

import java.util.Arrays;
public class Demo {
   public static void main(String[] argv) throws Exception {
      float[] floatArray = new float[10];
      float floatValue = 5.8F;
      int indexStart = 5;
      int indexFinish = 9;
      Arrays.fill(floatArray, indexStart, indexFinish, floatValue);
      System.out.println("The float array content is: " + Arrays.toString(floatArray));
   }
}

1 2 3 4 5 6 7 ... 2054 Next
Advertisements
Loading...