5

I am trying to get certain text out from a line that I get when using the new Stream in Java 8.

This is what I am reading in:

46 [core1]
56 [core1]
45 [core1]
45 [core2]
67 [core2]
54 [core2]

And here is the code I read it with currently:

Path path = Paths.get("./src/main/resources/", "data.txt");
            try(Stream<String> lines = Files.lines(path)){
                List<Integer> temps = new ArrayList<>();
                lines
                        .filter(line -> line.contains("[core1]"))
                        .filter(line -> line.contains("(\\d+).*"))
                        .flatMapToInt(temperature -> IntStream.of(Integer.parseInt(temperature)))
                        .forEach(System.out::println);
                System.out.print(temps.size());
            }

I have checked the regex expression in https://www.regex101.com/ and it seems to work fine. Also if I just search for the [core1] string, it will find it.

The problem is that when using all of this together, I get 0 matches. My logic behind this currently is that I read a line, see what core it is, and then get it's number before it. After that I want to add it to a List.

What am I doing wrong here?

5
  • Read the javadoc of contains(): docs.oracle.com/javase/7/docs/api/java/lang/…. Does it accept regexes? Commented Apr 22, 2015 at 19:03
  • Does not say anything about it. Would be logical to have the support tough. Any way to get this working in some hackish style? Commented Apr 22, 2015 at 19:06
  • 1
    I guess you are looking for String#matches instead of contains... Though you'll need to extract the integer from the line before calling parseInt. And calling flatMapToInt by creating an IntStream with one element is not very useful. Just use mapToInt instead... Commented Apr 22, 2015 at 19:06
  • 1
    @Kaspar if a javadoc says: "Returns true if and only if this string contains the specified sequence of char values.", then no, it's not logical for it to support regexes. Methods don't do what you think they do. They do what the documentation says they do. Commented Apr 22, 2015 at 19:08
  • 1
    Not related to your question, but there is no need to flatMapToInt for a single item. Simple use mapToInt. .mapToInt(Integer::parseInt) docs.oracle.com/javase/8/docs/api/java/util/stream/… Commented Apr 22, 2015 at 19:56

1 Answer 1

11

contains works only with strings (regexes not supported)... you can use line.matches("(\\d+).*") for achieving the same.

Sign up to request clarification or add additional context in comments.

2 Comments

If your file is very large, you can cache the compiled regex Pattern for improved speed. Pattern is thread safe (immutable) so you can even use it in a multithreaded context if need be.
@Giovanni Botta: right, and if you have a compiled pattern, you can use asPredicate to create a filter, so you can still do it in one line: .filter(Pattern.compile("[core1]").asPredicate()) .filter(Pattern.compile("(\\d+).*").asPredicate())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.