• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calculate sum of elements

 
Ranch Hand
Posts: 138
1
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can anyone tell me how to do sum, using java8

(A,3), (B,4), (C,6)

For this, the sum should be 3+4+6=13

Is it possible, using Java8.
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are these items in a collection of some sort?
Here is a small program to do this which uses an enhanced for and an array:
MyClass.java contents:

There are many different ways that this could be done, including using streams.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you shou‍ld start by defining what sum means. It would appear to mean the transverse sum of the range of that relation, or the total of the right halves of each pair. Is that right?
How are you storing those pairs? Are they in a List, or are they “K” and  “V” in a Map?

Let's guess you have a Pair<Character, Integer> and you are putting it into a List.so far, so good. Now work out what you are going to get as the return value of this call:-
pairs.stream();

Alternatively, run this code on an IDE; if you use Eclipse and hover your mouse over the name of the method “stream”, a little popup will appear with its return type on.

Once you have got that working, look in the Java™ Tutorials about Lists and about nested classes (especially λs) and Stream's methods to see what you might be able to do. Hint: you want to get ints because you can summate them. Tell us how far you have got and I shall be out of circulation for some time, so somebody else will probably help you.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This Java™ Tutorials section will be more useful than that about Lists I showed you earlier.
 
Atul More
Ranch Hand
Posts: 138
1
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Below is the code which I have written to get the sum.
No doubt improvement is required but at primary stage it is showing me result.

 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Atul More wrote:. . .

Nonononononononono. You might have a total, but don't do that with Strings. That is a non‑object‑oriented solution and the code is horribly clunky. If you insist on having Strings, you need to turn the String into a Pair object with its Pair(String) constructor. Here is a suggestion. Note I have added a rather unusual factory method.Your method of checking whether the right half is an int is badly named because it will reject valid numbers like 123.45. It is also not a good way to write it; look here for what I think is a better test for an int.
 
New rule: no elephants at the chess tournament. Tiny ads are still okay.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic