Reputation: 2429
Example:
String wholeString =
"Typical models for star formation assume that every type of galaxy produces stars"
I'd like to store the splitted string and its following (+1) String in a treemap:
with windowSize = 4
(predefined):
Typi
,ypic
-> put into TreeMap
ypic
,pica
-> put into TreeMap
for windowSize = 2
it would look like this:
Ty
,yp
-> TreeMap
and so on.
My code so far:
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())))));
}
If I sysprint it, I gets this:
{ Augen=Augen, Außen=Außen, Innen=Innen, Jauch=Jauch,
and so on
Upvotes: 0
Views: 493
Reputation: 200168
My take:
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());
Upvotes: 1
Reputation: 10151
If you like recursion, you may try this:
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);
}
}
Please note that I'm using 'LinkedHashMap', so output will be in order you put values into it. If you need to put into a TreeMap, simply replace LinkedHashMap with TreeMap and add import statement. But result will be sorted in natural order, not the order you put your values in.
Upvotes: 1
Reputation: 47608
Here you go (I changed the TreeMap by a LinkedHashMap for debug purposes, you can put back the TreeMap if your prefer):
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);
}
}
Upvotes: 1