#I can not see first repeated one while using stream

1 messages · Page 1 of 1 (latest)

frozen pike
#

`` `package com.bjss.tests.questions_interview;

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class printFirstDublicateOne {
public static void main(String[] args) {
{
String inputString = "mmJava Concept Of The Day".replaceAll(" ", "").toLowerCase();
System.out.println(inputString);

        Map<String, Long> charCountMap =
                Arrays.stream(inputString.split(""))
                        .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

        String firstRepeatedChar = charCountMap.entrySet()
                .stream()
                .filter(entry -> entry.getValue() > 1)
                .map(entry -> entry.getKey())
                .findFirst()
                .get();

        System.out.println(firstRepeatedChar);
    }
}

}
`` in this code, the output is "a" but expected m, what can be the reason , I have debugged but can not find out the reason , any help really appreciated

red meadowBOT
#

<@&987246399047479336> please have a look, thanks.

rapid stirrup
#

because maps have no guarantee about the order of the entries

#

you can provide it a custom map factory for a map which does preserve insertion order (LinkedHashMap)