String comparision

R

raj

Hi,
I have two strings with values say like

String s1 ="hello,test,from,one,party";
String s2 = "into,bytes,hello,to";

i need to compare s1 values with s2 values if exists i need to return
the matched value.

hello in s1 is present in s2 ..i neeed to return hello...

can any one help how to proceed.

regards
raj
 
L

Lars Enderin

raj said:
Hi,
I have two strings with values say like

String s1 ="hello,test,from,one,party";
String s2 = "into,bytes,hello,to";

i need to compare s1 values with s2 values if exists i need to return
the matched value.

hello in s1 is present in s2 ..i neeed to return hello...

can any one help how to proceed.

You can use s1.split(",") and s2.split(",") to create two String arrays.
Then you compare the arrays element by element.
 
R

raj

You can use s1.split(",") and s2.split(",") to create two String arrays.
Then you compare the arrays element by element.


thanks for the reply ..i did the same way

public class StringComp {
public static void main(String args[]) {
String s1 ="hello,test,from,cbn,party";
String s2 = "jain,dead,hello,to";

String sold [] = s1.split(",");
for(int i =0 ;i<sold.length;i++) {
System.out.println("OLD:::: " +sold);
}
String snew [] = s2.split(",");
for(int i =0 ;i<snew.length;i++) {
System.out.println("NEW::: " +snew);
}
}
}


how can i compare the array with another ?
 
R

raj

You can use s1.split(",") and s2.split(",") to create two String
arrays. Then you compare the arrays element by element.

Or convert those arrays to Lists with Arrays.asList(T[]), make a HashSet
out of one of them, and pass the other to its retainAll(Collection) method.

thanks alot

i tried out and got it

public class StringComp {
public static void main(String args[]) {
String s1 ="hello,test,from,cbn,party";
String s2 = "jain,dead,hello,to,cbn";

String sold [] = s1.split(",");
for(int i =0 ;i<sold.length;i++) {
String out = sold;
String snew [] = s2.split(",");
for(int j=0; j<snew.length; j++){
String in = snew[j];
if(in.equalsIgnoreCase(out)){
System.out.println("equal: " +in);
}
}
}
}
 
D

Donkey Hottie

thanks alot

i tried out and got it

public class StringComp {
public static void main(String args[]) {
String s1 ="hello,test,from,cbn,party";
String s2 = "jain,dead,hello,to,cbn";

String sold [] = s1.split(",");
for(int i =0 ;i<sold.length;i++) {
String out = sold;
String snew [] = s2.split(",");
for(int j=0; j<snew.length; j++){
String in = snew[j];
if(in.equalsIgnoreCase(out)){
System.out.println("equal: " +in);
}
}
}
}


Simple optimisation: create the array snew[] at the same time as sold[].
Now you keep creating it again and again for each value in sold[].
 
D

Donkey Hottie

Lars said:
raj wrote:
    String s1 ="hello,test,from,one,party";
    String s2 = "into,bytes,hello,to";
i need to compare s1 values with s2 values if exists i need to
return the matched value.
hello in s1 is present in s2 ..i neeed to return hello...
You can use s1.split(",") and s2.split(",") to create two String
arrays. Then you compare the arrays element by element.

Or convert those arrays to Lists with Arrays.asList(T[]), make a
HashSet out of one of them, and pass the other to its
retainAll(Collection) metho d.

thanks alot

i tried out and got it

public class StringComp {
public static void main(String args[]) {
String s1 ="hello,test,from,cbn,party";
String s2 = "jain,dead,hello,to,cbn";

String sold [] = s1.split(",");
for(int i =0 ;i<sold.length;i++) {
String out = sold;
String snew [] = s2.split(",");
for(int j=0; j<snew.length; j++){
String in = snew[j];
if(in.equalsIgnoreCase(out)){
System.out.println("equal: " +in);
}
}
}
}


Done with the mentioned Arrays.asList() and retainAll()


package retainall;

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

public class Main
{
public static void main(String[] args)
{
String s1 ="hello,test,from,cbn,party";
String s2 = "jain,dead,hello,to,cbn";

List<String> l1 = new ArrayList(Arrays.asList(s1.split(","))) ;
List<String> l2 = new ArrayList(Arrays.asList(s2.split(","))) ;
l1.retainAll(l2);

for (String s : l1)
{
System.out.println("equal: " + s) ;
}
}
}


The lists must be created as an ArrayList, as the asList() returned
Collections do not support remove().
 
R

Roedy Green

Or convert those arrays to Lists with Arrays.asList(T[]), make a HashSet
out of one of them, and pass the other to its retainAll(Collection) method.

Another approach, a little harder to program, but might pay for very
large lists:

sort each list. Do a linear merge-style compare working through both
lists looking for dups. You don't have to compare every element
against every element in the other list. You just have to compare
every element in one list against a few in the other list.

For the merge dedup logic see
http://mindprod.com/products1.html#SORTED sorted array lists.
--
Roedy Green Canadian Mind Products
http://mindprod.com

For most people, a global average temperature rise of 3°C (5°F) does not sound
alarming. They don’t understand that the warming is uneven, mostly over the
continents which will rise 6°C (11°F). The maximum annual temperature will
increase considerably more. Paris will have the temperature of Algiers,
Moscow the temperature of Paris, San Francisco the temperature of Tijuana and
New York the temperature of Charleston, and Houston the temperature of some
obscure dusty town in southern Mexico.
 
D

Donkey Hottie

Lars Enderin wrote:
raj wrote:
    String s1 ="hello,test,from,one,party";
    String s2 = "into,bytes,hello,to";

i need to compare s1 values with s2 values if exists i need to
return the matched value.

hello in s1 is present in s2 ..i neeed to return hello...

You can use s1.split(",") and s2.split(",") to create two String
arrays. Then you compare the arrays element by element.

Or convert those arrays to Lists with Arrays.asList(T[]), make a
HashSet out of one of them, and pass the other to its
retainAll(Collection) metho d.

thanks alot

i tried out and got it

public class StringComp {
public static void main(String args[]) {
String s1 ="hello,test,from,cbn,party";
String s2 = "jain,dead,hello,to,cbn";

String sold [] = s1.split(",");
for(int i =0 ;i<sold.length;i++) {
String out = sold;
String snew [] = s2.split(",");
for(int j=0; j<snew.length; j++){
String in = snew[j];
if(in.equalsIgnoreCase(out)){
System.out.println("equal: " +in);
}
}
}
}


Done with the mentioned Arrays.asList() and retainAll()


package retainall;

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

public class Main
{
public static void main(String[] args)
{
String s1 ="hello,test,from,cbn,party";
String s2 = "jain,dead,hello,to,cbn";

List<String> l1 = new ArrayList(Arrays.asList(s1.split(","))) ;
List<String> l2 = new ArrayList(Arrays.asList(s2.split(","))) ;
l1.retainAll(l2);

for (String s : l1)
{
System.out.println("equal: " + s) ;
}
}
}


The lists must be created as an ArrayList, as the asList() returned
Collections do not support remove().


Tuned up version. This does not use Lists and prints the values in sorted
order.


package retainall;

import java.util.Arrays;
import java.util.Collection;
import java.util.TreeSet;

public class Main
{
public static void main(String[] args)
{
String s1 = "hello,test,from,cbn,party";
String s2 = "jain,dead,hello,to,cbn";

Collection<String> l1 = new TreeSet<String>(Arrays.asList
(s1.split(",")));
Collection<String> l2 = Arrays.asList(s2.split(","));
l1.retainAll(l2);

for (String s : l1)
{
System.out.println("equal: " + s);
}
}
}
 
D

Daniel Pitts

raj said:
Hi,
I have two strings with values say like

String s1 ="hello,test,from,one,party";
String s2 = "into,bytes,hello,to";

i need to compare s1 values with s2 values if exists i need to return
the matched value.

hello in s1 is present in s2 ..i neeed to return hello...

can any one help how to proceed.

regards
raj

public Set<String> inBoth(String s1, String s2) {
Set<String> same = new HashSet<String>(Arrays.asList(s1.split(",")));
same.retainAll(Arrays.asList(s2.split(",")));
return same;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top