• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashMAp issues  RSS feed

 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • 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
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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?
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • 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: 536
1
  • Mark post as helpful
  • send pies
  • 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.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 536
1
  • Mark post as helpful
  • send pies
  • 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.

 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • 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: 536
1
  • Mark post as helpful
  • send pies
  • 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: 536
1
  • Mark post as helpful
  • send pies
  • 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
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 56529
172
  • Mark post as helpful
  • send pies
  • 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: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hash‑based collections and maps usually have hash in their name.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!