0

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> L = new LinkedList<Person>();
 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?

3 Answers3

2

Just use a for loop over the persons:

double sum = 0;
for(Person p : L)
    sum += p.getDouble();
System.out.print(sum);
Niels Billen
  • 2,189
  • 11
  • 12
  • 1
    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 – KnutKnutsen Jan 21 '15 at 09:19
  • @KnutKnutsen Use a `for each` loop maybe it's the easiest way but not the optimal. Also OP asked for `Iterator`in `LinkedList`... – Jordi Castilla Jan 21 '15 at 09:31
  • @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 – KnutKnutsen Jan 21 '15 at 09:37
  • @JordiCastilla PS: I didn't mean that your answer is wrong, but not so beautiful ;) – KnutKnutsen Jan 21 '15 at 09:40
  • @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... – Jordi Castilla Jan 21 '15 at 09:46
  • @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 ?! – KnutKnutsen Jan 21 '15 at 09:49
  • not really, but sure you can easily find it... BTW: i edited my question to cover all possibilities... ;) – Jordi Castilla Jan 21 '15 at 09:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69282/discussion-between-knutknutsen-and-jordi-castilla). – KnutKnutsen Jan 21 '15 at 09:54
2

You have couple of options to iterate over it

A) Using iterators as asked

Person person = new Person();
ListIterator<Person> 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

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • 1
    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? – KnutKnutsen Jan 21 '15 at 09:53
1

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(Person::getDoubleProperty).sum();

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

Eran
  • 387,369
  • 54
  • 702
  • 768