• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Largest Value in an array in a hashmap

 
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so i have a hashmap with a String key and array values, how would i search through the first indexes of each array to find the largest number? I tried but this hasnt worked with arrays.
 
Saloon Keeper
Posts: 8220
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see where you are getting the zero'ith index into the array.
 
Gurtej Grewal
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is just an example from google of finding the max integer, i didnt do anything to it, only reason i added it was to say this method wont work, what else can i do
 
Saloon Keeper
Posts: 4497
166
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gives you a Collection of int[], and you cannot take the max of that.
You could use:
Then follow Careys remark.
 
Carey Brown
Saloon Keeper
Posts: 8220
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you'll need two loops, the one you have on line 2 and another one just like it. The first loop's job is to find the max value of array at index zero. The next looks again through the values and if one is the same as max then print the key.
 
Gurtej Grewal
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So from what i understand im calling
for (Map.Entry<String, int[]> entry : map.entrySet())
and within this loop im finding the max, the im using the loop again
for (Map.Entry<String, int[]> entry1 : map.entrySet())
and if the values of this loop equal the value of max i print the key?
 
Carey Brown
Saloon Keeper
Posts: 8220
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Piet has a better suggestion for loop 1.
 
Gurtej Grewal
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im told the part checking if max is equal to the number found is always false and so nothing happens when the method is run, how should i fix this loop?
 
Piet Souris
Saloon Keeper
Posts: 4497
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your entry.getValue() is an int[], not an Int.  Have a look at Carey reply.

Edit: is your outer loop necessary?
 
Gurtej Grewal
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe this should work now
 
Marshal
Posts: 72913
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gurtej Grewal wrote:This is just an example from google . . .

Beware of simply searching for an answer. You don't know whether what you find will be really good or really bad. You also won't learn anything from it. Please also tell us where you found it, so we can evaluate the original.
As I said earlier, you are making things difficult for yourself by not having a Player class.
Where are you going for your course? Will the teach you proper object oriented programming? Please consider whether you are learning anything useful from your searching or whether you should wait until you start your course to do any programming. I can see some bad habits you are getting into already, I am afraid:-
  • 1: Do you know how methods like getAsInt() and Collections.max() work? When you are experienced you might never write that sort of code, but you should consider writing the code out as far as possible at an early stage in your career.
  • 2: Your indentation isn't consistent. If you put that method into a class, you are risking unmatched {...} or even worse mismatched {...}. The most difficult errors to correct are where your code runs readily and silently produces the wrong result, and you can get that with mismatched {...}.
  • 3: “Naming Things” (Carey Brown). Your method doesn't “get” anything; it prints it. A getXXX() method should return something. It also has a plural name but only prints one result. You are going to have to think again about what to do if there are two players with the same (max) score
  •  
    Gurtej Grewal
    Greenhorn
    Posts: 27
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This is where i found the original: https://www.codegrepper.com/code-examples/java/finding+the+max+value+in+hashmap

    I know theres a lot here that i dont understand, normally i wouldnt get into anything new like hashmaps right now at all, but because of school we are just rushed into alot. Im hoping to go through MOOC when schools over and go through everything carefully instead of learning by just implementing these things into my code.

    1.I know the JavaDoc is good place to learn different methods and more but due to all the information it seems hard to find what im looking for, whats the best way to navigate sites like the javaDoc?

    2. What do you mean by my indentation isnt consistent? From what i see in my code i have anything thats surrounded by either a class, method, if statement, for loop, etc indented to show that it is inside something else, is this incorrect?

    3.Thanks i often seem to forget that 'get' in naming is something different in java compared to regular english. Ill keep this in mind from now on.

     
    Piet Souris
    Saloon Keeper
    Posts: 4497
    166
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    That code worked because the Map was a Map<Integer, Integer>, while your Map is a Map<String, int[]>.
     
    Gurtej Grewal
    Greenhorn
    Posts: 27
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    yea thats why i didnt want to use the exact same thing but wanted to change it to my needs.
     
    Carey Brown
    Saloon Keeper
    Posts: 8220
    71
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Indentation and white space. Minor changes.

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

    Piet Souris wrote:That code worked . . .

    Only just. I think it is iterating the key set twice and you should be able to find the maxima along with they keys in one pass through the key set.
     
    Piet Souris
    Saloon Keeper
    Posts: 4497
    166
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Not just, it works. But you are right, it can be done in one go.
     
    Campbell Ritchie
    Marshal
    Posts: 72913
    330
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    ...And I think I know how you can cope with two players with the same (maximum) score. All in the one pass.
     
    Carey Brown
    Saloon Keeper
    Posts: 8220
    71
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Example without using Streams.

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

    Carey Brown wrote:Example without using Streams. . . .

    That looks exactly like what I was thinking of.
     
    I claim this furniture in the name of The Ottoman Empire! You can keep this tiny ad:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic