java实现不同数字或者字母排列组合,但是不能处理特别长的字符

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class PermutationCombination {
    public static HashMap dealHashMap(HashMap rs) {
        HashMap resultMap = new HashMap<>();
        if (rs == null || rs.isEmpty()) {
            return resultMap;
        }

        Iterator> it = rs.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = it.next();
            String message = entry.getValue();
            for (int index = 0; index < message.length(); index++) {
                String key = entry.getKey() + message.substring(index, index + 1);
                String value = message.substring(0, index) + message.substring(index + 1, message.length());
                resultMap.put(key, value);
            }
        }
        rs.clear();
        return resultMap;

    }

    public static void main(String[] args) {
        String message = "123456789";
        HashMap rs = new HashMap<>();
        //输出数的排列组合
        for (int index = 0; index < message.length(); index++) {
            message.substring(index, index + 1);
            String mes = message.substring(0, index) + message.substring(index + 1, message.length());
            rs.put(message.substring(index, index + 1), mes);
        }
        int i = 0;
        while (i < message.length() - 1) {
            rs = dealHashMap(rs);
            i++;
        }
        for (Map.Entry entry : rs.entrySet()) {
            System.out.println(entry.getKey());
        }
    }
}

你可能感兴趣的:(java)