Getting Mean of LinkedList<?>

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 464 Views - Last Post: 29 January 2019 - 05:39 PM Rate Topic: -----

#16 JackEsparrou   User is offline

  • New D.I.C Head

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

Re: Getting Mean of LinkedList<?>

Posted 28 January 2019 - 12:54 PM

View PostNormR, 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 ^^

View PostNormR, 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 ;-;
Was This Post Helpful? 0
  • +
  • -

#17 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3618
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#18 Piet Souris   User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • 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());


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2