admin管理员组

文章数量:1446760

获取不重复的字符串

代码语言:java复制
    /**
     * 获取不重复的字符串
     *  aabbc->abc
     * @param srcStr
     * @return
     */
    public static String getSingleCharStr(String srcStr){
        if (srcStr==null){
            return null;
        }
        if (srcStr.isEmpty()){
            return null;
        }
        String trim = srcStr.trim();
        Map<Character, Integer> characterIntegerHashMap = new LinkedHashMap<>();
        for (int i = 0; i < trim.length(); i++) {
            char c = trim.charAt(i);
            if (characterIntegerHashMap.containsKey(c)){
                characterIntegerHashMap.put(c,characterIntegerHashMap.get(c)+1);
            }else {
                characterIntegerHashMap.put(c,1);
            }
        }
        ArrayList<CustomerStr> customerStrs = new ArrayList<>();
        characterIntegerHashMap.forEach((character, integer) ->{
            CustomerStr customerStr = new CustomerStr();
            customerStr.setId(UUID.randomUUID().toString());
            customerStr.setCharTemp(character);
            customerStr.setCharCout(integer);
            customerStrs.add(customerStr);
        });
        StringBuilder stringBuilder = new StringBuilder();
        customerStrs.forEach(e->{
            stringBuilder.append(e.getCharTemp());
        });
        return stringBuilder.toString();
    }

    public static String getSingleCharStr1(String srcStr){
        if (srcStr==null){
            return null;
        }
        if (srcStr.isEmpty()){
            return null;
        }
        String trim = srcStr.trim();
        Set<Character> characters = new LinkedHashSet<>();
        for (int i = 0; i < trim.length(); i++) {
            characters.add(trim.charAt(i));
        }
        StringBuilder stringBuilder = new StringBuilder();
        characters.forEach(e->{
            stringBuilder.append(e);
        });
        return stringBuilder.toString();
    }
    class CustomerStr{
    private String id;
    private Character charTemp;
    private Integer charCout;
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Character getCharTemp() {
        return charTemp;
    }

    public void setCharTemp(Character charTemp) {
        this.charTemp = charTemp;
    }

    public Integer getCharCout() {
        return charCout;
    }

    public void setCharCout(Integer charCout) {
        this.charCout = charCout;
    }
}

本文标签: 获取不重复的字符串