- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正尝试在文本文件中搜索大量可能性。
例如,我想搜索包含唯一名称的文本文件。现在,如果我找到名称 X,那么我想将 X 存储在另一个文件中。
这里的问题我有超过 1000 个唯一名称,我不想为每个唯一名称执行 1000 次搜索调用和 if 语句。
在 java/javascript/php 中有更好的方法吗?
最佳答案
你有一组名字,你想找出哪些名字与另一组名字匹配。
Set<String> namesFromFile = readFile(filename);
Set<String> namesToMatch = readFile(matchingNames);
namesToMatch.retainAll(namedFromFile);
retainAll
是一个 O(n) 操作,其中 n
是较小集合的大小。在 Java 中,一组 1000 个值的 retainAll
可能需要几毫秒。
Set.retainAll()做以下事情
Retains only the elements in this set that are contained in the specified collection (optional operation). In other words, removes from this set all of its elements that are not contained in the specified collection. If the specified collection is also a set, this operation effectively modifies this set so that its value is the intersection of the two sets.
1000 个元素的集合太小了,很难准确测试,所以在这个测试中,我测试了一个 10 倍大的元素,即 10,000 个元素与 100,000 个元素的集合。
public static void main(String... args) {
Set<String> names1 = generateStrings(100000, 2);
Set<String> names2 = generateStrings(10000, 3);
for (int i = 0; i < 10; i++) {
long start = System.nanoTime();
Set<String> intersect= new HashSet<String>(names2);
intersect.retainAll(names1);
long time = System.nanoTime() - start;
System.out.printf("The intersect of %,d and %,d elements has %,d and took %.3f ms%n",
names1.size(), names2.size(), intersect.size(), time / 1e6);
}
}
private static Set<String> generateStrings(int number, int multiple) {
Set<String> set = new HashSet<String>();
for (int i = 0; i < number; i++)
set.add(Integer.toBinaryString(i * multiple));
return set;
}
打印
The intersect of 100,000 and 10,000 elements has 5,000 and took 21.173 ms
The intersect of 100,000 and 10,000 elements has 5,000 and took 10.785 ms
The intersect of 100,000 and 10,000 elements has 5,000 and took 9.597 ms
The intersect of 100,000 and 10,000 elements has 5,000 and took 3.414 ms
The intersect of 100,000 and 10,000 elements has 5,000 and took 2.791 ms
The intersect of 100,000 and 10,000 elements has 5,000 and took 2.629 ms
The intersect of 100,000 and 10,000 elements has 5,000 and took 2.689 ms
The intersect of 100,000 and 10,000 elements has 5,000 and took 2.753 ms
The intersect of 100,000 and 10,000 elements has 5,000 and took 2.704 ms
The intersect of 100,000 and 10,000 elements has 5,000 and took 2.645 ms
关于algorithm - 大量可能性的最佳搜索技术,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13518279/
是否可以在表格 View 中提供单独的单元格样式? 最佳答案 是的,看看几乎所有的苹果应用程序。他们倾向于使用不同控件的表格进行设置。这就是表格单元格具有不同重用标识符的原因。这样您就可以将正确的类型
我想知道是否可以在 Javascript 中做这样的事情: one(plus(nine())); // returns 10 看起来是这样,但我不确定如何将 one() 的值传递给 plus() 函数
我正在编写一个算法,在给定模型的情况下,我计算数据集列表的可能性,然后需要对每个可能性进行归一化(概率)。所以像 [0.00043, 0.00004, 0.00321] 这样的东西可能会被转换成 [0
我想检测是否可以进行局域网唤醒。 在我的路由器(Tomato 固件)上有一个包含信息的表格 - 当显示设备“事件(在 ARP 中)”时 - 可以通过 WOL(离线 Linux 电脑)打开该设备。 我想
我正在寻找一种方法来解析具有几个可能使用的不同终止字符的子字符串。我应该使用不同的方法还是有办法使用正则表达式来整理字符? 我当前的代码使用: smallstring = bigstring.subs
目前我有可能使用 surefire 插件在 maven 上运行多个测试,如下所示: mvn clean test -Dsurefire.suiteXmlFiles=test1.xml,test2.xm
最近,我成功地为 HDFS 和 YARN 启用了 HA。现在我有一个事件的和备用的名称节点,自动故障转移工作正常。我正在使用 Cloudera Manager 和 CDH 5。 我有以下问题。 例如,
我想要最简单的动词,它给出给定长度的所有 bool 列表的列表。 例如 f=. NB. Insert magic here f 2 0 0 0 1 1 0 1 1 f 3 0 0 0
这将是一个井字游戏实现: data Row = A | B | C deriving (Show, Read, Eq, Ord, Enum, Bounded) data Column = X
这是一个假设性的问题: 我想构建一个 Chrome 扩展程序,它会跟踪用户在该扩展程序处于事件状态的网页子集上的点击情况,并通过 AJAX 将数据作为 POST 或 GET 请求发送到我在某处运行的外
我们想使用 Entity Framework (.NET 4.0) 构建可以处理 Sql Server、MySQL 和 Oracle 的应用程序。也许 Sqlite 也是。 通过配置文件中的一些设置应
是否可以在 iPhone 上通过指定网络的 SSID 来创建数据连接? 是否可以从应用程序检查具有指定 SSID 的网络的信号/可用性? 问候,斯腾 最佳答案 遗憾的是,如果不使用私有(private
我正在使用各种 lambda 表达式语法测试性能差异。如果我有一个简单的方法: public IEnumerable GetItems(int point) { return this.ite
Effective Java 第 2 版的第 16 条,支持组合优于继承 说如下 “如果父类(super class)在后续版本中获得了一个新方法并且你运气不好给子类一个具有相同签名的方法和不同的返回
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我想知道 Tic Tac Toe 有多少种可能性,所以我在网上搜索并找到了一个数学定理,它表明 Tic Tac Toe 中有 255168 种可能的游戏。 网站:http://www.se16.inf
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我想在单个值中存储 4 个 boolean 可能性。例如,我想要一个单一的值来判断一个人是否: IsSingle IsGraduate IsMale IsLookingForPartner 那么将它们
我的 Wicket 口应用程序遇到了一些问题。 问题出在一个名为 OverviewPage 的页面上,这里有一些面板,例如 ListPanel,其中有我的 RepeatingView。 这个Repea
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 7年前关闭。 Improve this questi
我是一名优秀的程序员,十分优秀!