Get Size of Java LinkedHashMap

Java 8Object Oriented ProgrammingProgramming

The size() method is used to get the size of LinkedHashMap in Java.

Create a LinkedHashMap and add some elements to it −

LinkedHashMap<String,String> l = new LinkedHashMap<String,String>();
l.put("1", "Jack");
l.put("2", "Tom");
l.put("3", "Jimmy");
l.put("4", "Morgan");
l.put("5", "Tim");
l.put("6", "Brad");

Get the size now −

l.size()

The following is an example to get the size of LinkedHashMap −

Example

 Live Demo

import java.util.LinkedHashMap;
public class Demo {
   public static void main(String[] args) {
      LinkedHashMap<String,String> l = new LinkedHashMap<String,String>();
      l.put("1", "Jack");
      l.put("2", "Tom");
      l.put("3", "Jimmy");
      l.put("4", "Morgan");
      l.put("5", "Tim");
      l.put("6", "Brad");
      System.out.println("LinkedHashMap elements: "+l);
      System.out.println("Size = "+l.size());
   }
}

Output

LinkedHashMap elements: {1=Jack, 2=Tom, 3=Jimmy, 4=Morgan, 5=Tim, 6=Brad}
Size = 6
raja
Published on 30-Jan-2019 11:44:21
Advertisements