Get size of LinkedHashMap in Java

In this tutorial, we will learn how to Get size of LinkedHashMap in Java.

LinkedHashMap in Java is Hash table and Linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. It maintains a linked list of the entries in the map, in the order in which they were inserted. This allows insertion-order iteration over the map. That is, when iterating through a collection-view of a LinkedHashMap, the elements will be returned in the order in which they were inserted. You can also create a LinkedHashMap that returns its elements in the order in which they were last accessed.

In we want to calculate the size or no. of elements in LinkedHashMap, then this class in Java provides size() method to get size of LinkedHashMap. The size method returns the size or the no. of elements in LinkedHashMap. The syntax of size() method is:

Syntax of size() method:

Returns:
the number of elements in this map

Example:

Output:

Related posts

Leave a Comment