One liner for getting a sublist from a Set

Posted by yegor256 on Stack Overflow See other posts from Stack Overflow or by yegor256
Published on 2012-09-19T08:25:28Z Indexed on 2012/09/19 9:38 UTC
Read the original article Hit count: 256

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:

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about guava