Program to remove duplicate elements from String?
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class DuplicateElementsString {
public static void main(String[] args) {
String s="javalanguage";
List<String> duplicateElements=Arrays.stream(s.split(""))
.collect(Collectors.groupingBy(Function.identity(),Collectors.counting()))
.entrySet().stream().filter(x->x.getValue()>1)
.map(Map.Entry::getKey).collect(Collectors.toList());
System.out.println(duplicateElements);
List<String> uniqueElements=Arrays.stream(s.split(""))
.collect(Collectors.groupingBy(Function.identity(),Collectors.counting()))
.entrySet().stream().filter(x->x.getValue()==1)
.map(Map.Entry::getKey).collect(Collectors.toList());
System.out.println(uniqueElements);
}
}
Program to print Even Numbers?
import java.util.Arrays;
import java.util.List;
public class EvenNumbersUsingStreams {
public static void main(String[] args) {
List<Integer> list=Arrays.asList(10,20,15,12,19,17);
list.stream().filter(n->n%2==0).forEach(System.out::println);
}
}
Program to find duplicate elements in String?
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class FindDuplicateElements {
public static void main(String args[])
{
List<Integer> list=Arrays.asList(10,20,12,13,12,20,10,90,70);
Set<Integer> set=new HashSet();
list.stream()
.filter(n->!set.add(n))
.forEach(System.out::println);
}
}
Program to find find first element?
import java.util.Arrays;
import java.util.List;
public class FindFirstElement {
public static void main(String[] args) {
List<Integer> list=Arrays.asList(1,2,3,4,5);
list.stream().findFirst().ifPresent(System.out::println);
}
}
Program to find first Non Repeated Element in String?
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;
public class FirstNonRepeatedElement {
public static void main(String[] args) {
String s="javajavalang";
String firstNonrepeatedChar=Arrays.stream(s.split(""))
.collect(Collectors.groupingBy(Function.identity(),LinkedHashMap::new,Collectors.counting()))
.entrySet().stream().filter(x->x.getValue()==1)
.findFirst().get().getKey();
System.out.println(firstNonrepeatedChar);
}
}
Program to find max value?
import java.util.Arrays;
import java.util.List;
public class MaxValue {
public static void main(String[] args) {
List<Integer> list=Arrays.asList(10,2,3,30,7,8);
int max=list.stream().max(Integer::compare).get();
System.out.println(max);
}
}
Program to display numbers starting with 1?
import java.util.Arrays;
import java.util.List;
public class NumbersStartingWith1 {
public static void main(String[] args) {
List<Integer> list=Arrays.asList(10,20,12,13,30,40);
list.stream()
.map(s->s+"") //converts integer to String
.filter(s->s.startsWith("1"))
.forEach(System.out::println);
}
}
Program to find occurance of each char in a String?
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class OccuranceOfEachChar {
public static void main(String[] args) {
String s="java";
//convert string to array
//String str[]=s.split("");
//System.out.println(Arrays.toString(str));
Map<String,List<String>> result=Arrays.stream(s.split("")).collect(Collectors.groupingBy(s1->s1));
System.out.println(result); //{a=[a, a], v=[v], j=[j]}
Map<String,Long> charCount=Arrays.stream(s.split("")).collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));
System.out.println(charCount);//{a=2, v=1, j=1}
}
}
Program to find 2nd Highest number in array?
import java.util.Arrays;
import java.util.Comparator;
public class SecondHighestNumberArray {
public static void main(String[] args) {
int nums[]= {3,1,10,7,5};
Integer secondHighest=Arrays.stream(nums).boxed().sorted(Comparator.reverseOrder()).skip(1).findFirst().get();
System.out.println(secondHighest);
}
}
Program to find total number of elements?
import java.util.Arrays;
import java.util.List;
public class TotalNumberofElements {
public static void main(String[] args) {
List<Integer> list=Arrays.asList(1,2,3,4,5);
long count=list.stream().count();
System.out.println(count);
}
}
Comments
Post a Comment