• Post Reply Bookmark Topic Watch Topic
  • New Topic

Averaging object elements in an ArrayList  RSS feed

 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've created an Array List, and using a List Iterator I'd like to get an average of the employees' pay rate in my Test Class....any suggestions?

Compensation Interface



Employee Class




Test Class







       
 
Carey Brown
Saloon Keeper
Posts: 3324
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get an average, loop through the data and for each item, add the value to a sum and increment a counter. When the loop is done divide the sum by the count.
 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:To get an average, loop through the data and for each item, add the value to a sum and increment a counter. When the loop is done divide the sum by the count.


How do I get it to loop the hourlyPayRate though, as opposed to the taxRate?  Zero in on those specific numbers?
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an area where java 8 makes life easier (well, after a while). Have a look at these articles:

https://docs.oracle.com/javase/tutorial/collections/streams/
http://www.oracle.com/technetwork/articles/java/ma14-java-se-8-streams-2177646.html
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Martos wrote:
Carey Brown wrote:To get an average, loop through the data and for each item, add the value to a sum and increment a counter. When the loop is done divide the sum by the count.


How do I get it to loop the hourlyPayRate though, as opposed to the taxRate?  Zero in on those specific numbers?

Hmmm.  Not sure what you mean.  You loop through the list, getting an Employee object, then you get the hourlyPayRate or whatever data you need from that object.
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP

That Employee class is way too big, it contains such fields which should not be directly allocated for Employee's responsibility to control them, that is why your constructor can't fit to my 27' screen.

Scanning quickly through, half or more than half of all fields in my opinion should go out and make themselves class/'es. i.e. good candidates to consider removing (might not all):

 
Carey Brown
Saloon Keeper
Posts: 3324
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Martos wrote:
Carey Brown wrote:To get an average, loop through the data and for each item, add the value to a sum and increment a counter. When the loop is done divide the sum by the count.


How do I get it to loop the hourlyPayRate though, as opposed to the taxRate?  Zero in on those specific numbers?

Depending on if you use enhanced for() loop vs traditional for() loop.......
employee.getHourlyPayRate()
or
myList1[i].getHourlyPayRate()

This is what getters are for, to access a specific piece of an object.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need a bigger screen if I try to implement PS' suggestion and you want to read the whole post. I agree with PS there. If you have a List of any sort (well, actually a Collection of any sort), it will have a stream() method which returns a Stream<E> where E is the same type as the E in List<E>.
Now that Stream can be used to create a second Stream of a different type with its mapXXX methods. If your Employee class has a getEarnings method returning a double, then you can use the mapToDouble() method. That takes a function returning a double as its parameter. All you need in this instance to do is supply details of the object you have in the Stream (let's call it e for employee) and then supply something on e that returns a double. Then you separate the two with the arrow token -> and that turns it into a λ expression.
Now, when you use mapToDouble, you find it returns a DoubleStream, and that has a method called average(). That does exactly what you want, but returns an OptionalDouble reference. There are several ways to get the value out of that; let's try the orElse method. That will return its argument (here 0.0) if the DoubleStream happens to have no elements, otherwise the average.
 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Daniel Martos wrote:
Carey Brown wrote:To get an average, loop through the data and for each item, add the value to a sum and increment a counter. When the loop is done divide the sum by the count.


How do I get it to loop the hourlyPayRate though, as opposed to the taxRate?  Zero in on those specific numbers?

Depending on if you use enhanced for() loop vs traditional for() loop.......
employee.getHourlyPayRate()
or
myList1[i].getHourlyPayRate()

This is what getters are for, to access a specific piece of an object.


So I would do this?

for(i=0; i <
   
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing you want to do is to make a loop that will get your objects in the list one by one.  The easiest way to do this is with an enhanced for loop.

This code will iterate through myList1, setting employee for each iteration.  Once you have employee you can access the taxRate or the hourlyPayRate with getters.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!