Java Tutorials

Java is a general-purpose programming language that is a concurrent, class-based, and object-oriented language. Java follows the concept of “write once and run anywhere (WORA).” This means that compiled Java code can be run on all different platforms that support Java. There’s no need for recompilation.

Eclipse IDE

Chapter 1 How to Download and Install Eclipse IDE
Chapter 2 How to Clone a project from GitLab using Eclipse
Chapter 3 How to Export Eclipse projects to GitLab

IntelliJ IDE

Chapter 1 How to install IntelliJ on Windows
Chapter 2 How to create a Java project in IntelliJ
Chapter 3 How to Clone a project from GitLab using IntelliJ
Chapter 4 How to Export IntelliJ project to GitLab

Basics of Java

Chapter 1 How to Download & Install Java JDK 11 in Windows
Chapter 2 Data Types and Operators in Java
Chapter 3 Decision Making in Java – If, If Else, Switch, Break, Continue
Chapter 4 Loop Control Statements in Java – For, While, Do While, Enhanched For Loop
Chapter 5 String Manipulation
Chapter 6 Difference between == and equals() method in Java
Chapter 7 Arrays in Java
Chapter 8 Java Access Modifiers: Explained with Examples
Chapter 9 ArrayList in Java
Chapter 10 How to compare ArrayLists – contains?
Chapter 11 How to compare ArrayLists – containsAll method?
Chapter 12 Methods in Java
Chapter 13 Method Overloading in Java
Chapter 14 Constructors in Java   
Chapter 15 This Keyword in Java   
Chapter 16 Static Keyword – Static Variable and Static Method in Java
Chapter 17 Difference between Static Method and Non-Static Method
Chapter 18 How to use Java Lambda expression to create thread via Runnable function
Chapter 19 runAsync and supplyAsync in ComputableFuture in Java8
Chapter 20 HashMap in Java
Chapter 21 LinkedHashMap in Java
Chapter 22 Iterators in Java

OOPs Concepts

Chapter 1 Class and Object in Java
Chapter 2 Inheritance in Java
Chapter 3 Encapsulation in Java
Chapter 4 Polymorphism in Java
Chapter 5 Abstraction in Java
Chapter 6 Interface in Java
Chapter 7 Difference between Abstract Class and Interface

Exceptions in Java

Chapter 1 Exception Handling in Java
Chapter 2 Java Exceptions Tutorial: Built-in and User-defined Exceptions
Chapter 3 Flow control in try catch finally in Java
Chapter 4 Multiple Catch Exceptions
Chapter 5 Throw in Java
Chapter 6 Throws in Java

Data Handling (Excel Manipulation)

Chapter 1 How to download and install Apache POI
Chapter 2 Reading Excel Data with Apache POI in Java
Chapter 3 How to Write Data to Excel File in Java using Apache POI
Chapter 4 How to update existing excel in Java
Chapter 5 Java Excel Tutorial: Creating Excel with Formulas Using Apache POI
Chapter 6 Change Font Style in Excel with Apache POI – NEW

Multiple Choice Questions

Chapter 1 Multiple questions on Exception Handling in Java

Java Library

Chapter 1 AssertJ – Fluent Assertions in Java

LinkedHashMap in Java

HOME

The LinkedHashMap Class is just like HashMap with an additional feature of maintaining an order of elements inserted into it. However, the linked hash map is based on both a hash table and a linked list to enhance the functionality of the hash map.

Create a LinkedHashMap object as shown below from:-

import java.util.LinkedHashMap;

Syntax of LinkedHashMap

LinkedHashMap<String, String> employeeDetail = new LinkedHashMap<String, String>();

LinkedHashMap<String, String> employeeDetail = new LinkedHashMap<>();

LinkedHashMap<String, Integer> employeeDetail = new LinkedHashMap<String, Integer>();

Adding the Elements

Below is an example where we are adding items to HashMap by using put() method.

import java.util.LinkedHashMap;

public class LinkedHashMap_Demo {
    public static void main(String a[]) {

        // Creating an empty LinkedHashMap
        LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<String, String>();

        // Adding entries in Map using put() method
        linkedHashMap.put("Tim", "DBA");
        linkedHashMap.put("Cillian", "SDET");
        linkedHashMap.put("Patrick", "BA");
        linkedHashMap.put("Brendan", "Dev");

        // Printing all entries inside Map
        System.out.println("Linked HashMap :" + linkedHashMap);

    }
}

Accessing an Element

To access a value in the LinkedHashMap, use the get() method and refer to its key:-

import java.util.LinkedHashMap;

public class LinkedHashMap_Demo {
    public static void main(String a[]) {

        // Creating an empty LinkedHashMap
        LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<String, String>();

        // Adding entries in Map using put() method
        linkedHashMap.put("Tim", "DBA");
        linkedHashMap.put("Cillian", "SDET");
        linkedHashMap.put("Patrick", "BA");
        linkedHashMap.put("Brendan", "Dev");

        // Printing all entries inside Map
        System.out.println("Linked HashMap :" + linkedHashMap);

        // Access a value
        System.out.println("Access Value of Key Patrick :" + linkedHashMap.get("Patrick"));

    }
}

Removing an Element

To remove an item, use the remove() method and refer to the key:

import java.util.LinkedHashMap;

public class LinkedHashMap_Demo {
    public static void main(String a[]) {

        // Creating an empty LinkedHashMap
        LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<String, String>();

        // Adding entries in Map using put() method
        linkedHashMap.put("Tim", "DBA");
        linkedHashMap.put("Cillian", "SDET");
        linkedHashMap.put("Patrick", "BA");
        linkedHashMap.put("Brendan", "Dev");

        // Printing all entries inside Map
        System.out.println("Linked HashMap :" + linkedHashMap);

        // Removing the mapping with Key Brendan
        linkedHashMap.remove("Brendan");

        // Printing the updated map
        System.out.println("Updated Map :" + linkedHashMap);

    }
}

To remove all items, use the clear() method:-

import java.util.LinkedHashMap;

public class LinkedHashMap_Demo {
    public static void main(String a[]) {

        // Creating an empty LinkedHashMap
        LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<String, String>();

        // Adding entries in Map using put() method
        linkedHashMap.put("Tim", "DBA");
        linkedHashMap.put("Cillian", "SDET");
        linkedHashMap.put("Patrick", "BA");
        linkedHashMap.put("Brendan", "Dev");

        // Printing all entries inside Map
        System.out.println("Linked HashMap :" + linkedHashMap);

        // Removing the mapping with Key Brendan
        linkedHashMap.clear();

        // Printing the updated map
        System.out.println("Updated Map :" + linkedHashMap);

    }
}

import java.util.LinkedHashMap;
import java.util.Map;

public class LinkedHashMap_Demo {
    public static void main(String a[]) {

        // Creating an empty LinkedHashMap
        LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<String, String>();

        // Adding entries in Map using put() method
        linkedHashMap.put("Tim", "DBA");
        linkedHashMap.put("Cillian", "SDET");
        linkedHashMap.put("Patrick", "BA");
        linkedHashMap.put("Brendan", "Dev");

        // For-each loop for traversal over Map
        for (Map.Entry<String, String> mapElement : linkedHashMap.entrySet()) {

            String key = mapElement.getKey();

            // Finding the value
            // using getValue() method
            String value = mapElement.getValue();

            // Printing the key-value pairs
            System.out.println(key + " : " + value);
        }
    }
}

Methods in LinkedHashMap

import java.util.LinkedHashMap;

public class LinkedHashMap_Demo {
    public static void main(String a[]) {

        // Creating an empty LinkedHashMap
        LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<String, String>();

        // Adding entries in Map using put() method
        linkedHashMap.put("Tim", "DBA");
        linkedHashMap.put("Cillian", "SDET");
        linkedHashMap.put("Patrick", "BA");
        linkedHashMap.put("Brendan", "Dev");

        // Getting and printing value for a specific key
        System.out.println("Getting value for key 'Patrick' :" + linkedHashMap.get("Patrick"));

        // Getting size of Map using size() method
        System.out.println("Size of the map :" + linkedHashMap.size());

        // Checking whether Map is empty or not
        System.out.println("Is map empty? :" + linkedHashMap.isEmpty());

        // Using containsKey() method to check for a value
        System.out.println("Contains value 'SDET' "  + linkedHashMap.containsValue("SDET"));

        // Using containsKey() method to check for a key
        System.out.println("Contains key 'Brendan'? :" + linkedHashMap.containsKey("Brendan"));

        // Removing entry using remove() method
        System.out.println("Delete element 'Brendan' :" + linkedHashMap.remove("Brendan"));

        // Printing mappings to the console
        System.out.println("Mappings of LinkedHashMap :" + linkedHashMap);
        }
}

We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!