56

What is the best way to get value from java.util.Collection by index?

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
keo
  • 561
  • 1
  • 4
  • 4

11 Answers11

59

You shouldn't. a Collection avoids talking about indexes specifically because it might not make sense for the specific collection. For example, a List implies some form of ordering, but a Set does not.

Collection<String> myCollection = new HashSet<String>();
myCollection.add("Hello");
myCollection.add("World");

for (String elem : myCollection) {
    System.out.println("elem = " + elem);
}

System.out.println("myCollection.toArray()[0] = " + myCollection.toArray()[0]);

gives me:

elem = World
elem = Hello
myCollection.toArray()[0] = World

whilst:

myCollection = new ArrayList<String>();
myCollection.add("Hello");
myCollection.add("World");

for (String elem : myCollection) {
    System.out.println("elem = " + elem);
}

System.out.println("myCollection.toArray()[0] = " + myCollection.toArray()[0]);

gives me:

elem = Hello
elem = World
myCollection.toArray()[0] = Hello

Why do you want to do this? Could you not just iterate over the collection?

smn.tino
  • 2,272
  • 4
  • 32
  • 41
butterchicken
  • 13,583
  • 2
  • 33
  • 43
  • Hi @butterchicken, my case is that I use a linkedhashmap where I get the keyset (which is a Set). I found this post: http://stackoverflow.com/questions/2923856/is-the-order-guaranteed-for-the-return-of-keys-and-values-from-a-linkedhashmap-o that the order within this Set is guaranteed. Should I still not itarate over it? – jobbert Jan 27 '16 at 11:43
  • 1
    @jobbert You can absolutely iterate over it. You get a `Set` back in this case because that is the contract of the interface of `Map`. – butterchicken Feb 04 '16 at 09:00
27

I agree with Matthew Flaschen's answer and just wanted to show examples of the options for the case you cannot switch to List (because a library returns you a Collection):

List list = new ArrayList(theCollection);
list.get(5);

Or

Object[] list2 = theCollection.toArray();
doSomethingWith(list[2]);

If you know what generics is I can provide samples for that too.

Edit: It's another question what the intent and semantics of the original collection is.

akarnokd
  • 69,132
  • 14
  • 157
  • 192
  • 1
    If the library function returns a Collection, then I don't think you have any guarantees about the ordering of elements inside that collection. Presumably the library call returns a Collection precisely because it doesn't want you to make these kinds of assumptions? – butterchicken Jun 26 '09 at 08:46
  • Good point. The OP did not share enough information about what kind of library he is using. Some libraries return Collection just because the developer read about a best practice to return some super interface instead of the concrete implementation. The library could just return Iterable if there was never an intend to allow anything else but looping the result (like in AXIOM). – akarnokd Jun 26 '09 at 09:10
  • @akarnokd the question about the Collection interface. There is no method "get(idx)" – Meow Meow Feb 22 '22 at 10:28
19

In general, there is no good way, as Collections are not guaranteed to have fixed indices. Yes, you can iterate through them, which is how toArray (and other functions) work. But the iteration order isn't necessarily fixed, and if you're trying to index into a general Collection, you're probably doing something wrong. It would make more sense to index into a List.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
11

I agree that this is generally a bad idea. However, Commons Collections had a nice routine for getting the value by index if you really need to:

CollectionUtils.get(collection, index)

LEQADA
  • 1,913
  • 3
  • 22
  • 41
JodaStephen
  • 60,927
  • 15
  • 95
  • 117
6

You must either wrap your collection in a list (new ArrayList(c)) or use c.toArray() since Collections have no notion of "index" or "order".

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
3

Convert the collection into an array by using function

Object[] toArray(Object[] a) 
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Bhushan Bhangale
  • 10,921
  • 5
  • 43
  • 71
1

you definitively want a List:

The List interface provides four methods for positional (indexed) access to list elements. Lists (like Java arrays) are zero based.

Also

Note that these operations may execute in time proportional to the index value for some implementations (the LinkedList class, for example). Thus, iterating over the elements in a > list is typically preferable to indexing through it if the caller does not know the implementation.

If you need the index in order to modify your collection you should note that List provides a special ListIterator that allow you to get the index:

List<String> names = Arrays.asList("Davide", "Francesco", "Angelocola");
ListIterator<String> i = names.listIterator();

while (i.hasNext()) {
    System.out.format("[%d] %s\n", i.nextIndex(), i.next());
}
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
dfa
  • 114,442
  • 31
  • 189
  • 228
1

It would be just as convenient to simply convert your collection into a list whenever it updates. But if you are initializing, this will suffice:

for(String i : collectionlist){
    arraylist.add(i);
    whateverIntID = arraylist.indexOf(i);
}

Be open-minded.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
LifesAway
  • 159
  • 1
  • 5
0

use for each loop...

ArrayList<Character> al = new ArrayList<>();    
String input="hello";

for (int i = 0; i < input.length(); i++){
    al.add(input.charAt(i));
}

for (Character ch : al) {               
    System.Out.println(ch);             
}
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
0

If your Collection is a List, simply cast it as a List and call get(final int index). Otherwise, it might make sense to consider finding the nth element in an ordered set, for example if it's a LinkedHashSet respecting insertion order (keep in mind that it's possible to create such an instance not respecting insertion order), you can use Collection.stream().skip(index).limit(1).findFirst().orElse(null).

gouessej
  • 3,640
  • 3
  • 33
  • 67
-1

You can get the value from collection using for-each loop or using iterator interface. For a Collection c for (<ElementType> elem: c) System.out.println(elem); or Using Iterator Interface

 Iterator it = c.iterator(); 
        while (it.hasNext()) 
        System.out.println(it.next()); 
Soni K
  • 156
  • 2
  • 6
  • 19