递归将字符串拆分为固定数量的单词[Java] [英] Split String into Fixed Number of Words Recursively [Java]
问题描述
我试图递归地将String
(存储在ArrayList
中)分成固定数目的单词(不是字符).
I am attempting to split a String
(stored in ArrayList
) into fixed number of words (not characters) recursively.
例如,假设我有一个ArrayList
,其中包含以下两个String
短语:
For example, suppose I have the an ArrayList
which contains the following two String
phrases:
ArrayList<String> words = new ArrayList<String>();
words.add("key1 key2 key3 key4 key5 key6 key7");
words.add("key11 key12 key13 key14 key15 key16 key17");
我想分成5个单词(int desiredListSize = 5;
)的块-这将产生以下两个列表:
And I want to split into chunks of 5 words (int desiredListSize = 5;
) - this would produce the following two lists:
列表1:
word1 word2 word3 word4 word5
word2 word3 word4 word5 word6
word3 word4 word5 word6 word7
清单2:
word11 word12 word13 word14 word15
word12 word13 word14 word15 word16
word13 word14 word15 word16 word17
上面的每个列表都将添加到Lists of Lists数组中,因此输出将采用以下格式:ArrayList<ArrayList<String>()
Each list above would then be added to List of Lists array so the output would be in format: ArrayList<ArrayList<String>()
到目前为止,以下代码片段解决了大多数问题:
So far the following code fragment solves most of the problem:
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public static void splitListIntoWords()
{
int desiredListSize = 5;
final ArrayList<String> textWords = new ArrayList<String>();
textWords.add("key1 key2 key3 key4 key5 key6 key7");
textWords.add("key11 key12 key13 key14 key15 key16 key17");
final List<List<String>> listOfLists = textWords.stream().flatMap(w -> {
final String[] wordList = StringX.splitStrIntoWordsRtrnArr(w); // w.split(" ");
int calculatedListSize = (wordList.length < desiredListSize) ? wordList.length : desiredListSize;
return IntStream.range(0, Math.min(wordList.length - (calculatedListSize - 1), wordList.length)).mapToObj(i -> i ).flatMap(i -> Stream.of(
IntStream.range(i, Math.min(i+desiredListSize, wordList.length)).mapToObj(j -> wordList[j])
.collect(Collectors.toList())));
}) .collect(Collectors.toList());
for (int counter = 0; counter < listOfLists.size(); counter++) {
System.out.println("LIST: " + counter);
System.out.println(listOfLists.get(counter).toString());
}
}
哪个会产生以下输出:
LIST: 0
[key1, key2, key3, key4, key5]
LIST: 1
[key2, key3, key4, key5, key6]
LIST: 2
[key3, key4, key5, key6, key7]
LIST: 3
[key11, key12, key13, key14, key15]
LIST: 4
[key12, key13, key14, key15, key16]
LIST: 5
[key13, key14, key15, key16, key17]
但是理想的输出是:
LIST 0:
key1 key2 key3 key4 key5
key2 key3 key4 key5 key6
key3 key4 key5 key6 key7
LIST 1:
key11 key12 key13 key14 key15
key12 key13 key14 key15 key16
key13 key14 key15 key16 key17
然后将以上两个列表都添加到listOfLists
.
Both lists above should then be added to listOfLists
.
请注意,每个列表如何在所需的输出中将对字符串:key1 key2 key3 key4 key5
的操作结果存储为单个String
(每个单词之间有空格)不作为列表.
Notice how in the desired output each list stores the result of operation on String: key1 key2 key3 key4 key5
as a single String
(with a space between each word) NOT as a list.
换句话说,当一个调用listOfLists.get(0);
时,应该获得一个包含对words.add("key1 key2 key3 key4 key5 key6 key7");
进行操作的结果的列表,而当一个调用listOfLists.get(1);
时,应该应当获得对words.add("key11 key12 key13 key14 key15 key16 key17");
进行操作的结果.如果原始textWords
列表中有两个以上的条目,则listOfLists
将包含相应数量的列表.
In other words, when one calls listOfLists.get(0);
one should obtain a list that contains the results of operation on words.add("key1 key2 key3 key4 key5 key6 key7");
and when one calls listOfLists.get(1);
one should obtain results of operation on words.add("key11 key12 key13 key14 key15 key16 key17");
Of course, if there's more than two entries in original textWords
List then listOfLists
will contain a corresponding number of lists.
谢谢!
推荐答案
O.S.我现在没有时间完善我的帖子,但是(我认为)原始帖子更好.我明天可能会回来,但我也有一份工作需要做:-)
O.S. I don't have time right now to refine my posting, but here's the better (I think) of the original postings. I may get back tomorrow sometime, but I've got a job that needs doing too :-)
无论如何,这里是:
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class StackOverflow {
public static void main(final String[] args) {
final List<String> words = new ArrayList<>();
words.add("key1 key2 key3 key4 key5 key6 key7");
words.add("key11 key12 key13 key14 key15 key16 key17");
final List<List<String>> listOfLists = words.stream().flatMap(w -> {
final String[] wordList = w.split(" ");
return IntStream.range(0, Math.min( 3, wordList.length)).mapToObj(i -> i ).flatMap(i -> Stream.of(
IntStream.range(i, Math.min(i+5, wordList.length)).mapToObj(j -> wordList[j])
.collect(Collectors.toList())));
}) .collect(Collectors.toList());
listOfLists.forEach(System.out::println);
}
}
这篇关于递归将字符串拆分为固定数量的单词[Java]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!