Solved

Intersection of a string

Posted on 2009-07-12
11
234 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
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
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.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
reverse digits of a number using for loop 5 52
null output 3 35
Running JavaFX on JDeveloper 12C 1 55
Why doesn't this text field show up on my Applet frame? 2 20
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:

827 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