我有一个集合 - 一个HashSet我想从中删除一些项目......"removals"集合中的所有项目都不在原始集合中.
我在命令行中指定"source"集的大小和"removals"集合的大小,并构建它们.源集仅包含非负整数; 删除集仅包含负整数.我测量使用System.currentTimeMillis()移除所有元素所需的时间,这不是世界上最准确的秒表,但在这种情况下绰绰有余,正如您将看到的那样.这是代码:
import java.util.*;
public class Test
{
public static void main(String[] args)
{
int sourceSize = Integer.parseInt(args[0]);
int removalsSize = Integer.parseInt(args[1]);
Set<Integer> source = new HashSet<Integer>();
Collection<Integer> removals = new ArrayList<Integer>();
for (int i = 0; i < sourceSize; i++)
{
source.add(i);
}
for (int i = 1; i <= removalsSize; i++)
{
removals.add(-i);
}
long start = System.currentTimeMillis();
source.removeAll(removals);
long end = System.currentTimeMillis();
System.out.println("Time taken: " + (end - start) + "ms");
}
}
Run Code Online (Sandbox Code Playgroud)
让我们从一个简单的工作开始: …