gpt4 book ai didi

java - 将子字符串作为键并将以下子字符串作为值放入 TreeMap

转载 作者:行者123 更新时间:2023-12-02 00:24:15 25 4
gpt4 key购买 nike

示例:

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

我想将分割的字符串及其后面的 (+1) 字符串存储在树形图中:windowSize = 4(预定义):Typi,ypic -> 放入 TreeMapypic,pica -> 放入TreeMap

对于windowSize = 2,它看起来像这样:Ty,yp -> 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())))));

}

如果我用 sysprint 打印它,我会得到:{Augen=Augen、Außen=Außen、Innen=Innen、Jauch=Jauch,等等

最佳答案

我的看法:

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());

关于java - 将子字符串作为键并将以下子字符串作为值放入 TreeMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10322883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com