aspose file tools
The moose likes Beginning Java and the fly likes Map instantiate with key-value Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Map instantiate with key-value" Watch "Map instantiate with key-value" New topic
Author

Map instantiate with key-value

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
How can I do this in one line?



Something like:

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14672
    
  11

Unless I'm missing something obvious, the API does not allow you to do it. Is it bothering you to put it in different lines ?


[My Blog]
All roads lead to JavaRanch
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 472

Originally posted by Christophe Verre:
Unless I'm missing something obvious, the API does not allow you to do it. Is it bothering you to put it in different lines ?


Hi , i think Christophe is right API doesn't allow this check this for more deatils


Thanks,
Sherif
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

You can create an anonymous subclass with an initializer block:

I wouldn't do that though, since a) it's confusing, and b) it's longer than just calling the put methods.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14672
    
  11

I wouldn't call Rob's trick to be one line long
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
okay thanks.

It does not matter, I was just wondering, if it would be possible with anonymous declarations or the like.

thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Originally posted by Christophe Verre:
I wouldn't call Rob's trick to be one line long

But it can be; just remove all the enters

This is actually similar to how .NET 3.0 and up handle object initialization; the inner bracket pair is removed but the rest is the same.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Map instantiate with key-value
 
Similar Threads
IllegalArgumentException: more than 10 parameters
jaxb hashmap?
if isEmpty or null
HashMap Construction with initial values
JSP variable into javascript