如何编写java程序只打印字符串中的重复字符?

我想仅使用集合(Set)打印字符串中的重复字符。


我已经编写了代码,但如果字符串是“ashish”,它将显示正确的结果,但如果字符串是“ashish java”,则会显示失败,因为字符“a”出现了三次。


public class DuplicateStringMethod {

    public static void duplicateString(String str) {

        char[] cArray = str.toCharArray();

        Set<Character> set = new HashSet<Character>();


        for(char c:cArray) {

            if(set.add(c)==false) {

                System.out.println(c);

            }

        }

    }


    public static void main(String[] args) {

        duplicateString("Java ashishj ");

    }

}

它将打印a a s h。但我a s h只想使用Set界面。


动漫人物
浏览 225回答 4
4回答

凤凰求蛊

再使用一组来存储重复元素并打印该元素。尝试这样:public static void duplicateString(String str) {&nbsp; &nbsp; &nbsp; &nbsp; str=str.replaceAll(" ","");&nbsp; &nbsp; &nbsp; &nbsp; char[] cArray = str.toCharArray();&nbsp; &nbsp; &nbsp; &nbsp; Set<Character> set = new HashSet<Character>();&nbsp; &nbsp; &nbsp; &nbsp; Set<Character> set1 = new HashSet<Character>();&nbsp; &nbsp; &nbsp; &nbsp; for(char c:cArray) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(set.add(c)==false) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(set1.add(c) == true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(c);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

猛跑小猪

我并不完全清楚“仅使用接口”需要什么,Set但我假设这意味着重复的字符将在Set. 有几种方法可以做到这一点。第一个是对输入字符串的字符进行直接循环。它利用了以下功能:如果集合被修改,则Set.add返回;如果没有,则返回;这意味着返回的操作是重复的。truefalseaddfalsestatic Set<Character> dups0(String input) {&nbsp; &nbsp; Set<Character> dups = new HashSet<>();&nbsp; &nbsp; Set<Character> seen = new HashSet<>();&nbsp; &nbsp; for (char ch : input.toCharArray()) {&nbsp; &nbsp; &nbsp; &nbsp; if (! seen.add(ch)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dups.add(ch);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return dups;}有一种流式的方式可以做到这一点,本质上与流形式表达的东西是一样的:static Set<Character> dups1(String input) {&nbsp; &nbsp; &nbsp;Set<Character> seen = new HashSet<>();&nbsp; &nbsp; &nbsp;return input.chars()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.mapToObj(ch -> (char)ch)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.filter(ch -> !seen.add(ch))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(toSet());}有些人可能会觉得这令人讨厌,因为它的过滤操作会产生副作用。此外,如果并行运行,结果将需要类似于ConcurrentHashMap.newKeySet.另一种方法是生成字符频率表并删除仅出现一次的所有条目:static Set<Character> dups2(String input) {&nbsp; &nbsp; &nbsp;Map<Character, Long> map = input.chars()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.mapToObj(i -> (char)i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(groupingBy(ch -> ch, HashMap::new, counting()));&nbsp; &nbsp; &nbsp;map.values().removeIf(v -> v == 1);&nbsp; &nbsp; &nbsp;return map.keySet();}请注意,这在映射的值集合视图上使用集合批量突变操作。为了确保映射是可变的,我使用了三参数重载groupingBy来指定映射的实现类型。如果你不喜欢突变,有一个纯流的方法可以做到这一点:static Set<Character> dups3(String input) {&nbsp; &nbsp; Map<Character, Long> map = input.chars()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .mapToObj(i -> (char)i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(groupingBy(ch -> ch, counting()));&nbsp; &nbsp; return map.entrySet().stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(entry -> entry.getValue() > 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(Map.Entry::getKey)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(toSet());}

隔江千里

尝试一下:public static void duplicateString(String str) {    Set<Character> firstTime = new HashSet<Character>();    Set<Character> reported = new HashSet<Character>();    char[] cArray = str.toCharArray();    for(char c:cArray) {        if (!firstTime.contains(c)) {          firstTime.add(c);          continue;        }        if (reported.contains(c)) { continue; }        reported.add(c);        System.out.println(c);    }}我进行了一些测试:添加:10000000 次操作需要 52443260ns包含:10000000 次操作需要 28209745ns因此上面的代码虽然不短但速度最快。

至尊宝的传说

检查这个程序public static void duplicateString(String str) {&nbsp; &nbsp; &nbsp; &nbsp; char[] cArray = str.replaceAll("\\s+", "").toCharArray();&nbsp; &nbsp; &nbsp; &nbsp; Set<Character> set = new HashSet<Character>();&nbsp; &nbsp; &nbsp; &nbsp; Set<Character> alreadyExistingSet = new HashSet<Character>();&nbsp; &nbsp; &nbsp; &nbsp; for (char c : cArray) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (set.add(c) == false && alreadyExistingSet.add(c) == true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(c);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java