# Looping through java hashmap from ruby through rjb

**URL:** <https://www.ruby-forum.com/t/looping-through-java-hashmap-from-ruby-through-rjb/168785>\
**Category:** Rails\
**Created:** [May 25, 2009, 10:28pm UTC](https://www.ruby-forum.com/t/looping-through-java-hashmap-from-ruby-through-rjb/168785 "2009-05-25T22:28:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [May 25, 2009, 10:28pm UTC](https://www.ruby-forum.com/t/looping-through-java-hashmap-from-ruby-through-rjb/168785/1 "2009-05-25T22:28:26Z")

</div>

Greetings.

I am trying to list all keys in a java.util.HashMap field.

The Java code to do this looks like:

# 

Collection c = hMap.values();

# 

# 

//obtain an Iterator for Collection

# 

Iterator itr = c.iterator();

# 

# 

//iterate through HashMap values iterator

# 

while(itr.hasNext())  
System.out.println(itr.next());

So, I took a stab at the ruby version:

# I have a java hashmap called fields that was created using iText

thusly

reader = pdfreader.new( “pdf\_file.pdf” )  
@form = reader.getAcroFields()  
fields = @form.getFields() # this is a hashmap of all fields on form  
fields.\_classname # entere in irc displays: java.util.HashMap

# So, following the java code above

c = fields.values()  
itr = c.iterator()  
while itr.hasNext()  
p itr.next()  
end

Instead of getting the field names, I get  
#\<#Class:0x2d47d74:0x2c15208\>  
#\<#Class:0x2d47d74:0x2c151a4\>  
#\<#Class:0x2d47d74:0x2c150b4\>

(there are three keys in the hash)

Any ideas?

Thanks,  
Dan

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [May 25, 2009, 11:49pm UTC](https://www.ruby-forum.com/t/looping-through-java-hashmap-from-ruby-through-rjb/168785/2 "2009-05-25T23:49:29Z")

</div>

On May 25, 9:28 pm, Dan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 

Well p (or puts etc) end up calling to\_s on objects, and you’re  
obviously getting Object’s default to\_s. Does rjb have some method to  
convert java strings into ruby strings ?

Fred

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [May 26, 2009, 1:29am UTC](https://www.ruby-forum.com/t/looping-through-java-hashmap-from-ruby-through-rjb/168785/3 "2009-05-26T01:29:26Z")

</div>

Frederick C. wrote:

> On May 25, 9:28ï¿½pm, Dan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > 
> 
> Well p (or puts etc) end up calling to\_s on objects, and you’re  
> obviously getting Object’s default to\_s. Does rjb have some method to  
> convert java strings into ruby strings ?
> 
> Fred

Fred,

Good question.

I did some research and found enough to try using itr.next().to\_string  
and guess what. It worked!

I had tried ruby’s .to\_s and naively thought that rjb was translating it  
to the java equivalent because I wasn’t getting an error.

Thanks-a-bunch for the guidance,  
Dan
