Solved

# data comparision

Posted on 2014-08-23
182 Views
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 .
0
Question by:cofactor

LVL 10

Expert Comment

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.
0

Author Comment

@jeffry,

That will cost  6/7 lines of  code.

I want make  with very less code.

any other approach ?
0

LVL 10

Expert Comment

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
0

LVL 26

Assisted Solution

boolean result = ("," + givenroleids + ",").contains("," + selectedroleid + ",") ;

The adding of "," to both the string to match against and the pattern means that "2" becomes ",2," which matches ",2," in the original, but not ",12,".

Doug
0

LVL 16

Expert Comment

// 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.
0

LVL 27

Accepted Solution

I want make  with very less code.
You could use Jeffrey Dake's idea in this way
``````import java.util.*;
public class Test {
public static void main(String args[]){
String givenroleids ="12,14,18";
String selectedroleid = "12";
System.out.print( Arrays.asList(givenroleids.split(",")).contains(selectedroleid));
}
}
``````
All you need is a one liner.
0

LVL 26

Expert Comment

Arrays.asList(givenroleids.split(",")).contains(selectedroleid)
All you need is a one liner.

It's one line, but it is one expensive (to compute) line :)

Doug
0

LVL 27

Expert Comment

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.
0

LVL 26

Expert Comment

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
0

LVL 27

Expert Comment

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.
0

LVL 26

Expert Comment

But, I just thought my code was clearer.
Yep - that it may be :)

Doug
0

LVL 10

Expert Comment

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.
0

LVL 27

Expert Comment

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.
0

Author Comment

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.
0

LVL 31

Expert Comment

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.
0

LVL 27

Expert Comment

@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.
0

## Featured Post

### Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be \$37.1B.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.