先挖个坑,日后来填!
跟着java核心技术365页的示例程序实现了一遍,发现在IDE环境下,跑不出结果,而在终端中运行就能出结果,代码如下:
package set;
import java.util.*;
public class SetTest {
public static void main(String[] args)
{
Set<String> words=new HashSet<String>();
long totalTime=0;
try(Scanner in=new Scanner(System.in))
{
while(in.hasNext())
{
String word=in.next();
System.out.println(word);
long callTime=System.currentTimeMillis();
words.add(word);
callTime=System.currentTimeMillis()-callTime;
totalTime+=callTime;
System.out.println("OK!"); //程序会一直卡在while里出不来
}
System.out.println("jump!");
}
Iterator<String> a=words.iterator();
int i=0;
while(a.hasNext()&&i<=20)
{
System.out.println(a.next());
i++;
}
System.out.println(words.size()+"distinct words."+totalTime+"mil