1

Hello I'm trying to split next iterator entryset from hashmap but I can't get it to work.

I have an hashmap in which I put two things, first one is sender, second one is channel:

channelList = HashMap()
channelList.put(playername, channelname) #have on mind that those can be changed, depending on what user types in

I have this iterator:

it = channelList.entrySet().iterator()
next = it.next()

But when I print next out it has "=" between arguments from hashmap. For example, if playername is PLAYER and channel name is balkan I get as result: PLAYER=balkan. Question is, how do I get ONLY PLAYERNAME on every next. I tried splitting it like this, but it's not working:

       next = it.next()
       realnext = next.split("=")
       realrealnext = realnext.split("=")[0]

Have on mind that I check for every next using this while loop:

while it.hasNext():

Thanks in advance, Amar!

P.S. I'm jython/python programmer.

Amar Kalabić
  • 888
  • 4
  • 15
  • 33

2 Answers2

1

The problem is you're casting java.util.Map.Entry to a String. Try this instead

#!/usr/bin/jython
import java.util.HashMap

channelList = java.util.HashMap()
channelList.put("Hello", "World")
it = channelList.entrySet().iterator()
while (it.hasNext()):
    e = it.next()
    print("key = " + e.getKey())
    print("value = " + e.getValue())

Which on my system runs as follows -

$ ./test.py
key = Hello
value = World
$
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I tried getting getValue and I get this error: http://pastebin.com/363ivaG8 Line 178 is: nextvalue = it.next().getValue() – Amar Kalabić Nov 22 '13 at 18:17
  • @user2971511 Add a pastebin with that code, and I'll look at it. This example works on my system. – Elliott Frisch Nov 22 '13 at 18:24
  • Did you check `if it.hasNext()` before you called `next = it.next()`? You'll get that error if you don't. – Elliott Frisch Nov 22 '13 at 18:27
  • Your problem is here `nextkey = it.next().getKey() nextvalue = it.next().getValue()`. – Elliott Frisch Nov 22 '13 at 18:32
  • You need to set an object if (and only if) the iterator `it.hasNext()`. – Elliott Frisch Nov 22 '13 at 18:33
  • Note: Every time you call `next()` you **advance the iterator**. – Elliott Frisch Nov 22 '13 at 18:34
  • Hmm, I recoded it like this, but it's not working, maybe I don't get you. Can you recode it and put it on pastebin with #hashcomments like explanations for me please? I'm still learning hashmaps in jython... btw. same error. http://pastebin.com/9AJPTDiJ – Amar Kalabić Nov 22 '13 at 18:38
  • That works, but now I have another problem, I have list of players "players" and now I have to iterate between whole list of players and send them sendMessage method at bottom of code... Because, I can't use sendMessage on whole list, only per one player, but I have to send that message to all players... I'm talking about this part of code(check #hashcomments: http://pastebin.com/yQYvd8rm Thank you very much, you are very helpful! – Amar Kalabić Nov 22 '13 at 19:07
0

You shouldn't name reference to Map ....List, it is confusing. You should name it channelMap.

Next, your Maps should use generic types to set up elements they are using, like for example

Map<String, Channel> channelMap = new HashMap<>();

This way you would be able to safely use

Iterator<Entry<String, Channel>> it = channelMap.entrySet().iterator();

and have access to it.next().getKey() (notice that order of elements in HashMap is based on hashCode if its Key so don't be surprised with order like Player2, Player1, Player 3).


Anyway if you just want to iterate over all keys then maybe

for (String key: channelMap.keySet()){
    System.out.println(key);
}

would be better solution.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • But how do I get whole keyset for specific value? – Amar Kalabić Nov 22 '13 at 18:20
  • @user2971511 there can be many keys with same value. Unfortunately it seems that you will have to iterate over all entries, check if its value is the one you are looking for and add key for such value to predefined set. After you iterate over all entries you can return this set. – Pshemo Nov 22 '13 at 18:47
  • [Here](http://stackoverflow.com/q/46898/1393766) is example of how to iterate over all map entries. – Pshemo Nov 22 '13 at 18:50