From 641e8b35863c3f808eb2c811695d67ec424a6df3 Mon Sep 17 00:00:00 2001 From: sats17 Date: Wed, 29 May 2024 20:00:01 +0530 Subject: [PATCH 1/2] Added new stream question for Extracting unique matching strings from String array --- src/com/rohit/Sample.java | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/com/rohit/Sample.java b/src/com/rohit/Sample.java index a9a3080..0232a4b 100644 --- a/src/com/rohit/Sample.java +++ b/src/com/rohit/Sample.java @@ -4,6 +4,7 @@ import java.time.Period; import java.util.*; import java.util.function.Function; +import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; @@ -249,6 +250,13 @@ public static void main(String[] args) { * Write a Java 8 program to calculate the age of a person in years given their birthday. */ calculatePersonAgeInYear(); + + /** + * Extract unique matching strings from String array + * + * Write a Java 8 program to extract Unique String starting with "#" from the String array Using Java Streams + */ + extractUniqueStringStartingWithHashtag(); } private static void calculatePersonAgeInYear() { @@ -607,4 +615,19 @@ private static void separationOfEvenOddNumberInMap() { System.out.println(evenAddOddSeparation); } -} + + private static void extractUniqueStringStartingWithHashtag() { + String[] input = new String[]{ + "This JEP is #mainly for scientific #applications", + "and it makes #floating-point operations consistently #strict.", + "The default #floating-point operations are #strict or strictfp," + }; + + List output = Arrays.stream(input).flatMap(line -> Arrays.stream(line.split(" "))) + .filter(word -> word.startsWith("#")) + .map(str -> "#" + str.substring(1).replaceAll("^\\W+|\\W+$", "")) + .distinct() + .collect(Collectors.toList()); + output.forEach(System.out::println); + } +} \ No newline at end of file From ecc849b7c7965441e7e23316ca1ef33050badbed Mon Sep 17 00:00:00 2001 From: Satish Kumbhar <40035899+sats17@users.noreply.github.com> Date: Wed, 29 May 2024 21:37:51 +0530 Subject: [PATCH 2/2] Update README.md for new question --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 09ca450..e864d07 100644 --- a/README.md +++ b/README.md @@ -130,3 +130,9 @@ This repository contains sample Java 8 coding questions that can be used for int 32. **Calculate the age of a person in years** Write a Java 8 program to calculate the age of a person in years given their birthday. + +33. **Extract unique matching strings from String array** + + Write a Java 8 program to extract Unique String starting with "#" from the String array Using Java Streams. + +