Solved

Intersection of a string

Posted on 2009-07-12
11
245 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

729 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