找到 34423 篇文章 关于编程

在 Java 中从 HashSet 获取同步集合

karthikeya Boyini
更新于 2019年7月30日 22:30:24

284 次浏览

synchronizedSet() 方法返回一个由指定排序集支持的同步(线程安全)排序集。首先,创建一个 HashSet 并添加元素 −Set hs = new HashSet(); hs.add(29); hs.add(879); hs.add(88); hs.add(788); hs.add(456); 现在,要获取同步集合,请使用 synchronizedSet() 方法 −Set s = Collections.synchronizedSet(hs); 下面是一个从 HashSet 获取同步集合的示例 −示例 在线演示import java.util.*; public class Demo { public static void main(String args[]) { Set hs = new HashSet(); hs.add(29); hs.add(879); ... 阅读更多

在 Java 中遍历 HashSet

Samual Sam
更新于 2019年7月30日 22:30:24

215 次浏览

要遍历 HashSet,首先声明 HashSet 并添加元素 −HashSet hs = new HashSet(); hs.add("P"); hs.add("Q"); hs.add("R"); 要进行遍历 −Enumeration e = Collections.enumeration(hs); 下面是一个在 HashSet 中进行遍历的示例 −示例 在线演示import java.util.*; public class Demo {    public static void main(String[] args) {       HashSet hs = new HashSet();       hs.add("P");       hs.add("Q");       hs.add("R");       Enumeration e = Collections.enumeration(hs);       while (e.hasMoreElements())       System.out.println(e.nextElement()); } }输出P Q R

在 Java 中查找 HashSet 的最小元素

karthikeya Boyini
更新于 2019年7月30日 22:30:24

2K+ 次浏览

要获取 HashSet 的最小元素,请使用 Collections.min() 方法。声明 HashSet −Set hs = new HashSet(); 现在让我们添加元素 −hs.add(29); hs.add(879); hs.add(88); hs.add(788); hs.add(456); 现在让我们获取最小元素 −Object obj = Collections.min(hs); 下面是一个查找 HashSet 最小元素的示例 −示例 在线演示import java.util.*; public class Demo { public static void main(String args[]) { // 创建哈希集合 Set hs = new HashSet(); hs.add(29); hs.add(879); ... 阅读更多

在 Java 中检查 HashSet 中是否存在元素

Samual Sam
更新于 2019年7月30日 22:30:24

2K+ 次浏览

要在 Java 中检查元素是否在 HashSet 中,请使用 contains() 方法。创建一个 HashSet −HashSet hs = new HashSet(); 向其中添加元素 −hs.add("R"); hs.add("S"); hs.add("T"); hs.add("V"); hs.add("W"); hs.add("Y"); hs.add("Z"); 要检查元素,例如这里的 S,请使用 contains() −hs.contains("S") 下面是在 HashSet 中检查元素的示例 −示例 在线演示import java.util.*; public class Demo { public static void main(String args[]) { HashSet hs = new HashSet(); // 向 HashSet 添加元素 ... 阅读更多

在 Java 中从 HashSet 中删除单个元素

karthikeya Boyini
更新于 2019年7月30日 22:30:24

1K+ 次浏览

要从 HashSet 中删除单个元素,请使用 remove() 方法。首先,创建一个 HashSet −HashSet hs = new HashSet(); 现在,向 HashSet 添加元素 −hs.add("R"); hs.add("S"); hs.add("T"); hs.add("V"); hs.add("W"); hs.add("Y"); hs.add("Z"); 现在让我们删除一个元素 −hs.remove("R"); 下面是从 HashSet 中删除单个元素的示例 −示例 在线演示import java.util.*; public class Demo { public static void main(String args[]) { HashSet hs = new HashSet(); // 向哈希集合添加元素 hs.add("R"); ... 阅读更多

在 Java 中检查两个 HashMap 是否相等

Samual Sam
更新于 2019年7月30日 22:30:24

2K+ 次浏览

要检查两个 HashMap 是否相等,请使用 equals() 方法。让我们首先创建第一个 HashMap −// 创建哈希映射 1 HashMap hm1 = new HashMap(); hm1.put("Shirts", new Integer(700)); hm1.put("Trousers", new Integer(600)); hm1.put("Jeans", new Integer(1200)); hm1.put("Android TV", new Integer(450)); hm1.put("Air Purifiers", new Integer(300)); hm1.put("Food Processors", new Integer(950)); 现在让我们创建第二个 HashMap −HashMap hm2 = new HashMap(); hm2.put("Shirts", new Integer(700)); hm2.put("Trousers", new Integer(600)); hm2.put("Jeans", new Integer(1200)); hm2.put("Android TV", new Integer(450)); hm2.put("Air Purifiers", new Integer(300)); hm2.put("Food Processors", new Integer(950)); 要检查两个 HashMap 是否相等,请使用 equals() 方法,如下所示 −hm1.equals(hm2) 下面是 ... 阅读更多

在 Java 中克隆 HashMap

karthikeya Boyini
更新于 2019年7月30日 22:30:24

170 次浏览

使用 clone() 方法克隆 HashMap。以下是包含元素的 HashMap −HashMap hm1 = new HashMap(); hm1.put("Shirts", new Integer(700)); hm1.put("Trousers", new Integer(600)); hm1.put("Jeans", new Integer(1200)); hm1.put("Android TV", new Integer(450)); hm1.put("Air Purifiers", new Integer(300)); hm1.put("Food Processors", new Integer(950)); 创建另一个 HashMap 并将第一个 HashMap 克隆到其中 −HashMap hm2 = (HashMap)hm1.clone(); 下面是在 Java 中克隆 HashMap 的示例 −示例 在线演示import java.util.*; public class Demo { public static void main(String args[]) { // 创建哈希映射 HashMap hm1 = new HashMap(); ... 阅读更多

在 Java 中从 HashMap 中检索 Map.Entry 元素集

Samual Sam
更新于 2019年7月30日 22:30:24

284 次浏览

创建一个 HashMap 并向其中添加元素 −HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); hm.put("Backpack", new Integer(1200)); 下面是检索 Map.Entry 元素集的代码片段 −Set s = hm.entrySet(); Iterator iter = s.iterator(); System.out.println("键\t值"); while (iter.hasNext()) { Map.Entry e = (Map.Entry) iter.next(); System.out.println(e.getKey() + " " + e.getValue()); } 下面是从 HashMap 中检索 Map.Entry 元素集的示例 −示例 在线演示import java.util.*; public class Demo { public static void main(String args[]) { ... 阅读更多

Java 程序用于检索 HashMap 中所有值的集合

karthikeya Boyini
更新于 2019年7月30日 22:30:24

223 次浏览

首先,创建一个 HashMap 并添加元素 −HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); hm.put("Backpack", new Integer(1200)); 现在,检索所有值 −Collection getValues = hm.values(); System.out.println("值..."); Iterator i = getValues.iterator(); while (i.hasNext()) { System.out.println(i.next()); } 下面是从 HashMap 获取所有值的集合的示例 −示例 在线演示import java.util.*; public class Demo { public static void main(String args[]) { // 创建哈希映射 HashMap hm = new HashMap(); ... 阅读更多

Java 程序用于将一个 Map 中的所有键值对复制到另一个 Map 中

Samual Sam
更新于 2019年7月30日 22:30:24

471 次浏览

要复制,请使用 putAll() 方法。让我们首先创建两个 Map −第一个 Map −HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); 第二个 Map −HashMap hm2 = new HashMap(); hm.put("Bag", new Integer(1100)); hm.put("Sunglasses", new Integer(2000)); hm.put("Frames", new Integer(800)); 现在,将一个 Map 的键值对复制到另一个 Map −hm.putAll(hm2); 下面是从一个 Map 将所有键值对复制到另一个 Map 的示例 −示例 在线演示import java.util.*; public class Demo { public static void main(String args[]) { // 创建哈希映射 1 HashMap hm = ... 阅读更多

广告
© . All rights reserved.