Best way to get value from Collection by index

JavaCollections

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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?

Solution 2 - Java

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.

Solution 3 - Java

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.

Solution 4 - Java

Solution 5 - Java

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".

Solution 6 - Java

Convert the collection into an array by using function

Object[] toArray(Object[] a) 

Solution 7 - Java

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

Solution 8 - Java

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.

Solution 9 - Java

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

Solution 10 - Java

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).

Solution 11 - Java

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()); 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkeoView Question on Stackoverflow
Solution 1 - JavabutterchickenView Answer on Stackoverflow
Solution 2 - JavaakarnokdView Answer on Stackoverflow
Solution 3 - JavaMatthew FlaschenView Answer on Stackoverflow
Solution 4 - JavaJodaStephenView Answer on Stackoverflow
Solution 5 - JavaAaron DigullaView Answer on Stackoverflow
Solution 6 - JavaBhushan BhangaleView Answer on Stackoverflow
Solution 7 - JavadfaView Answer on Stackoverflow
Solution 8 - JavaLifesAwayView Answer on Stackoverflow
Solution 9 - JavaapksatheeshView Answer on Stackoverflow
Solution 10 - JavagouessejView Answer on Stackoverflow
Solution 11 - JavaSoni KView Answer on Stackoverflow