0

I have a string array as below.

String [] exmp = {"Example ExamPle", "Example"};

I want to get distinct element from the above array irrespective of character case.

I need to get the below output for above array. "Example"

I have tried with the following code.

LinkedHashSet<String> set = new LinkedHashSet<String>();

String [] exmp = {"Example ExamPle", "Example"};

for(String s : exmp) {

String unqWrds = Arrays.stream(s.split("\\s+")).distinct().collect(Collectors.joining(" "));

set.add(unqWrds);

}

But currently I am getting entire string getting added to set due to case difference "Example ExamPle", "Example"

Could you please advise here.

4
  • Are you asking how to ignore the case difference? Because the example you chose is problematic. I don’t understand how you define distinct . If it was “example example example” would it still be the same? Commented Nov 22, 2019 at 13:27
  • There are two distinct elements in the array. Now if the array was {"Example", "ExamPle", "Example"}, there would be one distinct value when you ignore case. Commented Nov 22, 2019 at 13:28
  • And is “a a a a” the same as “a a a”? What’s the logic Commented Nov 22, 2019 at 13:28
  • I am asking how can I do equalIgnorecase here? Commented Nov 22, 2019 at 13:31

3 Answers 3

2

According to the sample code in the question, you want to split the strings on whitespace, even though you never said that in the question.

You then try to use distinct(), but that will unfortunately not work, because distinct() doesn't take a Comparator, so it cannot compare case-insensitively.

To get your desired result:

// Using loops
public static Set<String> distinctWords(String... input) {
    Set<String> distinct = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
    for (String s : input)
        for (String word : s.trim().split("\\s+"))
            distinct.add(word);
    return distinct;
}
// Using streams
public static Set<String> distinctWords(String... input) {
    return Stream.of(input)
            .flatMap(s -> Stream.of(s.split("\\s+")))
            .collect(Collectors.toCollection(() -> new TreeSet<>(String.CASE_INSENSITIVE_ORDER)));
}

The TreeSet will keep the capitalization of the first word seen, and will sort the words, so the result of calling with {"Example ExamPle", "example"} is the desired result [Example].

Sign up to request clarification or add additional context in comments.

Comments

0

Try that:

String unqWrds = Arrays.stream(s.split("\\s+")).reduce("", (x, y) -> x.toUpperCase().contains(y.toUpperCase()) ? x : x + " " + y);

Comments

0

The ignore case part can be done like this:

String unqWrds = Arrays.stream(s.split("\\s+")).map(String::toLowerCase).distinct().collect(Collectors.joining(" "));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.