Best way to get value from Collection by index

前端 未结 10 1501
旧时难觅i
旧时难觅i 2020-12-05 17:08

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

相关标签:
10条回答
  • 2020-12-05 17:25

    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.

    0 讨论(0)
  • 2020-12-05 17:32

    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.

    0 讨论(0)
  • 2020-12-05 17:34

    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);             
    }
    
    0 讨论(0)
  • 2020-12-05 17:35

    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()); 
    
    0 讨论(0)
  • 2020-12-05 17:36

    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?

    0 讨论(0)
  • 2020-12-05 17:36

    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());
    }
    
    0 讨论(0)
提交回复
热议问题