• (2 Pages)
• 1
• 2

## 17 Replies - 464 Views - Last Post: 29 January 2019 - 05:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=414646&amp;s=bf2ccd125b3fae12f9bedecbfaf417e6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #16 JackEsparrou

Reputation: 0
• Posts: 10
• Joined: 28-January 19

## Re: Getting Mean of LinkedList<?>

Posted 28 January 2019 - 12:54 PM

NormR, on 28 January 2019 - 12:44 PM, said:

That is also ugly code. Did you look at the API doc for the Numbers class?

I know it is ugly xD I think this is a more elegant if supposing i Will only use Integers and Doubles:

```	public Number getMean(LinkedList<Number> dataList) {

Number summatory = 0;
Iterator<Number> iterator = dataList.iterator();
while(iterator.hasNext()) {
Number number = iterator.next();
summatory=sum(summatory,number);
}
return divide(summatory,dataList.size());

}

@SuppressWarnings("unchecked")
public static <N extends Number> N sum(N number1, N number2) {
Class<? extends Number> cls1 = number1.getClass();
Class<? extends Number> cls2 = number2.getClass();
if (cls1 == Integer.class && cls2 == Integer.class) {
return (N) Integer.valueOf(number1.intValue()+number2.intValue());
}
if (cls1 == Double.class && cls2 == Double.class) {
return (N) Double.valueOf(number1.doubleValue()+number2.doubleValue());
}
if (cls1 == Integer.class && cls2 == Double.class) {
return (N) Double.valueOf(number1.intValue()+number2.doubleValue());
}
if (cls1 == Double.class && cls2 == Integer.class) {
return (N) Double.valueOf(number1.doubleValue()+number2.intValue());
}
throw new UnsupportedOperationException("unknown class: ");
}

@SuppressWarnings("unchecked")
public static <N extends Number> N divide(N number1, N number2) {
return (N) Double.valueOf(number1.doubleValue()/number2.doubleValue());
}

```

By the way, thank you for trying to help me, I really appreciate it

NormR, on 28 January 2019 - 12:44 PM, said:

That is also ugly code. Did you look at the API doc for the Numbers class?

And yes, I've been watching that API doc for more than 2 hours to figure out this ;-;

### #17 g00se

• D.I.C Lover

Reputation: 3618
• Posts: 16,597
• Joined: 20-September 08

## Re: Getting Mean of LinkedList<?>

Posted 29 January 2019 - 03:39 AM

Why does a method returning a numeric mean have to return anything other than double (maybe BigDecimal in extremis)?

This post has been edited by g00se: 29 January 2019 - 03:40 AM
Reason for edit:: Clarification

### #18 Piet Souris

Reputation: 44
• Posts: 198
• Joined: 01-December 15

## Re: Getting Mean of LinkedList<?>

Posted 29 January 2019 - 05:39 PM

Agree with Goose. If it is not a problem to return a double, even if the List is a List of BigIntegers, you can simplify the method to just one line of (perhaps a little hard to read) code:
```public static <N extends Collection<? extends Number>> DoubleSummaryStatistics statistics(N list) {
return list.stream().mapToDouble(x -> x.doubleValue()).summaryStatistics();
}
```

You can use it like this, for instance:
```List<Integer> ints = List.of(1, 2, 3, 4);
Set<Long> longs = Set.of(1L, 2L, 3L, 4L);
Set<Float> floats = Set.of(1f, 2f, 3f, 4f);
List<BigInteger> bigs = List.of(BigInteger.ONE, BigInteger.TWO, BigInteger.TEN);

var ssInts = statistics(ints);
var ssLongs = statistics(longs);
var ssFloats = statistics(floats);
var ssBigs = statistics(bigs);

System.out.format("mean ints: %.3f %n", ssInts.getAverage());
System.out.format("mean longs: %.3f %n", ssLongs.getAverage());
System.out.format("mean floats: %.3f %n", ssFloats.getAverage());
System.out.format("mean bigs: %.3f %n", ssBigs.getAverage());

```