JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

write a program to check if a key exists in a HashMap in Java collection

9:20 PM
write a program to check if a key exists in a HashMap in Java collection  Given a HashMap and a key in Java, the task is to check if this key exists in the HashMap or not. Examples: Input: HashMap: {1=Java, 2=JavaHashMap, 3=JavaPrograme}, key = 2 Output: true Input: HashMap: {1=G, 2=e, 3=e, 4=k, 5=s}, key = 10 Output: false Recommended: Please try your approach on {IDE} first, before moving on to the solution. Using Iterator (Not Efficient): Get the HashMap and the Key Create an iterator to iterate over the HashMap using HashMap.iterate() method. Iterate over the HashMap using the Iterator.hasNext() method. While iterating, check for the key at that iteration to be equal to the key specified. The entry key of the Map can be obtained with the help of entry.getKey() method. If the key matches, set the flag as true. The flag value after iterating, contains the result. Below is the implementation of the above approach: Program 1: // Java program to check if a key exists  // in a HashMap or

How to write a program to create a TreeMap in reverse order in Java

9:15 PM
  How to write a program to create a TreeMap in reverse order in Java By default TreeMap elements in Java are sorted in ascending order of keys. However, we can create the TreeMap in reverse order using Collections.reverseOrder() method in Java and display the elements in descending order of keys. The Collections.reverseOrderS() method in Java returns a Comparator that imposes reverse order of a passed Comparator object. We can use this method to sort any list or any other collection in reverse order of user defined Comparator. Examples: // Insert elements to the TreeMap Input : treemap.put("1", "Welcome");              treemap.put("2", "to");              treemap.put("3", "the");              treemap.put("4", "that");              treemap.put("5", "Community"); // Elements should be printed in reverse order // of their insertion Output : 5: Community                4: that              

How to Clone a Map in Java collection

9:10 PM
 How to Clone a Map in Java collection Following are the 5 different ways to Clone a Map in Java. Example: {1=this, 2=For, 3=this} Method 1: Naive method 1. Create an object for the class map. 2. Put the elements into the map using the put() method. 3. Again create another object for the class map. 4. Now finally iterate the map and call put method to clone the initial map. Below is the implementation of the above approach: // Program to clone a Map in Java  // Naive Method     import java.util.*;     class JavacloneExample {      public static void main(String[] args)      {          // Creating an object for class Map          Map<Integer, String> hash_Map              = new HashMap<Integer, String>();             // putting elements into the map          hash_Map.put(1, "this");          hash_Map.put(2, "For");          hash_Map.put(3, "this");             // Creating a new object for          // class Map to clone a map          Map<Inte

.