Solved

Intersection of a string

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

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now