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 H...

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: Co...

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_M...

How to find the Entry with largest Key in a Java Map Collection

8:53 PM
  How to find the Entry with largest Key in a Java Map  Collection Given a map in Java, the task is to find out the entry in this map with the highest key. Examples: Input: Map = {ABC = 10, DEF = 30, XYZ = 20} Output: XYZ = 20 Input: Map = {1 = 40, 2 = 30, 3 = 60} Output: 3 = 60 Approach Iterate the map entry by entry for (Map.Entry entry : map.entrySet())  {     // Operations } Store the first entry in a reference variable to compare to initially. If the current entry’s key is greater than the reference entry’s value, then store the current entry as the reference entry. Repeat this process for all the entries in the map. In the end, the reference variable has the required entry with the highest key in the map. Print this entry Below is the implementation of the above approach: filter_none edit play_arrow brightness_4 // Java program to find entry  // with highest key in a map     import java.util.*;     public class GFG {...

how to get java collection Map size() Method in Java With Examples

8:35 PM
How to get java collection Map size() Method in Java With Examples Map size() method in Java is used to get the total number entries i.e, key-value pair. So this method is useful when you want total entries present on the map. If the map contains more than Integer.MAX_VALUE elements return Integer.MAX_VALUE. Parameters: (This method does not take any parameters.) Return value (This method returns the number of key-value mappings in this map.) Syntax: int size(); Example 1: For non-generic input // Java program to illustrate  // the Map size() Method  import java.util.*;     public class MapSizeExample {             // Main Method      public static void main(String[] args)      {          Map map = new HashMap();                     // Adding key-values          map.put(1, "delhi");   ...

core java collection Map program for interview

8:27 PM
  Core-Java Collection Map program for interview preparation : how to get java collection Map size() Method in Java With Examples How to find the Entry with the largest Key in a Java Map How to Clone a Map in Java ArrayList vs. HashMap in Java How to create a TreeMap in reverse order in Java How to check if a key exists in a HashMap in Java Difference between HashMap and HashSet Remove an Entry using value from HashMap while Iterating over it Remove an Entry using key from HashMap while Iterating over it Flatten a Stream of Map in Java using forEach loop Replace null values with default value in Java Map Program to Convert List to Map in Java Sort elements by frequency | Set 5 (using Java Map) Program to Convert HashMap to TreeMap in Java Traverse through a HashMap in Java Initialize HashMap in Java ImmutableMap, as suggested by the name, is a type of Map which is immutable. It means that the content of the map are fixed or… Read More Given marks scored out of 100 by a student in s...

Maven – Dependency Management

2:16 AM
In Maven, dependency is another archive—JAR, ZIP, and so on—which your current project needs in order to compile, build, test, and/or to run. The dependencies are gathered in the  pom.xml  file, inside of a <dependencies> tag. When you run a build or execute a maven goal, these dependencies are resolved, and are then loaded from the local repository. If they are not present there, then Maven will download them from a remote repository and store them in the local repository. You are allowed to manually install the dependencies as well. Read More: Local Repository Path en Dependency Example Before going further deep inside dependency management, let’s have a quick example having different elements which we will talk in this post. < dependencies >      < dependency >          < groupId >junit</ groupId >          < arti...

.