The correct way to return the only element from a

2020-02-26 14:49发布

I have the following kind of situation:

Set<Element> set = getSetFromSomewhere();
if (set.size() == 1) {
    // return the only element
} else {
    throw new Exception("Something is not right..");
}

Assuming I cannot change the return type of getSetFromSomewhere(), is there a better or more correct way to return the only element in the set than

  • Iterating over the set and returning immediately
  • Creating a list from the set and calling .get(0)

4条回答
小情绪 Triste *
2楼-- · 2020-02-26 15:19
if(set.size()==1){
   set.toArray(new Element[0])[0];
}
查看更多
爷、活的狠高调
3楼-- · 2020-02-26 15:25

You could grab the iterator:

Element firstEl = set.iterator().next();
查看更多
▲ chillily
4楼-- · 2020-02-26 15:32

The best general solution (where you don't know the actual set class) is:

Element first = set.iterator().next();

If the set class is known to be a SortedSet (e.g. a TreeSet or ConcurrentSkipListSet), then a better solution is:

Element first = ((SortedSet) set).first();

In both cases, an exception will be thrown if the set is empty; check the javadocs. The exception can be avoided using Collection.isEmpty().


The first solution is O(1) in time and space for a HashSet or LinkedHashSet, but typically worse for other kinds of set.

The second one is O(logN) in time , and uses no space for TreeSet or ConcurrentSkipListSet.

The approach of creating a list from the set contents and then calling List.get(0) gives a poor solution since the first step is an O(N) operation, both in time and space.


I failed to notice that N is actually 1. But even so, creating an iterator is likely to be less expensive than creating a temporary list.

查看更多
相关推荐>>
5楼-- · 2020-02-26 15:34

You can use an Iterator to both obtain the only element as well as verify that the collection only contains one element (thereby avoiding the size() call and the unnecessary list creation):

Iterator<Element> iterator = set.iterator();

if (!iterator.hasNext()) {
    throw new RuntimeException("Collection is empty");
}

Element element = iterator.next();

if (iterator.hasNext()) {
    throw new RuntimeException("Collection contains more than one item");
}

return element;

You would typically wrap this up in its own method:

public static <E> E getOnlyElement(Iterable<E> iterable) {
    Iterator<E> iterator = iterable.iterator();

    // The code I mentioned above...
}

Note that this implementation is already part of Google's Guava libraries (which I highly recommend, even if you don't use it for this particular code). More specifically, the method belongs to the Iterables class:

Element element = Iterables.getOnlyElement(set);

If you're curious about how it is implemented, you can look at the Iterators class source code (the methods in Iterables often call methods in Iterators):

  /**
   * Returns the single element contained in {@code iterator}.
   *
   * @throws NoSuchElementException if the iterator is empty
   * @throws IllegalArgumentException if the iterator contains multiple
   *     elements.  The state of the iterator is unspecified.
   */
  public static <T> T getOnlyElement(Iterator<T> iterator) {
    T first = iterator.next();
    if (!iterator.hasNext()) {
      return first;
    }

    StringBuilder sb = new StringBuilder();
    sb.append("expected one element but was: <" + first);
    for (int i = 0; i < 4 && iterator.hasNext(); i++) {
      sb.append(", " + iterator.next());
    }
    if (iterator.hasNext()) {
      sb.append(", ...");
    }
    sb.append('>');

    throw new IllegalArgumentException(sb.toString());
  }
查看更多
登录 后发表回答