Get Object at Index in Set<T>

Get object at index in Set T

Swift 3 and newer

You can offsetBy: from .startIndex:

let mySet: Set = ["a", "b", "c", "d"]
mySet[mySet.index(mySet.startIndex, offsetBy: 2)] // -> something from the set.

Swift 2 (obsolete)

You can advancedBy() from .startIndex:

let mySet: Set = ["a", "b", "c", "d"]
mySet[mySet.startIndex.advancedBy(2)] // -> something from the set.

Swift 1.x (obsolete)

Similar to String, you have to advance() from .startIndex:

let mySet: Set = ["a", "b", "c", "d"]
mySet[advance(mySet.startIndex, 2)] // -> something from the set.

How to get index of an item in java.util.Set

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

Element at index in a std::set?

It doesn't cause a crash, it just doesn't compile. set doesn't have access by index.

You can get the nth element like this:

std::set<int>::iterator it = my_set.begin();
std::advance(it, n);
int x = *it;

Assuming my_set.size() > n, of course. You should be aware that this operation takes time approximately proportional to n. In C++11 there's a nicer way of writing it:

int x = *std::next(my_set.begin(), n);

Again, you have to know that n is in bounds first.

How to get index of element in Set object

A set is just an unordered collection of unique elements. So, an element is either in a set or it isn't. This means that no element in a set has an index.

Consider the set {1, 2, 3}. The set contains 3 elements: 1, 2, and 3. There's no concept of indices or order here; the set just contains those 3 values.

So, if data[key] in itemList returns True, then data[key] is an element of the itemList set, but there's no index that you can obtain.

How to get object at specific index in Persistent Set - Grails

Sets can't be indexed, they are unordered. If you need to index a collection then declare it as a list:

To keep objects in the order which they were added and to be able to reference them by index like an array you can define your collection type as a List:

class Author {

List books

static hasMany = [books: Book]
}

with the understanding that the associated table needs a column to use as an index. Otherwise there's no way to preserve an ordering. You can use indexColumn to specify the column to use:

By default when mapping an indexed collection such as a Map or List the index is stored in a column called association_name_idx which is an integer type in the case of lists and a String in the case of maps. You can alter how the index column is mapped using the indexColumn argument:

static mapping = {
matrix indexColumn: [name: "the_matrix", type: Integer]
}

http://grails.github.io/grails-doc/2.3.x/ref/Database%20Mapping/indexColumn.html

If you don't declare the instance variable with a type then GORM defaults the type to Set.

Why doesn't java.util.Set have get(int index)?

Because sets have no ordering. Some implementations do (particularly those implementing the java.util.SortedSet interface), but that is not a general property of sets.

If you're trying to use sets this way, you should consider using a list instead.

Getting an element from a Set

There would be no point of getting the element if it is equal. A Map is better suited for this usecase.


If you still want to find the element you have no other option but to use the iterator:

public static void main(String[] args) {

Set<Foo> set = new HashSet<Foo>();
set.add(new Foo("Hello"));

for (Iterator<Foo> it = set.iterator(); it.hasNext(); ) {
Foo f = it.next();
if (f.equals(new Foo("Hello")))
System.out.println("foo found");
}
}

static class Foo {
String string;
Foo(String string) {
this.string = string;
}
@Override
public int hashCode() {
return string.hashCode();
}
@Override
public boolean equals(Object obj) {
return string.equals(((Foo) obj).string);
}
}

How to get the first element of the List or Set?

Collection c;

Iterator iter = c.iterator();

Object first = iter.next();

(This is the closest you'll get to having the "first" element of a Set. You should realize that it has absolutely no meaning for most implementations of Set. This may have meaning for LinkedHashSet and TreeSet, but not for HashSet.)



Related Topics



Leave a reply



Submit