我有大量 ID,可以将其存储在 HashSet 或 String 中 即
String strIds=",1,2,3,4,5,6,7,8,.,.,.,.,.,.,.,1000,";
Or
HashSet<String> setOfids = new HashSet<String>();
setOfids.put("1");
setOfids.put("2");
.
.
.
setOfids.put("1000");
此外,我想对 ID 执行搜索
我应该使用哪个来获得更好的性能(更快且内存效率更高)
1) strIds.indexOf("someId");
or
2) setOfids.contains("someId");
请告诉我任何其他方式,这样我就可以做同样的事情。 感谢您查看这里:)
最佳答案
哈希表查找是“恒定时间”,即它不会随着 id 的数量而增长。
但是字符串中所有 id 的紧凑字符串需要最少的内存。
所以,请做出决定:最快的检索还是最少的存储!
关于Java : Searching Ids from hashset or String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24325494/