Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Intersection of a string

Posted on 2009-07-12
11
Medium Priority
?
260 Views
Last Modified: 2012-05-07
Is there any method to do intersection on String. Say I have a string:
str = car, apple, car, pencil, orange, apple, cup, coffee

I would like to loop through that string and then retrive only the intersected values. so the result would be:
result = car, apple

Can I do this?? Any idea please.
Thanks!!!
0
Comment
Question by:Juuno
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24834044
That wouldn't be an intersection, that would simply be repeated values
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24834065
Try something like the following:
        List<String> values = Arrays.asList("car, apple, car, pencil, orange, apple, cup, coffee".split("\\s*,\\s*"));
        Stack<String> s = new Stack<String>();
        Set<String> dups = new HashSet<String>();
        Collections.sort(values);
        s.push("");
        for (String value : values) {
            if (value.equals(s.peek())) {
                dups.add(value);
            }
            else {
                s.push(value);
            }
        }
        System.out.println(dups);

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 24834081
String str = "car,apple,car,pencil,orange,apple,cup,coffee";
Set unique = new HashSet(Arrays.asList(str.split(",")));
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Juuno
ID: 24834209
@ objects - thanks. but I want to get repeated values, not to reduce duplicate values. I think my title makes u confuse. Thanks anyway.

@ CEHJ, is there anyway I can also get the values of how many times they are repeated ?

Thanks!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24834240
>>@ CEHJ, is there anyway I can also get the values of how many times they are repeated ?

You can keep a Map and increment the count into it, keyed on the value
0
 

Author Comment

by:Juuno
ID: 24834272
ur code is like: if u find an equal string, then put that value into 'dups', right? so, I tried to change like this:

But it printed out like this: {car=1, apple=1}
String test = "car, apple, car, pencil, orange, apple, cup, coffee";
	       List<String> values = Arrays.asList(test.split("\\s*,\\s*"));
	        Stack<String> s = new Stack<String>();
	        Map<String, Integer> dups = new HashMap<String, Integer>();
	        Collections.sort(values);
	        s.push("");
	        int ctn=0;
	        for (String value : values) {
	            if (value.equals(s.peek())) {
	            	if(dups.get(value) == null) {
				        dups.put(value, new Integer(1));
				    } else {
				    	dups.put(value, new Integer(dups.get(value).intValue() + 1));
				    }
				    }
	            else {
	                s.push(value);
	            }
	        }
	        System.out.println(dups);
	}

Open in new window

0
 

Author Comment

by:Juuno
ID: 24834315
I got it:
I should change the value '1' to '2' in this line:
> dups.put(value, new Integer(1)
> dups.put(value, new Integer(2).

Thanks anyway!!

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24834371
You first of all need to get the count out of the Map if the key exists, and increment it. If the key doesn't exist, you put a count of 1
0
 
LVL 92

Expert Comment

by:objects
ID: 24834405
> @ objects - thanks. but I want to get repeated values, not to reduce duplicate values.

yes saw that. you just need to remove the unique values from the original list.
you will be left with the values that are diped

let me know if you need the code, should only be a couple of lines, you don't need a loop
0
 
LVL 8

Accepted Solution

by:
ozlevanon earned 2000 total points
ID: 24860852
You can also achieve that using Collections.frequency method (not that most efficient code, but shorter).
See snippet below.
String str = "car, apple, car, pencil, orange, apple, cup, coffee";
List<String> list = new ArrayList<String>(Arrays.asList(str.split("\\s*,\\s*")));
Set<String> duplicates = new HashSet<String>();
for (String s : list)
{
	if (Collections.frequency(list, s) > 1)
	{
		duplicates.add(s);
	}
}
System.out.println("duplicates = " + duplicates);

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question