Java 8 Streams modify collection values

Warosaurus

Using the stream API; once the relevant data has been filtered I'd like to edit the data being collected. Here is the code so far:

  String wordUp = word.substring(0,1).toUpperCase() + word.substring(1);
  String wordDown = word.toLowerCase();

  ArrayList<String> text = Files.lines(path)
        .parallel() // Perform filtering in parallel
        .filter(s -> s.contains(wordUp) || s.contains(wordDown) &&  Arrays.asList(s.split(" ")).contains(word))
        .sequential()
        .collect(Collectors.toCollection(ArrayList::new));

Edit The code below is awful and I am trying to avoid it.(It also does not entirely work. It was done at 4am, please excuse it.)

    for (int i = 0; i < text.size(); i++) {
        String set = "";
        List temp = Arrays.asList(text.get(i).split(" "));
        int wordPos = temp.indexOf(word);

        List<String> com1 = (wordPos >= limit) ? temp.subList(wordPos - limit, wordPos) : new ArrayList<String>();
        List<String> com2 = (wordPos + limit < text.get(i).length() -1) ? temp.subList(wordPos + 1, wordPos + limit) : new ArrayList<String>();
        for (String s: com1)
            set += s + " ";
        for (String s: com2)
            set += s + " ";
        text.set(i, set);
    }

It's looking for a particular word in a text file, once the line has been filtered in I'd like to only collect a portion of the line every time. A number of words on either side of the keyword that is being searched for.

eg:

keyword = "the" limit = 1

It would find: "Early in the morning a cow jumped over a fence."

It should then return: "in the morning"

*P.S. Any suggested speed improvements will be up-voted.

Holger

There are two different tasks you should think about. First, convert a file into a list of words:

List<String> words = Files.lines(path)
    .flatMap(Pattern.compile(" ")::splitAsStream)
    .collect(Collectors.toList());

This uses your initial idea of splitting at space characters. This might be sufficient for simple tasks, however, you should study the documentation of BreakIterator to understand the difference between this simple approach and a real, sophisticated word boundary splitting.

Second, if you have a list of words, your task is to find matches of your word and convert sequences of items around the match into a single match String by joining the words using a single space character as delimiter:

List<String> matches=IntStream.range(0, words.size())
    // find matches
    .filter(ix->words.get(ix).matches(word))
    // create subLists around the matches
    .mapToObj(ix->words.subList(Math.max(0, ix-1), Math.min(ix+2, words.size())))
    // reconvert lists into phrases (join with a single space
    .map(list->String.join(" ", list))
    // collect into a list of matches; here, you can use a different
    // terminal operation, like forEach(System.out::println), as well
    .collect(Collectors.toList());

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

forEach not modify java(8) collection

From Dev

Java 8 streams: count values

From Dev

build a collection using java8 streams

From Dev

Java 8 - Streams Nested ForEach with different Collection

From Dev

Java 8 streams - merge a collection of Maps

From Dev

Java 8 - Streams Nested ForEach with different Collection

From Dev

Convert a Collection to Map<String,Collection<String>> using java 8 streams

From Dev

Java 8 Streams - Collecting Values that could be null

From Dev

Java 8 streams: Handle null values

From Dev

Modify property value of the objects in list using Java 8 streams

From Dev

Java 8 streams/maps/filters to modify or delete list elements on the fly

From Dev

Filter and modify list object using java 8 streams

From Dev

Converting a collection to Map by sorting it using java 8 streams

From Java

Can Java 8 Streams operate on an item in a collection, and then remove it?

From Dev

Issue iterating a collection using Streams and Lambdas expressions of Java 8

From Dev

Best way to use Java8 streams to implement this collection

From Dev

return a value immediately from collection by java 8 streams

From Dev

Is it possible to filter/ignore objects during the collection phase with java 8 streams?

From Dev

How to generate summary report with nested collection using Java 8 streams?

From Dev

Remove some values from collection of collections using java streams

From Dev

java 8 streams unwind or emit lists to get single values

From Dev

How to group by range of values in Java 8 using streams

From Dev

How to multiply values in a list using java 8 streams

From Dev

Java 8 Streams - summing multiple values in a type from a stream

From Dev

How to multiply values in a list using java 8 streams

From Dev

Java 8 streams intermediary map/collect to a stream with 2 values

From Dev

Java 8 streams and varargs

From Dev

Java 8 streams, lambdas

From Dev

Java 8 streams and lambdas

Related Related

  1. 1

    forEach not modify java(8) collection

  2. 2

    Java 8 streams: count values

  3. 3

    build a collection using java8 streams

  4. 4

    Java 8 - Streams Nested ForEach with different Collection

  5. 5

    Java 8 streams - merge a collection of Maps

  6. 6

    Java 8 - Streams Nested ForEach with different Collection

  7. 7

    Convert a Collection to Map<String,Collection<String>> using java 8 streams

  8. 8

    Java 8 Streams - Collecting Values that could be null

  9. 9

    Java 8 streams: Handle null values

  10. 10

    Modify property value of the objects in list using Java 8 streams

  11. 11

    Java 8 streams/maps/filters to modify or delete list elements on the fly

  12. 12

    Filter and modify list object using java 8 streams

  13. 13

    Converting a collection to Map by sorting it using java 8 streams

  14. 14

    Can Java 8 Streams operate on an item in a collection, and then remove it?

  15. 15

    Issue iterating a collection using Streams and Lambdas expressions of Java 8

  16. 16

    Best way to use Java8 streams to implement this collection

  17. 17

    return a value immediately from collection by java 8 streams

  18. 18

    Is it possible to filter/ignore objects during the collection phase with java 8 streams?

  19. 19

    How to generate summary report with nested collection using Java 8 streams?

  20. 20

    Remove some values from collection of collections using java streams

  21. 21

    java 8 streams unwind or emit lists to get single values

  22. 22

    How to group by range of values in Java 8 using streams

  23. 23

    How to multiply values in a list using java 8 streams

  24. 24

    Java 8 Streams - summing multiple values in a type from a stream

  25. 25

    How to multiply values in a list using java 8 streams

  26. 26

    Java 8 streams intermediary map/collect to a stream with 2 values

  27. 27

    Java 8 streams and varargs

  28. 28

    Java 8 streams, lambdas

  29. 29

    Java 8 streams and lambdas

HotTag

Archive