Add elements to LinkedHashMap collection in Java

Java 8Object Oriented ProgrammingProgramming

Use the put() method to add elements to LinkedHashMap collection.

First, let us create a LinkedHashMap −

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

Now, add elements −

l.put("1", "Jack");
l.put("2", "Tom");
l.put("3", "Jimmy");
l.put("4", "Morgan");
l.put("5", "Tim");
l.put("6", "Brad");

The following is an example to add elements to LinkedHashMap collection −

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\n"+l);
   }
}

Output

LinkedHashMap elements
{1=Jack, 2=Tom, 3=Jimmy, 4=Morgan, 5=Tim, 6=Brad}
raja
Published on 16-Jan-2019 12:05:02
Advertisements