java 一个用于从 Set 中获取子列表的衬垫

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12490955/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:07:28  来源:igfitidea点击:

One liner for getting a sublist from a Set

javaguavaapache-commons-collection

提问by yegor256

Is there a one-liner (maybe from Guava or Apache Collections) that gets a sublist from a set. Internally it should do something like this:

是否有一个单行(可能来自 Guava 或 Apache Collections)从集合中获取子列表。在内部它应该做这样的事情:

public <T> List<T> sublist(Set<T> set, int count) {
  Iterator<T> iterator = set.iterator();
  List<T> sublist = new LinkedList<T>();
  int pos = 0;
  while (iterator.hasNext() && pos++ < count) {
    sublist.add(iterator.next());
  }
  return sublist;
}

Obviously, if there are not enough elements it has to return as many as possible.

显然,如果没有足够的元素,它必须返回尽可能多的元素。

回答by Louis Wasserman

With Guava:

番石榴:

return FluentIterable.from(set) 
  .limit(count)
  .toImmutableList();

(Also, this won't actually iterate over the whole set, in contrast to most of these other solutions -- it'll actuallyonly iterate through the first countelements and then stop.)

(此外,与大多数其他解决方案相比,这实际上不会遍历整个集合——它实际上只会遍历第一个count元素然后停止。)

回答by Dirk

(new LinkedList<Object>(mySet)).sublist(0, Math.min(count, mySet.size()))

But please note: the code (even your original code) is a little bit smelly, since iteration order of sets depends on the actual set implementation in question (it's totally undefined in HashSetand the key order for TreeSets). So, it is actually an open question, which elements make it into the final sublist.

但请注意:代码(甚至您的原始代码)有点臭,因为集合的迭代顺序取决于所讨论的实际集合实现(它完全未定义 inHashSetTreeSets的关键顺序)。所以,这实际上是一个悬而未决的问题,哪些元素会进入最终的子列表。

回答by Baz

This should do it:

这应该这样做:

return (new LinkedList<T>(set)).subList(0, count);

But ensure, that countisn't larger than the size of set.

但请确保,这count不大于set.

回答by Chris B

You could use a TreeSetand use it's subSetmethod:

您可以使用TreeSet并使用它的subSet方法:

Returns a view of the portion of this set whose elements range from fromElement to toElement. If fromElement and toElement are equal, the returned set is empty unless fromExclusive and toExclusive are both true. The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The returned set supports all optional set operations that this set supports.

返回此集合中元素范围从 fromElement 到 toElement 的部分的视图。如果 fromElement 和 toElement 相等,则返回的集合为空,除非 fromExclusive 和 toExclusive 都为真。返回的集合受此集合的支持,因此返回的集合中的更改会反映在此集合中,反之亦然。返回的集合支持该集合支持的所有可选集合操作。

EXAMPLE USING INTEGER:

使用整数的示例:

TreeSet<Integer> t = new TreeSet<Integer>();
t.add(1);
t.add(2);
t.add(3);
t.add(4);
t.add(5);

System.out.println("Before SubSet:");

for(Integer s : t){
    System.out.println(s);
}

System.out.println("\nAfter SubSet:");


for(Integer s : t.subSet(2,false,5,true)){
    System.out.println(s);
}

OUTPUT:

输出:

Before SubSet:
1
2
3
4
5

After SubSet:
3
4
5

Alternatively, If you do not know the elements and want to return the elements between two points you can use an ArrayList constructed with the Set and use the subListmethod.

或者,如果您不知道元素并希望返回两点之间的元素,您可以使用由 Set 构造的 ArrayList 并使用subList方法。

System.out.println("\nAfter SubSet:");

t = new TreeSet(new ArrayList(t).subList(2, 5));

for(Integer s : t){
    System.out.println(s);
}

回答by RNJ

What about this

那这个呢

Set<String> s = new HashSet<String>();
// add at least two items to the set 
Set<String> subSet = new HashSet(new ArrayList<String>(s).subList(1, 2));

This would sublist between 1 and 2

这将在 1 和 2 之间进行子列表

回答by Frank Pavageau

Without creating a copy of the Setbeforehand, you can do (using Guava) :

无需Set事先创建副本,您可以执行以下操作(使用 Guava):

Lists.newLinkedList(Iterables.getFirst(Iterables.partition(mySet, count), ImmutableList.of()))

It's a real LinkedListcontaining only (up to) the first countelements, not a view on a larger list.

它是一个真正LinkedList包含(最多)第一个count元素,而不是更大列表上的视图。