Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在if语句中使用java空HashSet? publicstaticvoidmain(字符串[]args){ Map test=newhashmap(); test.put(“1”,新的HashSet()); 系统输出打印LN(测试); System.out.println(test.get(“1”)); if(test.get(“1”)==null){ System.out.println(“你好世界”); } }_Java_If Statement_Data Structures_Set_Hashset - Fatal编程技术网

如何在if语句中使用java空HashSet? publicstaticvoidmain(字符串[]args){ Map test=newhashmap(); test.put(“1”,新的HashSet()); 系统输出打印LN(测试); System.out.println(test.get(“1”)); if(test.get(“1”)==null){ System.out.println(“你好世界”); } }

如何在if语句中使用java空HashSet? publicstaticvoidmain(字符串[]args){ Map test=newhashmap(); test.put(“1”,新的HashSet()); 系统输出打印LN(测试); System.out.println(test.get(“1”)); if(test.get(“1”)==null){ System.out.println(“你好世界”); } },java,if-statement,data-structures,set,hashset,Java,If Statement,Data Structures,Set,Hashset,第一个println获取我{1=[]} 第二个得到了我[] 我正试图打印“Hello world”,但if语句无法通过。 空哈希集[]是否不等于null 如何在这个if语句中使用空的HashSet?空的HashSet与空的HashSet之间有区别,空的表示“什么都没有”。空的HashSet是一个实际的HashSet,但碰巧没有任何元素。这类似于null与空字符串“”不同,空字符串中没有字符 要检查HashSet是否为空,请使用isEmpty方法: public static void main(

第一个println获取我
{1=[]}
第二个得到了我
[]

我正试图打印“Hello world”,但if语句无法通过。
空哈希集
[]
是否不等于
null


如何在这个if语句中使用空的HashSet?

空的HashSet与空的
HashSet
之间有区别,空的
表示“什么都没有”。空的
HashSet
是一个实际的
HashSet
,但碰巧没有任何元素。这类似于
null
与空字符串
”不同,空字符串中没有字符

要检查
HashSet
是否为空,请使用
isEmpty
方法:

public static void main(String[] args) {
    Map<String, HashSet<String>> test = new HashMap<String, HashSet<String>>();

    test.put("1", new HashSet<String>());
    System.out.println(test);

    System.out.println(test.get("1"));

    if(test.get("1") == null){
        System.out.println("Hello world");
    }
}
希望这有帮助

空的
HashSet
[]
是否不等于
null

没错,不是。这正是代码的行为方式的原因

要检查
null
和空集,请使用以下结构:

if(test.get("1").isEmpty()){
    System.out.println("Hello world");
}
HashSet=test.get(“1”);
if(set==null | | set.isEmpty()){
...
}

空的
哈希集
不是
null
。通过使用

或使用,

或者,您也可以将其注释掉

if (test.get("1") == null || test.get("1").isEmpty()) {
//test.put(“1”,new HashSet());
系统输出打印LN(测试);

然后
test.get(“1”)
null

应该使用Set_Obj.isEmpty()方法。这将返回一个布尔值,检查集合中是否有任何元素(true)

包(
null
)与空包(
HashSet
)是否相同?(注意:这里我使用的是“bag”的真实含义,而不是数据结构。)在Java中,两个对象(即非原语)上的
=
总是进行比较,以查看它们是否是对同一对象的引用(或者都为null)。某些语言在某些情况下能够重新定义
==
,以便比较内容。Java没有。(java通常使用)(<代码)>比较内容,但这对您没有帮助,因为<代码>(代码)>代码> > HASSETSE/<代码>不认为空集等于空引用。他们可以用这种方式定义,但它们没有。
if (test.get("1") == null || test.get("1").size() == 0) {
if (test.get("1") == null || test.get("1").isEmpty()) {
// test.put("1", new HashSet<String>());
System.out.println(test);