Q:

How do i make more elegant function to get equal letters from 2 strings in Java?

I could not find standard string-function in Java to find matching letters in 2 strings.
Do you know how to make the below code smaller/refactor it using some standard functions?
I only made simplest function: compare every letter of first string to every letter of second string.
public static void compare_letters(String name1, String name2) {
  int equal_letters = 0 ;
  for(int i = 0 ; i < name1.length() ; i++) {
    String current_letter  = name1.substring(i,i+1);

    for(int j = 0 ; j < name2.length() ; j++) {
      if(current_letter.equals(name2.substring(j,j+1)))
        equal_letters++ ;
    }
  }
  System.out.println("num of equal letters " + equal_letters) ;
}

public static void main(String args[]) {
  String n1 = "abc";
  String n2 = "bcd";
  compare_letters(n1,n2);
}

How do i refactor it to make it smaller - I m sure there must be some elegant way to do this! This is so common procedure - there must be elegant way to do that!

A:

Your code could be simplified a bit:
public static void compare_letters(String name1, String name2) {
    int equal_letters = 0 ;

    for( char c1 : name1.toCharArray() )
        for( char c2 : name2.toCharArray() )
            if( c1 == c2 ) equal_letters++;

    System.out.println("num of equal letters " + equal_letters);
}

But note that this way of comparing will double-count repeated letters, e.g. comparing "aa" to "aa" will return four, because each a in one string will be compared to each a in the other.
If you want to count common letters without double-counting repetitions, you'd need to do something like
public static void compare_letters(String name1, String name2) {

    // Declare a couple of sets: sets don't allow duplicate elements.
    Set<Character> letters1 = new HashSet<Character>();
    Set<Character> letters2 = new HashSet<Character>();

    // Populate the sets with the letters from the strings.
    for( char c : name1.toCharArray() ) letters1.add(c);
    for( char c : name2.toCharArray() ) letters2.add(c);

    // remove anything that isn't in letters2 from letters1
    letters1.retainAll(letters2);

    // letters1 is now the set of letters that appear in both names.
    // Its size is the number of common letters.
    System.out.println("num of equal letters " + letters1.size(););
}

