Solved

Intersection of a string

Posted on 2009-07-12
11
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
11 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 500 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet filter example 37 85
ejb stateless example 2 44
Why doesn't this text field show up on my Applet frame? 2 28
passing enum to a method 4 36
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

730 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