1
import java.util.*;

class set {
    public static void main(String args[]) {
        TreeSet<Character> t1 = new TreeSet<Character>();
        TreeSet<Character> t2 = new TreeSet<Character>();
        String s1 = "Ambitab bachan";
        String s2 = "Ranjikanth";
        for (char c1 : s1.toCharArray())
            t1.add(c1);
        for (char c2 : s2.toCharArray())
            t2.add(c2);
        t2.retainAll(t1);
        System.out.println(t2);
    }
}

this program finds the common character in two different strings. In this program TreeSet is used to store the value and retainAll() method is used to find the common characters.

Can anybody help me reduce the line of code.

cafebabe1991
  • 4,928
  • 2
  • 34
  • 42
Venkatesh
  • 577
  • 1
  • 7
  • 16
  • Apart from putting several statements on the same line, it is going to be difficult to reduce the number of lines. In particular, `toCharArray` returns a `char[]` so you need to transform each `char` to `Character` in a loop st some stage. – assylias Dec 15 '12 at 16:58
  • Declare two variables in same line? different statements in single line? – Pradeep Simha Dec 15 '12 at 16:58
  • Why are you concerned about reducing the number of lines in code that's clearly not for production? Is it a metric that you're trying to rate with? – Seeta Somagani Dec 15 '12 at 17:26

5 Answers5

3

If you use the Guava library, you can simply use Sets.intersection() and avoid the boilerplate.

reprogrammer
  • 14,298
  • 16
  • 57
  • 93
2

As you're dealing with Strings, you can forgo all these objects and use regular expressions.

private static final Pattern REGEX_DEDUPLICATE = Pattern.compile("(.)(?=.*\\1)");

public void intersect(String string1, String string2) {
    String commonChars = string1.replaceAll("[^" + string2 + "]", "");
    String uniqueCommonChars = REGEX_DEDUPLICATE.matcher(commonChars).replaceAll("");
    return uniqueCommonChars;
}

The first replaceAll removes all characters from string1 that are not in string2. commonChars is thus:

itaahan

More in this discussion.

The second replaceAll removes redundant instances of a character, the last two 'a's in this case. uniqueCommonChars becomes:

ithan

A great break down of how this regex works is in this discussion.

Compiling regular expressions is relatively computationally expensive so the we can precompile one as a static final Pattern. As the other regular expression is based on the input, it cannot be precompiled.

Community
  • 1
  • 1
Sean Connolly
  • 5,692
  • 7
  • 37
  • 74
1

I suppose you could condense this:

TreeSet<Character> t1 = new TreeSet<Character>();
for(char c1:s1.toCharArray())
t1.add(c1);

Into: (Edited)

TreeSet<String> t1 = new TreeSet<String>(Arrays.asList(s1.split("(?<=.)")));
femtoRgon
  • 32,893
  • 7
  • 60
  • 87
1

A bit simpler:

    TreeSet<Character> t1 = new TreeSet<Character>();
    String s1 = "Ambitab bachan";
    String s2 = "Ranjikanth";
    for(char c1:s1.toCharArray()) {
      if (s2.contains(new Character(c1).toString())) {
        t1.add(c1);
      }
    }
Diego Basch
  • 12,764
  • 2
  • 29
  • 24
0

You can easily remove the duplicate code to build the character sets

public static void main(String args[]) {
    TreeSet<Character> t1 = asCharacterSet("Ambitab bachan");
    TreeSet<Character> t2 = asCharacterSet("Ranjikanth");
    t2.retainAll(t1);
    System.out.println(t2);
}

private static Set<Character> asCharacterSet(String value) {
    TreeSet<Character> t1 = new TreeSet<Character>();
    for (char c1 : value.toCharArray())
        t1.add(c1);
    return t1;
}
bowmore
  • 10,842
  • 1
  • 35
  • 43