How to get the nth element of a set

In particular: how to get the nth element of LinkedHashSet (which has a predictable iteration order)? I want to get the nth element inserted in this Set(which has not been present yet).

Is it better to use List:

List<T> list = new ArrayList<T>(mySet);
T value = list.get(x); // x < mySet.size()

or toArray(T [] a):

T [] array = mySet.toArray(new T[mySet.size()]);
T value = array[y]; // y < mySet.size()

Besides the (albeit minor) differences in performance, all that needs to be tracked? Any clear winner?

Change 1

NB: It doesn't matter why I need the last inserted element, all that matters is what I want. The LinkedHashSet was specifically chosen because it "determines the iteration order, which is the order in which the elements were inserted into the set (insertion order). Note that the insertion order does not change if the element is reinserted into the set."

Edit 2

This question seems to have moved on to discuss whether any implementation will be able to Setmaintain the original insertion order. So I put a few simple test codes in http://pastebin.com/KZJ3ETx9 to show that yes, LinkedHashSet really keeps the insertion order (the same as its iteration order) as its Javadok claims.

Edit 3

, Set ( , , , ).

+4
6

, @Juvanis.

n- LinkedHashSet:

Iterator<T> itr = mySet.iterator();
int nth = y;
T value = null;

for(int i = 0; itr.hasNext(); i++) {
    value = itr.next();
    if (i == nth) {
        break;
    }
}

2 :

public class SetUtil {

    @Nullable
    public static <T> T nthElement(Set<T> set, int n) {
        if (null != set && n >= 0 && n < set.size()) {
            int count = 0;
            for (T element : set) {
                if (n == count)
                    return element;
                count++;
            }
        }
        return null;
    }
}

NB: Iterables<T>.

Set List, List ( , ).

, Set , , .

+2

n- , . , , . a Set mySet, nthElement(mySet, mySet.size()-1).

n Set, , , , ArrayList.

  /**
   * Return an element selected by position in iteration order.
   * @param data The source from which an element is to be selected
   * @param n The index of the required element. If it is not in the 
   * range of elements of the iterable, the method returns null.
   * @return The selected element.
   */
  public static final <T> T nthElement(Iterable<T> data, int n){
    int index = 0;
    for(T element : data){
      if(index == n){
        return element;
      }
      index++;
    }
    return null;
  }
+3

LinkedHashSet, :

Iterator<T> it = linkedHashSet.iterator();
T value = null;

while (it.hasNext()) {
    value = it.next();
}

, .

+2

, ModelClass HashSet.

ModelClass m1 = null;
int nth=scanner.nextInt();
for(int index=0;index<hashset1.size();index++){
    m1 = (ModelClass) itr.next();
    if(nth == index) {
        System.out.println(m1);
        break;
    }
}
+1

, . . Set , , , ,

class mySetAndLast extends Set{
   T last;
   Set<T> mySet;       
}

, " ". , .

-> insert x -> ok, x is the last inserted 
-> insert y (y!=x) -> ok: y is the last inserted 
-> insert x -> ? 

x y ? x , y , x , , x .

0

"" Set List:

public class ListSet<E> extends ArrayList<E> implements Set<E> {
    @Override
    public boolean add(E item) {
        return contains(item) ? false : super.add(item);
    }

    // ... and same for add(int, E), addAll(...), etc.
}

(O (n) ), , , contains() .

0

Source: https://habr.com/ru/post/1546395/


All Articles