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

The Archive Base Latest Questions

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

I have 2 arrays String key[] , long value[] . keys can be duplicated

  • 0

I have 2 arrays String key[] , long value[] . keys can be duplicated . I need unique keys and their sum of the values in a map. Below is the code I am trying but i get Array out of bound exception

public HashMap < String, Long > getValueMap(String key[], long value[]) {
    Map newmap = new HashMap();
    long count = 0;
    for (int i = 0; i < value.length; i++) {
        for (int j = 0; j < value.length; j++) {
            if (key[i] == key[j]) {
                count = count + value[j];
            }
        }
        newmap.put(key[i], count);

        count = 0;
    }
    return (HashMap < String, Long > ) newmap;

}


long[] value = {};
String key[] = {};
int i = 0;
        if(!worklogs.isEmpty()){
            for(Worklog wl:worklogs){
                value[i]=wl.getValue();
                key[i]=wl.getAuthor();i++;

            }
        }

HashMap < String, Long > newhm = getValueMap(key, value);
Iterator entries = newhm.entrySet().iterator();
while (entries.hasNext()) {
    Map.Entry entry = (Map.Entry) entries.next();
    String newkey = (String) entry.getKey();
    Long newvalue = (Long) entry.getValue();
    System.out.println("Key = " + newkey + ", Value = " + newvalue);
}

The above code is modified to make it look simple. Please can someone help ?

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

    This may be caused because arrays value and key haven’t been initialized before trying to add values to them.

    long[] value= new long[10];

    String key[]= new String[10];

    Why do you need to use arrays instead of Lists? Because if you need the length to ve variable I think it would be a more comfortable approach.

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

Sidebar

Related Questions

I have multiple arrays with string values and I want to compare them and
I have to convert a long string of data into values so that I
As long as I have key-value pairs unmarshalling is pretty straight forward but how
Let's say I have these two arrays: string[] arr1 = new string[2]{Hello, Stack} string[]
I have 4 string arrays,each of these array's length is 4. I also have
I have two String arrays a,b. String a [] = {one,two,three}; String b []
I have some String[] arrays, for example: ['a1', 'a2'] ['b1', 'b2', 'b3', 'b4'] ['c1']
I have 3 String arrays with constants. eg: String[] digit = {one, two, three};
I have 2 string arrays: A1: {aa,bb,cc,dd,ee} A2: {cc,dd,ee,bla,blu} how do I count the
In Javascript, I have 2 arrays of 15 string elements, each of 0 to

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.