Title: Java: Pick out multiple elements in Linked list
Tags: java;list;linked-list;listiterator
Question: I have a linked list of classes which contain 3 strings and a double.  I want to collect the total value of each double in the list. For example

``` LinkedList<Person&gt; L = new LinkedList<Person&gt;();
 Person p1 = new Person("Fee","Foo","Bar", 1.2);
 Person p2 = new Person("Fi","Fi","Fo", 2.5);
 L.add(p1);
 L.add(p2);
```

I would want to find and add up 1.2, and 2.5.  I'm assuming I should use ListIterator, but how do I tell it to add each found double value to the total?

Comment: To clarify: are you asking how to iterate over a list?

Comment: The list only contains one type: Person.

Comment: No, I know about Iteration, I just want to pick specific elements from a list with different data types.

Here is the accepted answer: You have couple of options to iterate over it

A) Using iterators as asked

```Person person = new Person();
ListIterator<Person&gt; listIterator = L.listIterator();

while (listIterator.hasNext()) {
      person = listIterator.next();
      double value = person.getDoubleAttribute();
}
```

B) Use a ```for-each``` loop as suggested in other answer: 

```for(Person person : L){
    double value = person.getDoubleAttribute();
}
```

PS: is highly discouraged to start ```Java``` variables or attributes by ```UPPERCASE```

Comment for this answer: I know you will be hating me ;), but speaking of memory usage wouldn't it be better to put the `Person person = ...` outside of the loop? Because now it reserves new memory space in every iteration?

Here is another answer: You can iterate over the list, get the double property of each Person and sum them, or you can use Java 8 Streams :

```double sum = L.stream().mapToDouble(Person181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16getDoubleProperty).sum();
```

Where ```getDoubleProperty``` stands for the name of a method in the ```Person``` class returning the double value.


Here is another answer: Just use a for loop over the persons:

```double sum = 0;
for(Person p : L)
    sum += p.getDouble();
System.out.print(sum);
```

Comment for this answer: @KnutKnutsen Use a `for each` loop maybe it's the easiest way but not the optimal. Also OP asked for `Iterator`in `LinkedList`...
Comment for this answer: @KnutKnutsen Internally converted is exactly what means less optimal ;)... Anyway, I'm totally agree with you, i don't like `Iterators`, code is less clear, but they are better  in the memory usage...
Comment for this answer: not really, but sure you can easily find it... BTW: i edited my question to cover all possibilities... ;)
Comment for this answer: I think your approach is the better one, than the accepted answer ;) but skipping of brackets should be avoided due to increased error ratio when extending the loop. See http://stackoverflow.com/questions/8020228/is-it-ok-if-i-omit-curly-braces-in-java
Comment for this answer: @JordiCastilla why not optimal? Sure he asked for an `Iterator`, but as far as I know the for-each loop will be internally converted to a similar approach. Even Oracle embraces the use of for-each, see http://docs.oracle.com/javase/7/docs/technotes/guides/language/foreach.html
Comment for this answer: @JordiCastilla PS: I didn't mean that your answer is wrong, but not so beautiful ;)
Comment for this answer: @JordiCastilla Do you have a source for the memory usage statement? (I'm really interested) I thought the conversion will be done by Compiler so that it should make any difference on runtime ?!
Comment for this answer: Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69282/discussion-between-knutknutsen-and-jordi-castilla).

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:01/21/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><list><linked-list><listiterator>', 'Title': 'Java: Pick out multiple elements in Linked list', 'ViewCount': 721, 'id': 28062900, 'user_AboutMe': None, 'user_AccountId': 4006124.0, 'user_DisplayName': 'user3300735', 'user_Reputation': 73.0, 'user_Views': 6.0, 'user_id': 3300735.0}}