• Post Reply Bookmark Topic Watch Topic
  • New Topic

confusion about Object vs. ArrayList in MapHash  RSS feed

 
Stan Lederer
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some java code that is not compiling.

I've declared this HashMap:

I populate each element of the HashMap from a text file like this:

Later in the program I want to access the HashMap like this:


The problem I'm having is this:


Line 28 is the line where I declare m to be an Object. I really want m to be of type ArrayList but, for some reason, the code will only compile if m is of type Object.

Can someone please explain why m must be an Object and then, how do I get "for(String st:m)" to work where m should be a list?

Thanks very much.
 
E Armitage
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get the value of the map you just need to use which you can assign to an ArrayList.

gets the first element from the ArrayList.
 
Stan Lederer
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This worked.


Thanks very much.

I have one more question which I'll post separately.
 
E Armitage
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On another note, you should not be writing any Java code in JSPs. JSPs should be for presentation only. All processing should be done in normal Java classes which are called from a servlet.
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan Lederer wrote:
Line 28 is the line where I declare m to be an Object. I really want m to be of type ArrayList but, for some reason, the code will only compile if m is of type Object.

Can someone please explain why m must be an Object and then, how do I get "for(String st:m)" to work where m should be a list?

Thanks very much.


You might want to declare your map with the value type of List<String> rather than ArrayList


Create a new List value and add to it.


Then get your List value from the map, and iterate through it.


Note: Didn't see the other replies until I posted.
 
Stan Lederer
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote:On another note, you should not be writing any Java code in JSPs. JSPs should be for presentation only. All processing should be done in normal Java classes which are called from a servlet.

Yes, I do know this. If it were my code I would not put Java code into JSP. It's legacy code and too painful to clean up.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan Lederer wrote:It's legacy code and too painful to clean up.

I feel your pain! (says he who is dealing with crufty old XSLT web apps )
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!