Fetch key-valuepair from Java LinkedHashSet

Java 8Object Oriented ProgrammingProgramming

To fetch LinkedHashSet key-value pair in Java, use the entrySet() method.

Let us first create LinkedHashSet and add some elements −

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");

Now, fetch a key-value pair −

l.entrySet()

The following is an example to fetch key-value pair from LinkedHashSet −

Example

 Live Demo

import java.util.*;
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...");
      System.out.println("Key Value pair = "+l.entrySet());
   }
}

Output

LinkedHashMap elements...
Key Value pair = [1=Jack, 2=Tom, 3=Jimmy, 4=Morgan, 5=Tim, 6=Brad]
raja
Published on 30-Jan-2019 07:37:55
Advertisements