Wednesday, July 17, 2019

String Operations with Java Streams

Q- Check if a string contains an element from a list of strings?
Q- Convert List of Characters to String in java?

package com.shubh.stream.api;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class StringOperationWithStream {

 public static void main(String[] args) {

  String input = "A hardworking person must relax";
  Predicate startsWithA = (text) -> text.startsWith("A");
  Predicate endsWithX = (text) -> text.endsWith("x");

  // Way-1 (Using Built-in Functional interface --Predicate)
  Predicate startsWithAAndEndsWithX = (text) -> startsWithA.test(text) && endsWithX.test(text);
  boolean result = startsWithAAndEndsWithX.test(input);
  System.out.println(result);

  // Way-2 (Using Predicate Composition "and()")
  Predicate composed = startsWithA.and(endsWithX);
  boolean result2 = composed.test(input);
  System.out.println(result2);

  // Way-3 (Using Predicate Composition "or()")
  Predicate composed2 = startsWithA.or(endsWithX);
  boolean result3 = composed2.test(input);
  System.out.println(result3);

// Using Functional Composition "compose()" example
  Function multiply = (value) -> value * 2;
  Function add = (value) -> value + 3;
  Function addThenMultiply = multiply.compose(add);
  Integer result4 = addThenMultiply.apply(3);
  System.out.println(result4);

// Using Functional Composition "andThen()" example
  // Function multiply = (value) -> value * 2;
  // Function add = (value) -> value + 3;
  Function multiplyThenAdd = multiply.andThen(add);
  Integer result5 = multiplyThenAdd.apply(3);
  System.out.println(result5);

// Find the string in string array which Start with A and End With C
  System.out.println("// String Start with A and End With C");
  String str[] = { "AAA", "AAB", "AAC", "ABC" };
  Predicate strStartsWithA = (text) -> text.startsWith("A");
  Predicate strEndWithC = (text) -> text.endsWith("C");
  Predicate strStartsWithAAndEndsWithC = (text) -> strStartsWithA.test(text) && strEndWithC.test(text);
  Arrays.asList(str).stream().filter(strStartsWithAAndEndsWithC).forEach(s -> System.out.println(s));
  

//Find the string in string array which Contains B and C
  System.out.println("\n// String Contains B and C");
  String str2[] = { "AAA", "AAB", "AAC", "ABC" };
  Predicate strContainB = (text) -> text.contains("B");
  Predicate strContainC = (text) -> text.contains("C");
  // Predicate strContainAandC = (text) -> strContainB.test(text) &&
  // strContainC.test(text);
  // Using Predicate Composition
  Predicate strContainAandC = strContainB.and(strContainC);
  Arrays.asList(str2).stream().filter(strContainAandC).forEach(s -> System.out.println(s));
  

// Find the string in string array which Contains B or C
  System.out.println("\n// String Contains B or C");
  String str3[] = { "AAA", "AAB", "AAC", "ABC" };
  Predicate strContain_B = (text) -> text.contains("B");
  Predicate strContain_C = (text) -> text.contains("C");
  // Predicate strContainA_or_C = (text) -> strContain_B.test(text) ||
  // strContain_C.test(text);
  // Using Predicate Composition
  Predicate strContainA_or_C = strContain_B.or(strContain_C);

  // Print filtered records
  Arrays.asList(str3).stream().filter(strContainA_or_C).forEach(s -> System.out.println(s));

  System.out.println("\n// Iterate using forEach()");
  List result6 = Arrays.asList(str3).stream().filter(strContainA_or_C).collect(Collectors.toList());
  result6.forEach(s -> System.out.println(s));
 }
}
Output of above operations.

true
true
true
12
9
// String Start with A and End With C
AAC
ABC

// String Contains B and C
ABC

// String Contains B or C
AAB
AAC
ABC

// Iterate using forEach()
AAB
AAC
ABC

No comments:

Post a Comment