If construct in hashmap.put call

I have a variable of type Hashmap<String,Integer>.

In this, the Integer value might have to go some manipulation depending upon the value of a flag variable. I did it like this...

Hashmapvariable.put( somestring,
    if (flag_variable) {
     //manipulation code goes here
     new Integer(manipulated value);
    } else {
     new Integer(non-manipulated value);
    }
);

But I get an error:

Syntax error on token(s), misplaced constructs.

at the Hashmapvariable.put call.

I also get another error

Syntax error on token ")", delete this token.

at the final ");" line. But I can't delete the ")" - its the closing parentheses for the put method call.

I don't get this. What mistake am I doing?

Answers


You cannot place a statement in the method call.

However, one option could be to make an method that returns a Integer such as:

private Integer getIntegerDependingOnFlag(boolean flag)
{
    if (flag)
        return new Integer(MANIPULATED_VALUE);
    else
        return new Integer(NON-MANIPULATED_VALUE);
}

Then, you can make a call like this:

hashmap.put(someString, getIntegerDependingOnFlag(flag));

 new Integer(flag_variable ? manipulated value : non-manipulated value)

Does the trick

Edit: On Java 5, I suppose you can also write

hashmap.put(someString, flag_variable ? manipulated value : non-manipulated value)

due to auto-boxing.


Need Your Help

Can writing to an output ksds be done if we use alternate key concept with dynamic access mode in a input ksds?

cobol mainframe jcl vsam

I have an input KSDS file, i am using emp-id as primary key and emp-dept as alternate key, with access mode as dynamic. I am reading the file using dynamic access base upon the alternate key, in ru...

iPhone 4 high res screen + high res Google maps custom icon?

google-maps jquery-mobile iphone-4

I'm using BG images twice the size of my element and using background-size attribute to take advantage of the high res iPhone 4 screen. I want to do this for the custom icon I am using in Google ma...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.