将substring作为键,将以下子字符串作为值放入TreeMap中

时间:2012-04-25 20:01:01

标签: java substring treemap

示例:

String wholeString = 
"Typical models for star formation assume that every type of galaxy produces stars"

我想将分割的字符串及其后续的(+1)字符串存储在树形图中: 与windowSize = 4(预定义): Typiypic - >放入TreeMap ypicpica - >放入TreeMap

对于windowSize = 2,它看起来像这样: Tyyp - > TreeMap的

等等。

到目前为止我的代码:

    Map<String, String> generateMap = new TreeMap<String, String>();

    for (int i = 0; i < wholeString.length(); i++) {
        generateMap
                .put((wholeString.substring((i),
                        Math.min((i + windowSize), wholeString.length()))),
                        (wholeString.substring(
                                (i + 1),
                                (Math.min((i + windowSize),
                                        wholeString.length())))));

    }

如果我系统化它,我得到这个: { Augen=Augen, Außen=Außen, Innen=Innen, Jauch=Jauch,等等

3 个答案:

答案 0 :(得分:1)

在这里(我为了调试目的,我通过LinkedHashMap更改了TreeMap,如果您愿意,可以放回TreeMap):

import java.util.LinkedHashMap;
import java.util.Map;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Map<String, String> generateMap = new LinkedHashMap<String, String>();
        String wholeString = "Typical models for star formation assume that every type of galaxy produces stars";
        int windowSize = 4;
        for (int i = 0; i < wholeString.length(); i++) {
            int start = i;
            int end = Math.min(i + windowSize, wholeString.length());
            int start1 = Math.min(i + 1, wholeString.length() - 1);
            int end1 = Math.min(i + 1 + windowSize, wholeString.length());
            generateMap.put(wholeString.substring(start, end), wholeString.substring(start1, end1));

        }
        System.err.println(generateMap);
    }

}

答案 1 :(得分:1)

我的看法:

final String wholeString =
  "Typical models for star formation assume that every type of galaxy produces stars";
final int windowSize = 4;
final Map<String, String> generateMap = new LinkedHashMap<String, String>();

final int limit = wholeString.length() - windowSize;
for (int i = 0; i < limit;) generateMap.put(
      wholeString.substring(i, i + windowSize),
      wholeString.substring(++i, i + windowSize));

for (Map.Entry<String, String> e : generateMap.entrySet())
  System.out.println(e.getKey() + " -> " + e.getValue());

答案 2 :(得分:1)

如果您喜欢递归,可以试试这个:

import java.util.LinkedHashMap;
import java.util.Map;


public class Subs {

    /**
     * @param args
     */

    public static void func(Map<String,String> map, String input, int windowSize) {
        if (input.length() <= windowSize) {
            map.put(input, input);
            return;
        } else {
            map.put(input.substring(0, windowSize), input.substring(1, windowSize + 1));
            func(map, input.substring(1), windowSize);
        }
    }
    public static void main(String[] args) {
        String wholeString = "Typical models for star formation assume that every type of galaxy produces stars";
        Map<String,String> ourMap = new LinkedHashMap<String, String>();
        int windowSize = 4;
        func(ourMap, wholeString, windowSize);
        System.out.print(ourMap);
    }
}

请注意我正在使用'LinkedHashMap',因此输出将按顺序放入值。如果需要放入TreeMap,只需将LinkedHashMap替换为TreeMap并添加import语句即可。但结果将按自然顺序排序,而不是您输入值的顺序。