如何在 java.util.Set 中获取项目的索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4395871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get index of an item in java.util.Set
提问by Jose L Martinez-Avial
I know the differences between Set and List(unique vs. duplications allowed, not ordered/ordered, etc). What I'm looking for is a set that keeps the elements ordered(that's easy), but I also need to be able to recover the index in which an element was inserted. So if I insert four elements, then I want to be able to know the order in which one of them was inserted.
我知道 Set 和 List 之间的区别(允许唯一与重复,未排序/有序等)。我正在寻找的是一个保持元素有序的集合(这很容易),但我还需要能够恢复插入元素的索引。因此,如果我插入四个元素,那么我希望能够知道其中一个元素的插入顺序。
MySet<String> set = MySet<String>();
set.add("one");
set.add("two");
set.add("three");
set.add("four");
int index = set.getIndex("two");
So at any given moment I can check if a String was already added, and get the index of the string in the set. Is there anything like this, or I need to implement it myself?
所以在任何给定的时刻,我都可以检查是否已经添加了一个字符串,并获取该字符串在集合中的索引。有没有这样的东西,或者我需要自己实现它?
采纳答案by Andreas Dolk
A small static custom method in a Util
class would help:
类中的一个小的静态自定义方法Util
会有所帮助:
public static int getIndex(Set<? extends Object> set, Object value) {
int result = 0;
for (Object entry:set) {
if (entry.equals(value)) return result;
result++;
}
return -1;
}
If you need/want one class that is aSet
and offers a getIndex()
method, I strongly suggest to implement a new Set
and use the decorator pattern:
如果你需要/想一个类,它是一个Set
,并提供getIndex()
方法,我强烈建议实施新的Set
和使用Decorator模式:
public class IndexAwareSet<T> implements Set {
private Set<T> set;
public IndexAwareSet(Set<T> set) {
this.set = set;
}
// ... implement all methods from Set and delegate to the internal Set
public int getIndex(T entry) {
int result = 0;
for (T entry:set) {
if (entry.equals(value)) return result;
result++;
}
return -1;
}
}
回答by Vladimir Ivanov
you can extend LinkedHashSet
adding your desired getIndex()
method. It's 15 minutes to implement and test it. Just go through the set using iterator and counter, check the object for equality. If found, return the counter.
您可以扩展LinkedHashSet
添加所需的getIndex()
方法。实现和测试它需要 15 分钟。只需使用迭代器和计数器遍历集合,检查对象是否相等。如果找到,返回计数器。
回答by Kiryl Ivanou
After creating Set just convert it to List and get by index from List:
创建 Set 后,只需将其转换为 List 并从 List 中通过索引获取:
Set<String> stringsSet = new HashSet<>();
stringsSet.add("string1");
stringsSet.add("string2");
List<String> stringsList = new ArrayList<>(stringsSet);
stringsList.get(0); // "string1";
stringsList.get(1); // "string2";
回答by abinkleysf
How about add the strings to a hashtable where the value is an index:
如何将字符串添加到值为索引的哈希表中:
Hashtable<String, Integer> itemIndex = new Hashtable<>();
itemIndex.put("First String",1);
itemIndex.put("Second String",2);
itemIndex.put("Third String",3);
int indexOfThirdString = itemIndex.get("Third String");
回答by Stan Sokolov
One solution (though not very pretty) is to use Apache common List/Set mutation
一种解决方案(虽然不是很漂亮)是使用 Apache 常见的 List/Set 突变
import org.apache.commons.collections.list.SetUniqueList;
final List<Long> vertexes=SetUniqueList.setUniqueList(new LinkedList<>());
it is a list without duplicates
这是一个没有重复的列表