要在Java中获取LinkedHashSet键值对,请使用entrySet()
方法。
让我们首先创建LinkedHashSet并添加一些元素-
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");
现在,获取一个键值对-
l.entrySet()
以下是从LinkedHashSet获取键值对的示例-
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()); } }
输出结果
LinkedHashMap elements... Key Value pair = [1=Jack, 2=Tom, 3=Jimmy, 4=Morgan, 5=Tim, 6=Brad]