Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8481019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:27:07+00:00 2026-06-10T19:27:07+00:00

I have following two methods: public static double calculateMeanInt(List<Integer> numbers) { double sum =

  • 0

I have following two methods:

public static double calculateMeanInt(List<Integer> numbers) {
    double sum = 0.0;
    for(Integer number : numbers)
        sum += number;
    return sum/numbers.size();
}

public static double calculateMeanDouble(List<Double> numbers) {
    double sum = 0.0;
    for(Double number : numbers)
        sum += number;
    return sum/numbers.size();
}

Do you have an elegant solution (other than using type casting and Object) that will avoid the duplicate code above and will use a single method name?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:27:09+00:00Added an answer on June 10, 2026 at 7:27 pm

    Each numeric type in Java extends from Number, so you can use the bounded type parameter (thanks Paul) to average all your number types in one method:

      public static <N extends Number> double calculateMean(List<N> numbers) {
        double sum = 0.0;
        for (N number : numbers)
          sum += number.doubleValue();
        return sum / numbers.size();
      }
    

    e.G. like that:

    double intMean = calculateMean(Lists.newArrayList(1,2,3,4,5));
    double doubleMean = calculateMean(Lists.newArrayList(1d,2d,3d,4d,5d));
    double longMean = calculateMean(Lists.newArrayList(1l,2l,3l,4l,5l));
    

    Note that Lists is part of Guava.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method requiring the following: public static List<ParetoElement> ParetoBuildBySum(List<KeyValuePair<string, double>> inputData) I
I have two Arrays that are initialized as such: public static double[] arrayweight= new
I have the following two methods that (as you can see) are similar in
I have the following two methods for getting data from my database: - (void)
I have the following two queries. SELECT account_name,SUM(amount) AS AMOUNT1 FROM entries LEFT JOIN
When I have to write methods which return two values , I usually go
Say i have the following two classes: public class User { public int ID
I have the following two methods on an interface for my service layer: ICollection<T>
If I have two yield return methods with the same signature, the compiler does
In the following class, the return type of the two methods is inconsistent with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.