从Java LinkedHashMap删除值
使用该remove()
方法从LinkedHashMap中删除单个值-
首先,创建一个LinkedHashMap并添加一些元素-
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");
现在,假设您需要从LinkedHashMap中删除元素2。为此,请使用remove()
方法-
Object ob = l.remove("2");
以下是从Java中的LinkedHashMap中删除值的示例-
示例
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...\n"+l); //删除一个元素 Object ob = l.remove("2"); System.out.println("\nLinkedHashMap elements after removing an element...\n"+l); } }
输出结果
LinkedHashMap elements... {1=Jack, 2=Tom, 3=Jimmy, 4=Morgan, 5=Tim, 6=Brad} LinkedHashMap elements after removing an element... {1=Jack, 3=Jimmy, 4=Morgan, 5=Tim, 6=Brad}