Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

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 .
0
cofactor
Asked:
cofactor
  • 5
  • 4
  • 3
  • +3
2 Solutions
 
Jeffrey Dake Senior Director of TechnologyCommented:
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
 
cofactorAuthor Commented:
@jeffry,

That will cost  6/7 lines of  code.

I want make  with very less code.

any other approach ?
0
 
Jeffrey Dake Senior Director of TechnologyCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
dpearsonCommented:
How about this?

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
 
krakatoaCommented:
// 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
 
rrzCommented:
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));
   }
}

Open in new window

All you need is a one liner.
0
 
dpearsonCommented:

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
 
rrzCommented:
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
 
dpearsonCommented:
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
 
rrzCommented:
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
 
dpearsonCommented:
But, I just thought my code was clearer.
Yep - that it may be :)

Doug
0
 
Jeffrey Dake Senior Director of TechnologyCommented:
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
 
rrzCommented:
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
 
cofactorAuthor Commented:
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
 
awking00Commented:
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
 
rrzCommented:
@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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 5
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now