Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Summing values in hash table

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Summing values in hash table

    Hi there;

    Just a simple question which has caused me problems.

    I created hashtable:
    private  Map <String, Double> v = new ConcurrentHashMap<String, Double>();

    In one of my method I would like to sum all the number which are doubles and display it as system.out.print.... Not sure how to get the sum just from the second field for each existing pair.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Summing values in hash table

    Have you looked at the API doc for the Map interface? It has several methods that can be used to get the contents of the Map. What have you tried?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Summing values in hash table

    I have seen a loop what it firstly takes the size and then calculates the sum. But I know there is an easier way using for loop... but could find an example which would help me.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Summing values in hash table

    If you are having problems with your code, post the code and your questions.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Summing values in hash table

    .collect(Collectors.groupingBy(Employee::getDepartment,
    Collectors.summingInt(Employee::getSalary)));
    I was trying to use above example get the sum but I was not able to adapt it so it would work with my map

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Summing values in hash table

    Sorry, I have no idea what that code is doing. Too many definitions are missing.

    What language is that? Java does not have :: syntax
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Summing values in hash table

    Well this is the example I found which might be useful but I wasn't able to adapt it:
    // Compute sum of salaries by department
    Map<Department, Integer> totalByDept = employees.stream()
    .collect(Collectors.groupingBy(Employee::getDepartment,
    Collectors.summingInt(Employee::getSalary)));

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Summing values in hash table

    What language is that? The :: doesn't look like java.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Summing values in hash table

    I found it in the map api for java... neverless I decided to use this function:
    double total = 0;
        	for (Double value : voltages.values()) {
        	    total = total + value;
    Is there a better way or more efficient?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Summing values in hash table

    What makes something more efficient? Does it always give the correct answer?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. trying to build a hash table, getting null pointer exception on search
    By kevingregg in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 19th, 2013, 01:18 PM
  2. [SOLVED] Problem with update table after inserting new values
    By justyStepi in forum JDBC & Databases
    Replies: 4
    Last Post: September 10th, 2012, 03:52 PM
  3. Dictionary using Distributed Hash Table and BST Tree
    By dezett in forum What's Wrong With My Code?
    Replies: 28
    Last Post: June 23rd, 2012, 12:03 PM
  4. hash table probes
    By victorh in forum Collections and Generics
    Replies: 4
    Last Post: November 8th, 2011, 12:33 AM
  5. TAKE & PUT VALUES INTO AN ORACLE TABLE THRU A LOOP
    By jai in forum Algorithms & Recursion
    Replies: 1
    Last Post: September 17th, 2011, 05:39 AM