cofactor
asked on
data comparision
I am posting sample data from my web application
// These are available role ids of a user. These data are dynamic and retrieved from DB.
String givenroleids ="12,14,18" ;
Now I have another String selectedroleid="2" // This is a role selected from select box . This value can be dynamic .
I want to check whether selectedroleid is in givenroleids . How do I do it ?
in this case result will be false because selectedroleid is not in givenroleids .
but suppose selectedroleid ="12" ,in this case result will be true because selectedroleid is in givenroleids .
// These are available role ids of a user. These data are dynamic and retrieved from DB.
String givenroleids ="12,14,18" ;
Now I have another String selectedroleid="2" // This is a role selected from select box . This value can be dynamic .
I want to check whether selectedroleid is in givenroleids . How do I do it ?
in this case result will be false because selectedroleid is not in givenroleids .
but suppose selectedroleid ="12" ,in this case result will be true because selectedroleid is in givenroleids .
You can use the split function on the string givenroleids, splitting on the ",". That will give you an array of all the givenroleids. Then you can iterate through that array and see if the selectedroleid equals the value ine the array.
ASKER
@jeffry,
That will cost 6/7 lines of code.
I want make with very less code.
any other approach ?
That will cost 6/7 lines of code.
I want make with very less code.
any other approach ?
My recommendation would be to change your initial set from the database. Instead of a string of comma seperated values, when you load your data in from the database, try putting it in a hashset. Then you could just call the contains function
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
// This is a role selected from select box . This value can be dynamic .
Good solution there from Doug.
Just odd why you would want to give a user the choice of choosing something which isn't in the known range in the first place.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Arrays.asList(givenroleids.split("," )).contain s(selected roleid)
All you need is a one liner.
It's one line, but it is one expensive (to compute) line :)
Doug
It's one line, but it is one expensive (to compute) line :)What he said was
That will cost 6/7 lines of code.
I want make with very less code.
I know - that's why I posted my 1-line solution.
I was just pointing out that not all 1-liners are created equal (in processing terms).
Doug
I was just pointing out that not all 1-liners are created equal (in processing terms).
Doug
I was just pointing out that not all 1-liners are created equal (in processing terms).Good point. But, I just thought my code was clearer.
But, I just thought my code was clearer.Yep - that it may be :)
Doug
I would warn against the fewer lines of code is moe efficient line of thinking and more of what the function is doing. A single loop through an array of 3 values is pretty much nothing in today's computing world. Cofactor, I would recommend not worrying too much about 6 lines of code.
A single loop through an array of 3 values is pretty much nothing in today's computing world.I agree that your method is the least expensive(among the three posted). But, I think making code clear and concise is a big factor in my book.
ASKER
Thanks all.
I have gone through all responses.
I have implemented @rrz solution because I find that more clear , concise and its simplicity.
I found @dpearson solution also interesting.
I have gone through all responses.
I have implemented @rrz solution because I find that more clear , concise and its simplicity.
I found @dpearson solution also interesting.
Glad to see you accepted rrz's solution because I was going to suggest it. The problem with ",2," is that the "2" can come at the beginning or end of the string, then you have to deal with ",2" and "2," and still not be sure since the given string could begin with "42," or end with ",27" which would cause extra lines of code anyway.
@awking00 You are wrong. dpearson's solution is perfectly good. He bracketed the given String with commas.
cofactor, you should have given Jeffrey Dake some points. I just built on his idea.
cofactor, you should have given Jeffrey Dake some points. I just built on his idea.