Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,987 questions

51,931 answers

573 users

How to get intersection of two strings in Java

3 Answers

0 votes
import java.util.HashSet;

public class StringIntersection {

    // Function to compute the intersection of characters in two strings
    public static HashSet<Character> getIntersection(String str1, String str2) {
        HashSet<Character> hst1 = new HashSet<>();
        for (char ch : str1.toCharArray()) {
            hst1.add(ch);
        }

        HashSet<Character> hst2 = new HashSet<>();
        for (char ch : str2.toCharArray()) {
            hst2.add(ch);
        }

        hst1.retainAll(hst2); // Keep only common characters
        
        return hst1;
    }

    public static void main(String[] args) {
        String str1 = "php";
        String str2 = "python";

        HashSet<Character> intersection = getIntersection(str1, str2);

        System.out.println("Intersection: " + intersection);
    }
}



/*
run:

Intersection: [p, h]

*/

 



answered Sep 12, 2023 by avibootz
edited Jul 6, 2025 by avibootz
0 votes
import java.util.HashSet;
import java.util.Set;

public class StringIntersection {

    // Function that returns the set of common characters between two strings
    public static Set<Character> getIntersection(String str1, String str2) {
        Set<Character> set1 = new HashSet<>();
        for (char ch : str1.toCharArray()) {
            set1.add(ch);
        }

        Set<Character> intersection = new HashSet<>();
        for (char ch : str2.toCharArray()) {
            if (set1.contains(ch)) {
                intersection.add(ch);
            }
        }

        return intersection;
    }

    public static void main(String[] args) {
        String str1 = "php";
        String str2 = "python";

        Set<Character> result = getIntersection(str1, str2);

        System.out.println("Intersection: " + result);
    }
}

/*
run:

Intersection: [p, h]

*/

 



answered Jul 6, 2025 by avibootz
0 votes
import java.util.Set;
import java.util.stream.Collectors;
 
public class StringIntersection {
    public static void main(String[] args) {
        String str1 = "php";
        String str2 = "python";
 
        Set<Character> set1 = str1.chars().mapToObj(ch -> (char) ch).collect(Collectors.toSet());
        Set<Character> set2 = str2.chars().mapToObj(ch -> (char) ch).collect(Collectors.toSet());
 
        set1.retainAll(set2); // This is the intersection
 
        String intersection = set1.stream()
                                  .map(String::valueOf)
                                  .collect(Collectors.joining());
 
        System.out.println("Intersection: " + intersection);
    }
}
 
 
/*
run:
 
Intersection: ph
 
*/

 



answered Jul 6, 2025 by avibootz
edited Jul 6, 2025 by avibootz
...