Initialize Static Map Java

Method 1: import java.util.HashMap; import java.util.Map;   public class Test { private static final Map<Integer, String> tempMap = new HashMap<>(); static { tempMap.put(1, "Sandy"); tempMap.put(2, "Roxy"); } }import java.util.HashMap; import java.util.Map; public class Test { private static final Map<Integer, String> tempMap = new HashMap<>(); static { tempMap.put(1, "Sandy"); tempMap.put(2, "Roxy"); } } Method 2: import … Read more

Initialize Map Java

Java 8 and lower Versions: Map<String, String> myMap = createNewMap();   private static Map<String, String> createNewMap() { Map<String,String> tempMap = new HashMap<String,String>(); tempMap .put("a", "Susan"); tempMap .put("b", "Roy"); return tempMap; }Map<String, String> myMap = createNewMap(); private static Map<String, String> createNewMap() { Map<String,String> tempMap = new HashMap<String,String>(); tempMap .put("a", "Susan"); tempMap .put("b", "Roy"); return tempMap; } … Read more

Iterate over characters of a String in Java

1. Naive solution public class TestJava { public static void main(String[] args) { String str = "w3schools";   // using simple for-loop for (int i = 0; i < str.length(); i++) { System.out.println(str.charAt(i)); } } }public class TestJava { public static void main(String[] args) { String str = "w3schools"; // using simple for-loop for (int … Read more

Char array preferred over string for passwords

There are 2 main reasons of Char array preference over string for passwords. Strings are immutable: As you know, Strings are immutable in Java which means we can not change the string, if you try to change any existing String it will produce a new string. Because of this if a password is stored as … Read more

Generate a random alpha numeric string

Best Solution import java.security.SecureRandom; import java.util.Locale; import java.util.Objects; import java.util.Random;   public class TestJava {   public static void main(String[] args) { RandomString randomString = new RandomString();   System.out.println(randomString.nextString()); }   }   class RandomString {   //Generate a random string. public String nextString() { for (int idx = 0; idx < buf.length; ++idx) buf[idx] … Read more

Generate random integers within a specific range in java

Java 1.7 or Higher Versions import java.util.concurrent.ThreadLocalRandom;   public class TestJava {   public static void main(String[] args) { int min = 100; int max = 200; // nextInt is normally exclusive of the top value, // so add 1 to make it inclusive int randomNumber = ThreadLocalRandom.current().nextInt(min, max + 1);   System.out.println(randomNumber); } }import … Read more

Increase heap size

Increase heap size in java Set or Increase the maximum heap size: java -Xmx256m TestData.java Where: TestData.java: represents java application file. Java command memory options $ java -X -Xmixed mixed mode execution (default) -Xint interpreted mode execution only -Xbootclasspath: set search path for bootstrap classes and resources -Xbootclasspath/a: append to end of bootstrap class path … Read more

Synchronized override method

We can synchronized the overridden method in subclass. See the below example. class ShowTest {   void display(int num) { System.out.println("Number = " + num); }   }   class Test extends ShowTest { synchronized void display(int num) { for(int i=1;i<=10;i++){ System.out.println(num*i); try{ Thread.sleep(600); }catch(Exception e){ System.out.println(e); } } }   } class MyThread1 extends … Read more

maven interview questions and answers

What are the build tools in java? A build tool is utility program to automate the process of repetitive tasks like compiling source code and creating files etc. A build tool can be executed from the command line. Note: Apache Ant, Maven and Gradle are the commonly used building tools in java. Why build tools … Read more

What is HTTPServletRequest class?

When a browser requests for a web page, it sends lot of information to the web server which cannot be read directly because this information travel as a part of header of HTTP request. HTTPServletRequest represents this HTTP Request. Related topics What is a Web application? What is a Web browser? What is different between … Read more