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 9126405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:02:04+00:00 2026-06-17T07:02:04+00:00

Say we got 2 ordered collections of numbers. We want to compute the arithmetic

  • 0

Say we got 2 ordered collections of numbers. We want to compute the arithmetic difference element by element.
I think we need to use List of Number in order to model the idea of ‘ordered collection of numbers’.
The problem is that the arithmetic difference (a banal ‘-‘ like in 3-2) is not defined for Number.
I can cast everything to Double, but I’d rather prefer a clean solution.

public static <E extends Number> List<E> listDifferenceElementByElement(List<E> minuend, List<E> subtrahend) throws Exception {
    if (minuend.size() != subtrahend.size()) {
        throw new Exception("Collections must have the same size"); //TODO: better exception handling
    }
    List<E> difference = new ArrayList<E>();
    int i = 0;
    for (E currMinuend : minuend) {
        difference.add(currMinuend-subtrahend.get(i)); //error: The operator - is undefined for the argument type(s) E, E
        i++;
    }
    return difference;
}

Any idea?

  • 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-17T07:02:05+00:00Added an answer on June 17, 2026 at 7:02 am

    Since Number does not provide any subtract method, you can’t do it simply. The alternatives I can think of are:

    1. use the provided doubleValue() method, and return a double, but you could lose precision if the lists contain BigDecimal for example.
    2. have one method per available Number subclass, but it could create a lot of duplicated code…
    3. restrict the type to whatever makes sense (say double if precision does not matter too much or BigDecimal if it does), which is a subcase of (2).

    Option 2 could be implemented like by replacing difference.add(currMinuend-subtrahend.get(i)); by difference.add(subtract(currMinuend, subtrahend.get(i)));

    Then it’s only a matter of writing 10 subtract methods:

    private static int subtract(int a, int b) { return a - b; }
    private static double subtract(double a, double b) { return a - b; }
    private static BigDecimal subtract(BigDecimal a, BigDecimal b) { return a.subtract(b); }
    

    etc.

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

Sidebar

Related Questions

I've got a List of objects - let's say they're Orders. Order OrderID Date
I've got an ordered list of thumbnail images. I'm trying to figure out how
Say I got a set of 10 random numbers between 0 and 100. An
Say I got a HTML form like below and want to pass the values
I've got a situation where I want to use an associative container, and I
Let´s say I´ve got the following xml string: <Item Name=\$Resources:myresource,somestring;\</Item> Now I want to
Let's say we got 'Car' bean with car color and we want to display
lets say i got combobox populated with items (items are some numbers for this
Say I got some lists and some tasks. Each list can have many tasks,
Let's say i got a list looking like this: <ul style=width:980px;> <li style=display:block; float:left;>Lorem

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.