Java

okhttpclient get/post/form requests example:

Java, Tools
okhttpclient get/post/form requests example: Maven dependency: [xml] <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.11.0</version> </dependency> [/xml] okhttpclient with formbody example: [java] public class App { public static void main( String[] args ) throws IOException { OkHttpClient client = new OkHttpClient(); RequestBody formBody = new FormBody.Builder() .add("client_id", "some_value") .add("client_secret", "some_value") .build(); Request request = new Request.Builder() .url("some_url") .post(formBody) .addHeader("authorization", "headers_here") .addHeader("cache-control", "no-cache") .build(); Response response = client.newCall(request).execute(); Sy...

Trim() not working with excel read apache poi in Java:

Java
Trim() not working with excel read apache poi in Java: While reading from the excel, you need to make sure you are considering this unicode value as well \u00A0 along with white spaces to handle all the possible inputs. Trim() does not help us for this, and below are the tries I made and got the solution, posted all here to help you with n number of possible use cases and solutions. Not working: Try 1: [java] row.getCell(0).getStringCellValue().replaceAll("\u00A0", ""); [/java]   Try 2: [java] row.getCell(0).getStringCellValue().trim(); [/java]   Try 3: Also after converting all the numbers to string also it does not work: [java] DataFormatter objDefaultFormat = new DataFormatter(); String value = objDefaultFormat.formatCellValue(row.

Arraylist to Array Using ToArray() in java

Articles, Java
Syntax: <T> T[] toArray(T[] a) Eg : String[] y = x.toArray(new String[0]); T - the runtime type of the array to contain the collection Parameters: a - the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose. Returns: an array containing the elements of this list Throws: ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in this list NullPointerException - if the specified array is null Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. Size of Array created is eq

hashMap Vs TreeMap Vs LinkedHashMap

Java, Quiz
════════════╦═════════════════════╦═══════════════════╦═════════════════════╗ ║ Property ║ HashMap ║ TreeMap ║ LinkedHashMap ║ ╠══════════════╬═════════════════════╬═══════════════════╬═════════════════════╣ ║ Iteration ║ no guarantee order ║ sorted according ║ ║ ║ Order ║ will remain constant║ to the natural ║ insertion-order ║ ║ ║ over time ║ ordering ║ ║ ╠══════════════╬═════════════════════╬═══════════════════╬═════════════════════╣ ║ Get/put ║ ║ ║ ║ ║ remove ║ O(1) ║ O(log(n)) ║ O(1) ║ ║ containsKey ║ ║ ║

Why StringBuffer/StringBuilder should be avoided in HashMap?

Java
Why StringBuffer/StringBuilder should be avoided in HashMap?   This is one of the core java interview question under collections category. Generally in all the map manipulations we use string and avoid stringbuffer and stringbuilders, because both are mutable. What is the issue if is mutable ? If you are keeping your hashmap's key/value as stringbuilder/stringbuffer then once after you inserted the value in the map, if any of the key/value modified then it actually affects inside the map as well, even if you won't perform the put operation with the modified value. Because stringbuffer/stringbuilder is mutable, it is actually modifying or changing the value in all the places, so it involves high risk to use these as key/values in any collections.     HashM...

Generate angular + Java code using yeoman & Jhipster

Angular, Articles, Java, Tutorials
Generate angular + Java code using yeoman & Jhipster Ensure you have installed yeoman (yo when dealing with npm and yarn) and jhipster (generator-jhipster when dealing with npm and yarn). If not installed, have a quick look at,     Step 1: Run yo jhipster     It will ask around 10 to 15 questions, you can provide the sample values as per the above screenshot and proceed to step 2:   Step 2: Angular 5 & Java Codes are generated   Yeoman generating the java source code:     yeoman generating the angular source code:     Both client (angular 5 - as on march 1st ) and server applications are successfully generated.     Step 3: Run mvnw to run the server application I ran mvnw (in...

Core Java Programming Interview Questions

Java
Core Java Programming Interview Questions: All these below are purely java programming interview questions collected from different attendees and different companies. Walk through all the programming questions and try to figure it out before checking the answers below.   Basics:   1.What is the output ? [java] package in.javadomain; class Basics { public static void main(String[] args) { System.out.println("I am main method" + new Basics()); } Basics() { System.out.println("I am construtor!"); } } [/java]   Output: [plain] I am construtor! I am main methodtrader.Basics@52e922 [/plain]     2.What is the output ? [java] package in.javadomain; class Basics { public static void main(String[] args) { Sy...

Java program to check whether the String is Pangram or Not

Java
Java program to check whether the String is Pangram or Not: What is Pangram ? If a given sentence has all the alphabets then it is a valid pangram.   If the below sentence is given as input then it has to return "Pangram" The quick brown fox jumps over the lazy dog   If the below sentence is given as input then it has to return "Not a Pangram" The quick brown fox jumps over the dog Pseudo code or Logic to check whether the string is pangram or not: Use Scanner nextLine() method to get the complete entered line as one whole string and convert all to lowercase using toLowerCase() method. Use getBytes() method to convert all the alpha letters to byte code values, which return byte[]. Create a arraylist with type byte/integer and iterate byte[] array whi...