How to get Thread ID in Java with Example

ThreadId can be obtained from the Thread.currentThread()  method.

Read Also :   Lifecycle of Thread in Java

What is thread ID in java?

According to Oracle docs , thread ID is the identifier of the thread.  The thread ID is a positive long number generated when the thread is created.
Note : thread ID  remains unique and unchanged during the lifetime of the thread. When a thread is terminated then its thread ID may be reused.

There are two ways to get thread ID in Java , one is by getting the reference of the current thread i.e Thread.currentThread() and the other is using Thread.get

currentThread() method in Java

currentThread() is a static method , returns a reference to the currently executing thread object.

After getting the reference of the current thread , we will call the getId() method on Thread class.

getId() method in Java

8 Difference between HashMap and TreeMap with Example

HashMap and TreeMap both store key value pair in their object. Difference between HashMap and TreeMap is one of the question you must at least go through once before appearing for the java interview. I have already shared how HashMap works in java and how TreeMap works in java. In this article we will see not only the difference between HashMap and TreeMap but also the similarities between them with examples.

Read Also :  Difference between Iterator and ListIterator with Example

Difference between HashMap and TreeMap in Java 

1. Ordering : HashMap does not maintain any order. In other words , HashMap does not provide any guarantee that the element inserted first will be printed first.

import java.util.HashMap;

public class HashMapExample {
    public static void main(String[] args) {
        HashMap  obj1 = new HashMap();
        obj1.put("Alive is","Awesome");
OUTPUT : [Alive is = Awesome, Love = Yourself]   

Just like TreeSet , TreeMap  elements are also sorted according to the natural ordering of its elements . If TreeMap objects can not be sorted in natural order than use compareTo() method to sort the elements of TreeMap object.