Posts

Showing posts with the label Collection

How to convert List to Array in Java

Sometimes we required to convert Arrays to List or List to array in java while doing programming. In this post, I am going to show you how to convert List to Array in java. You can convert List to Array in three different ways and those are listed below in ListUtil.java . ListUtil.java package com.javamakeuse.poc; import java.util.ArrayList; import java.util.List; public class ListUtil { // For reference type or wrapper classes public static Integer[] getIntegerArray(List<Integer> numberList) { Integer[] numArray = new Integer[numberList.size()]; return numberList.toArray(numArray); } // or public static String[] getStringArray(List<String> strList) { return strList.toArray(new String[strList.size()]); } // for primitive types public static int[] getPrimitiveArray(List<Integer> numberLsit) { int[] priArray = new int[numberLsit.size()]; for (int i = 0; i < numberLsit.size(); i++) { priArray[i] = numberLsit.get(i); } return priArray;...

How to sort HashMap in java example

So we have learned how to use HashMap and Hashtable now it's time to process them, I mean, although we have already seen How many ways to iterate HashMap here . In this post, I am going to show you how to sort a map. Sometimes we required to sort a map, of course based on different requirements you may need to sort HashMap or Hashtable based on key or values. But here I will show you a simple java program to sort map based on their keys. Of course TreeMap is Sorted Map and LinkedHashMap is Ordered Map, but yes they both have high payee Map implementation regarding space. Note: HashMap and Hashtable both are UN-Sorted and UN-Ordered Map implementation. Java Program to Sort HashMap or Hashtable package com.javamakeuse.poc; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Set; import java.util.TreeMap; public class HashTableExample { public static void main(String[] args) { HashMap<Character, S...

What is HashMap in java collection with example

In our previous post we have seen a very very popular java interview question, What is difference between HashMap and Hashtable here and in this post we are going to show you What is HashMap? and How to use HashMap? and also How to iterate HashMap . Well HashMap is an implementation of java.util.Map interface. HashMap basically allows you to store the element(s) in the form of key and value pair. HashMap provides all of the optional Map operations and allows one null as a key and any number of value as null. Of course you can say it's roughly equivalent to Hashtable except that HashMap is not synchronized and it also allow null as key unlike Hashtable which does not. HashMap does not maintain the insertion order, so you can say HashMap is un-sorted and un-order Map implementation. To put element(s) into HashMap it uses put() method and to retrieve back the element(s) from the HashMap, it uses get() method and both of these methods are gives constant time performance. F...

What is difference between HashMap and Hashtable

Last updated: September 2026 — refreshed with code examples, a comparison table, and interview Q&A. HashMap vs Hashtable is one of the most-asked Java interview questions of all time — right up there with why String is immutable and ArrayList vs LinkedList. Sometimes the interviewer just wants the differences; sometimes it's a gateway into concurrency. This post covers both. HashMap vs Hashtable: the differences Feature HashMap Hashtable Synchronized No — faster in single-threaded code Yes — every method is synchronized Null keys / values One null key, any number of null values No null keys or values (throws NullPointerException) Iteration order Unordered, but LinkedHashMap subclass preserves insertion order Unordered, no ordered variant Iterator behavior Fail-fast — throws ConcurrentModificationException on structural change during iteration Enumerator is not fail-fast Default capacity 16 11 Since Java 1.2 (Collections framework) Java 1.0 (legacy) The differe...

How Many Ways to Iterate HashMap in java

One of the interesting question as a developer you must have to know the ways of traversing a HashMap in java. Most of the time interviewer also asked you to list down the ways of traversing a HashMap in java. Below we have listed four different ways of traversing a HashMap in java. For Each Iterator EntrySet EntrySet with iterator TraversingMap.java package com.collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class TraversingMap { Map<String, String> pizza = new HashMap<String, String>(); public TraversingMap() { pizza.put("Large", "200"); pizza.put("Medium", "175"); pizza.put("Small", "150"); } // 1st way to traverse a HashMap public void forEach() { for (String key : pizza.keySet()) { System.out.println("Key= " + key + " Value= " + pizza.get(key)); } } // 2nd...

Which collection is better to store 10 millions of data between ArrayList and LinkedList in java?

If you came here from google or direct link you must have to read my previous post BFSI-domain:What is the different between ArrayList and LinkedList? Ok now lets start with a smart answer: Performance is always matter, If your business requirement is fast iteration of element based on index for example get(index) then must go with ArrayList Or If your business requirement is fast random insertion/deletion then must go with LinkedList. But if your interviewer says, that he does not want to fast random insertion/deletion of element or fast iteration of element based on index , he just want to store 10 million of element inside a collection between ArrayList and LinkedList. Most of the time candidate get fail to give the correct answer of this because candidates start comparing between insertion/deletion/iteration only. Most of the candidate forgot to comparing them about there internal structure(how they keep the elements) and fail to give the correct answer, but most of the ...