如何获取 java.util.Set 中项目的索引

发布于 2024-10-06 22:46:46 字数 367 浏览 0 评论 0原文

我知道 Set 和 List 之间的区别(允许唯一与允许重复、不有序/有序等)。我正在寻找的是一个保持元素有序的集合(这很容易),但我还需要能够恢复插入元素的索引。因此,如果我插入四个元素,那么我希望能够知道其中一个元素的插入顺序。

MySet<String> set = MySet<String>();
set.add("one");
set.add("two");
set.add("three");
set.add("four");

int index = set.getIndex("two");

因此,在任何给定时刻,我都可以检查是否已添加字符串,并获取该字符串在集合中的索引。有这样的事情吗,还是需要我自己实现?

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一绘本一梦想 2024-10-13 22:46:46

创建 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";

After creating Set just convert it to List and get by index from 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";

Util 类中的一个小型静态自定义方法会有所帮助:

 public static <T> int getIndex(Set<T> set, T value) {
   int result = 0;
   for (T entry:set) {
     if (entry.equals(value)) return result;
     result++;
   }
   return -1;
 }
  

如果您需要/想要一个 Set 并提供 的类getIndex() 方法,我强烈建议实现一个新的 Set 并使用装饰器模式:

 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;
   }
 }

A small static custom method in a Util class would help:

 public static <T> int getIndex(Set<T> set, T value) {
   int result = 0;
   for (T entry:set) {
     if (entry.equals(value)) return result;
     result++;
   }
   return -1;
 }
  

If you need/want one class that is a Set and offers a getIndex() method, I strongly suggest to implement a new Set and use the decorator pattern:

 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;
   }
 }
束缚m 2024-10-13 22:46:46

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.

や莫失莫忘 2024-10-13 22:46:46

一种解决方案(虽然不是很漂亮)是使用 Apache 通用列表/集突变,

import org.apache.commons.collections.list.SetUniqueList;

final List<Long> vertexes=SetUniqueList.setUniqueList(new LinkedList<>());

它是一个没有重复项的列表

https://commons.apache.org/proper/commons-collections/javadocs/api-3.2.2 /index.html?org/apache/commons/collections/list/SetUniqueList.html

One solution (though not very pretty) is to use Apache common List/Set mutation

import org.apache.commons.collections.list.SetUniqueList;

final List<Long> vertexes=SetUniqueList.setUniqueList(new LinkedList<>());

it is a list without duplicates

https://commons.apache.org/proper/commons-collections/javadocs/api-3.2.2/index.html?org/apache/commons/collections/list/SetUniqueList.html

﹏半生如梦愿梦如真 2024-10-13 22:46:46

如何将字符串添加到哈希表中,其中值是索引:

  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");

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");
再可℃爱ぅ一点好了 2024-10-13 22:46:46

您可以将设置的数据发送到新列表

ArrayList<String> myList = new ArrayList<>();
myList.addAll(uniqueNameSet);
myList.indexOf("xxx");

you can send your set data to a new list

ArrayList<String> myList = new ArrayList<>();
myList.addAll(uniqueNameSet);
myList.indexOf("xxx");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文