以下Java代码的输出是什么?

时间:2016-07-01 10:59:14

标签: java collections hashset treeset linkedhashset

class TryTree
{
public static void main(String[] args) 
{
Set<Integer>uniqueEmpid = new TreeSet<Integer>();
uniqueEmpid.add(113);
uniqueEmpid.add(111);
uniqueEmpid.add(112);
uniqueEmpid.add(111);
System.out.println(uniqueEmpid);
}
}

如果是Hashset或LinkedHashSet而不是Treeset会发生什么?

1 个答案:

答案 0 :(得分:1)

这将显示编译错误,因为Set并且TreeSet未定义。

cannot find Symbol Set<Integer>uniqueEmpid = new TreeSet<Integer>();

您应该使用import将内置和用户定义的包导入java源文件。

例如

import java.util.Date;
class MyDate extends Date
{
 //statement.
}

如果要从特定包导入所有类,请执行此操作。

import java.util.*; 
class MyDate extends Date
{
//statement;
}

更多细节了解有关java类和OOP概念的更多信息。

相关问题