• 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

HashMAp issues

 
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I wrote my HashMAp code as below



i am getting error as below


Multiple markers at this line
- s cannot be resolved to a
variable
- Entry cannot be resolved
to a type


What it means and how to resolve it. Can not i define HashMAp in generica way assuming both key and value as object instance. please advise
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From where did you get the code to loop (iterate) over the map?
Where is s defined? Is Entry a class you implemented or imported?
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the difference between your current code and the bit commented out.
They don't declare the 'entry' variable in the same way...and the commented out one doesn't use an 's' variable.
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i did not understand meaning of below three lines. please advise

HashMap<Dog, Integer> hashMap = new HashMap<Dog, Integer>(); //defining hashmap collection which has dog as value and integer as key right?


for (Map.Entry<Dog, Integer> entry : hashMap.entrySet()) {//why Map used at beginning and hashMap at the end also why Entry at beginning and entrySet at the end
System.out.println(entry.getKey().toString() + " - " + entry.getValue());//why using entry but not entrySet.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would help if you went back to the JavaDocs for HashMap (←click that link) to see exactly which is the key and which is the value. Then understand that the enhanced for-loop requires a collection or array on the right side of the colon. You currently specify the nonexistent variable s there which obviously doesn't work. When the compiler says it can't resolve a name, that means it can't find where you declared it in the current scope. In your code, what is the collection that you want to iterate over with the for loop?
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




above worked fine and got output

4
white dog22 - 20
white dog22 - 5
black dog22 - 15
red dog22 - 10

How to get get sorted output on key

like below

4

black dog22 - 15
red dog22 - 10
white dog22 - 20
white dog22 - 5

how to get sorted output on value like below

4
white dog22 - 5
red dog22 - 10
black dog22 - 15
white dog22 - 20

How to avoid duplicate key entry of white and get output like


4
white dog22 - 5
red dog22 - 10
black dog22 - 15

please advise.

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A hash‑based collection only works normally if you correctly override the equals and hashCode methods. You appear not to have overridden either so your white Dog objects are considered different from each other.
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




after implementing hashCode() and equals() methods i got output as below without duplicate key.

3
10=red dog22
15=black dog22
20=green dog22
red
black
green
10
15
20


How to sort on the key.

Also what are different hash based collections.

which are collections which are not Hash based

Please advise
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Above code gives below error
Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (ArrayList<Dog>). The
inferred type Dog is not a valid substitute for the bounded parameter <T extends Comparable<? super T>>

please advise
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:Above code gives below error
Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (ArrayList<Dog>). The
inferred type Dog is not a valid substitute for the bounded parameter <T extends Comparable<? super T>>


Dog does not implement the Comparable interface. You could either modify Dog to implement Comparable, or you can create a new class that implements Comparator and pass an instance of that to the sort() method.

Edit: In other words, sort needs to know "how" you want the objects sorted.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree: a tree map needs to take comparable objects or some object which does the comparisons, because the objects need to be compared to determine the locations they occupy in the tree.
Non‑hash‑based collections include tree maps, array lists, array deques, linked lists and many others. You are confusing yourself by calling a tree map hashMap in line 22.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hash‑based collections and maps usually have hash in their name.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic