Difference between HashMap and LinkedHashMap in Java with Example

HashMap and LinkedHashMap are common implementation of Map.The main difference between HashMap and LinkedHashMap is that LinkedHashMap maintains insertion order of keys, order in which keys are inserted to LinkedHashMap while HashMap does not maintain any order of keys.
In this post we will share the similarities, differences between HashMap and LinkedHashMap along with examples.

Difference between HashMap and LinkedHashMap

1. Insertion order :  HashMap does not maintain the order while LinkedHashMap maintains the insertion order of elements in java.

2. Memory : The second difference between HashMap and LinkedHashMap is memory. LinkedHashMap requires more memory than HashMap because of the ordering feature. Internally LinkedHashMap uses doubly Linked List to maintain order of elements.

How to Read Text File in Java

In java applications, sometimes we need to read a text file and process it into something meaningful. There are multiple ways through which we can read text files for e.g FileReader, BufferedReader or Scanner.To read a text file line by line we can use BufferedReader or Scanner.

Below are the different ways of reading a text file in java :

1.  FileReader : It is convenient for text files in the system's default encoding.

2. FileInputStream : Files that contain weird characters.Files can be binary or text.

Reading Text Files in Java

Use FileReader if you want to read an ordinary text file in the system's default encoding, and wrapped it in a BufferedReader.

3 ways to Create a New File in Java

The first task in the java i/o is to create a new file. Creating a new file is an easy task and it can be done in 3 ways. The three ways are
1. using java.io.File class
2. using java.io.FileOutputStream class
3. using java.nio.file.Files class

1. Create a file in java using java.io.File class

We will see how to create a file in java using createNewFile() method. If a file does not exist, then this method will create a file and returns true. If the file does not exist then it returns false.

import java.io.*;
import java.util.*;

public class CreateFileUsingFileClass

System.out.println in Java and How it works

System.out.println() is the most used statement in the java language.In this java tutorial we will learn what is System.out.println() and how it works. It is one of the most executed statement in the history of java. We shortly call it SOP. What is System.out.println() this question has been asked many times in java interviews.Let's find out below:

What is System.out.println

In simple words, System.out.println is a java statement that prints the argument passed, into the System.out which is generally stdout.

  • System - is a final class in java.lang package.According to Oracle docs: "Among the facilities provided by the System class are standard input, standard output, and error output streams;access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array".
  • Java Collections Framework Tutorial

    Collections in java are used in almost every enterprise application. Java Collections framework consists of interfaces and classes which helps in storing and processing the data efficiently. Java Collections Tutorial will look into each of the classes.

    Read Also :  Collections in Java Interview Questions 

    Java Collections Interfaces and Classes

  • Collection Interface