从字符串生成单词列表的组合
Generate a combination of word list from string
我想从 java
中的给定字符串生成关键字列表
例如“这是我的字符串”
生成列表
"this is my"
"this is"
"this"
"string"
"my string"
"is my string"
你可以试试这个:
public static void main(String[] args) {
String[] wordArray = "this is my string".split(" ");
Set<String> result = new HashSet<>();
int n = wordArray.length;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
result.add(
IntStream.rangeClosed(i, j)
.mapToObj(v -> wordArray[v])
.collect(Collectors.joining(" ")));
}
}
result.forEach(System.out::println);
}
如果没有特殊的输入规则。
我想从 java
中的给定字符串生成关键字列表例如“这是我的字符串”
生成列表
"this is my" "this is" "this" "string" "my string" "is my string"
你可以试试这个:
public static void main(String[] args) {
String[] wordArray = "this is my string".split(" ");
Set<String> result = new HashSet<>();
int n = wordArray.length;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
result.add(
IntStream.rangeClosed(i, j)
.mapToObj(v -> wordArray[v])
.collect(Collectors.joining(" ")));
}
}
result.forEach(System.out::println);
}
如果没有特殊的输入规则。