递归地将字符串拆分为固定数量的单词 [Java] [英] Split String into Fixed Number of Words Recursively [Java]

查看:19
本文介绍了递归地将字符串拆分为固定数量的单词 [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

然后将上面的每个列表添加到列表列表数组中,因此输出格式为:ArrayList()

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 key6key7"); 并且当调用 listOfLists.get(1); 时应该获得对 words.add("key11 key12 key13 key14 key15 key16 key17") 的操作结果; 当然,如果原始textWords List 中的条目超过两个,那么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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆