check Repeat String - Java java.lang

Java examples for java.lang:String Repeat

Description

check Repeat String

Demo Code


//package com.java2s;

import java.util.HashMap;

import java.util.Map;

public class Main {
    public static void main(String[] argv) throws Exception {
        String str = "java2s.com";
        System.out.println(checkRepeat(str));
    }//from w  ww .  ja  v  a  2s  .c o  m

    public static boolean checkRepeat(final String str) {

        final Map<Character, Integer> map = new HashMap<Character, Integer>();

        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) != ' ')
                if (map.containsKey(str.charAt(i))) {
                    map.put(str.charAt(i), map.get(str.charAt(i)) + 1);
                } else {
                    map.put(str.charAt(i), 1);
                }
        }

        for (final char c : map.keySet())
            if (map.get(c) > 1)
                return true;

        // 9899573109

        return false;
    }
}

Related Tutorials