In this section all program are important because mostly programming will ask form String Java 8 Stream api .We are disscussing the java 8 Stream api String based or related programs.Like as : Remove Duplicate Items from a List in Java or Print duplicate characters in a string? or How many character in String or count the Character in a String or How many String in a list or count string in list .
1). Print duplicate Unique Element in a string? or How to remove duplicates from Collections or Stream in Java 8? or How to remove duplicates from Collections or Stream in Java? or remove duplicates from string using stream java or How to remove duplicate from Arrays.
(Imp Question)
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Reamove_Duplicate_String_Example{
public static void main(String[] args) {
List<String> listofString = Arrays.asList(“Aryan”, “Shekher”, “Trilok”, “Sanjay”, “Aryan”, “Sanjay”);
List<String> uniqueElements = listofString.stream().distinct().collect(Collectors.toList());
System.out.println(“Unique Elements are: “ + uniqueElements);
}
}
2). Print duplicate characters in a string? or Program to Find the Duplicate Characters in a String or Java program to find all duplicate characters in a string or Program to Find Duplicate Characters in a String in Java or Print all the duplicate characters in a string in java 8 or Java program to find the duplicate characters in a string or Print all the duplicate characters in a string in stream api java 8 .
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Remove_Duplicate_Characotr_String_Example{
public static void main(String[] args) {
// Print duplicate characters in a string?
String inputString0 = “Java Concept Of The Day”.replaceAll(“\\s+”, “”).toLowerCase();
Set<String> uniqueChars = new HashSet<>();
Set<String> duplicateChars = Arrays.stream(inputString0.split(“”)).filter(ch -> !uniqueChars.add(ch))
.collect(Collectors.toSet());
System.out.println(duplicateChars);
}
} (Most Imp Question)
3). Count Occurrences of a Char in a String or Count Occurrences of a Char in a String in java 8 or How to count characters in Java 8 ? or How to get the size of string in Java? or How to find the maximum number of characters in a string in Java? or How many characters can a Java String have?or Java Program to count the total number of characters in a string.
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Count_character_Example{
public static void main(String[] args) {
// How many character in String
String inputString = “hi this is aryan singh”;
Map<Character, Long> charCountMap = inputString.chars().mapToObj(c -> (char) c)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(charCountMap);
}
} (Most Imp Question)
4). Reverse a string in Java . or How To Reverse a String in java 8 ? How to Reverse a String in Java? How to Reverse a String in Java Using Different Methods? or Reverse a String in Java 8 stream . or Java Program to find Reverse of the string or How can I reverse a single String in Java 8 using Lambda ? or Java 8 – Reverse complete String using Stream and collections . or What is reverse () in Java? or How do you reverse a number in Java 8? or Can we reverse a string in Java?
(Most Imp Question)
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Reverse_String_Example{
public static void main(String[] args) {
// Reverse of a String
String str = “Aryan Poona”;
int len = str.length();
IntStream.range(0, len).map(i -> len – 1 – i).mapToObj(j -> str.charAt(j)).forEach(System.out::print);
}
}
5). Count the string in java . or Count Occurrences of a Char in a String. or How do you count words in Java 8? or Java Program to Count the Occurrences of Each Character or How to count frequency of a string in java using stream or How to count words in string java Stream? or How to use count in streams Java? or Count occurrence of a given character in a string using stram api.
(Imp Question)
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Count_String_Example{
public static void main(String[] args) {
//How many number os String a list
List<String> stanioryList = Arrays.asList(“Pen”, “Rubber”, “Pencil”, “Rubber”, “Note Book”, “Book”);
Map<String, Long> countStaniroyList = stanioryList.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(countStaniroyList);
}
}
6). Find first repeated character in a string? or find first repeating character in a string using java 8 or Find First Repeated and Non Repeated Character in a String in java ? or How To Find First Repeated in A String Using java8 or Java 8 String streams and finding the first non repeated character with functional programming. or How to find first repeated character in a string in Java? or How to find repeated characters in a string using Java 8? or Java Program To Find First Non-Repeated Character In String
(Most Imp Question)
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Repeated_Character_Example{
public static void main(String[] args) {
// Find first repeated character in a string?
String inputString1 = “Java Concept Of The Day”.replaceAll(“\\s+”, “”).toLowerCase();
Map<String, Long> charCountMap1 = Arrays.stream(inputString1.split(“”))
.collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()));
String firstRepeatedChar = charCountMap1.entrySet().stream().filter(entry -> entry.getValue() > 1)
.map(entry -> entry.getKey()).findFirst().get();
System.out.println(“first repeated character: “ + firstRepeatedChar);
}
}
7). Find first non-repeating character of given String ? Find Non-Repeating Characters In a String In Java in java 8 ? or Find the First Non Repeating Character in a String in Java ? or Java program to Find First non repeating character in a String.
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class First_Non_Reapeat_Character_Example{
public static void main(String[] args) {
// Find first non-repeated character in a string
String inputString2 = “Java Concept Of The Day”.replaceAll(“\\s+”, “”).toLowerCase();
Map<String, Long> charCountMap2 = Arrays.stream(inputString2.split(“”))
.collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()));
String firstNonRepeatedChar = charCountMap2.entrySet().stream().filter(entry -> entry.getValue() == 1)
.map(entry -> entry.getKey()).findFirst().get();
System.out.println(“first non-repeated character: “ + firstNonRepeatedChar);
}
}
8). How to sort String array by length in java ? or Java program Sort an array of strings according to string lengths ? or or How to sort a string on their length in Java ? or How to sort in increasing order Java? or How do you sort a list of strings based on their length?
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Sort_String_Example{
public static void main(String[] args) {
// sort them according to increasing order of their length?
List<String> listOfStrings = Arrays.asList(“Java”, “Python”, “C#”, “HTML”, “Kotlin”, “C++”, “COBOL”, “C”);
listOfStrings.stream().sorted(Comparator.comparing(String::length)).forEach(System.out::println);
}
}
9). Java Program to determine whether two strings are the anagram ? or Java Program to Check if two strings are anagram.
or Check whether two Strings are anagram of each other in java ? or Are two strings anagrams Java? or How do you check if two given string are anagrams?
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class String_Anagram_Example{
public static void main(String[] args) {
// two strings are anagrams or not?
String s1 = “RaceCar”;
String s2 = “CarRace”;
s1 = Stream.of(s1.split(“”)).map(String::toUpperCase).sorted().collect(Collectors.joining());
s2 = Stream.of(s2.split(“”)).map(String::toUpperCase).sorted().collect(Collectors.joining());
if (s1.equals(s2)) {
System.out.println(“Two strings are anagrams”);
} else {
System.out.println(“Two strings are not anagrams”);
}
}
}