Skip to content
Advertisement

Java 8 stream string manipulation and comparison [closed]

In Java 8, can I use streams to manipulate, filter and compare string? I know how to do in java 5 but stuck with Java 8 api using Streams. Please assist with Java 8

Let us assume I have two Java objects (Request and Response) as below

JavaScript

I need to compare the request and response list objects as below

From request List I need extract indexOf first “/” for each list entry (for example “/first/same”) and compare this string with Response list to see which contains or not. If contains add it to result list.

Java 5 Impl:

JavaScript

Java 8: I am trying like,

JavaScript

Expected result:

JavaScript

Advertisement

Answer

Your code looks broken to me. Based on your “Java 5 Impl” logic I will assume responseIds should be:…

Update: According to your comment both the request and response need to id.substring(id.indexOf("/"));

Given that assumption you could do:

JavaScript

Runnable example: https://replit.com/@lrn2prgrm/StreamStringMatching#Main.java

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement