Q 1) Write A Java 8 Program To Find Out Maximum Element From An Array?
import java.util.Arrays;
public class MaximumElementOfArray {
// How to find Maximum Element of An Array
public static void main(String[] args) {
int arr[] = { 10, 5, 6, 78, 23, 42 };
int max = Arrays.stream(arr).max().getAsInt();
System.out.println("The Maximum Element Of An Array:" + max);
}
}
Q 2 ) Write A Java 8 Program To Find Out A Counting Each character Occurances From A String?
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Java8CharOccurance {
// Write A java 8 program for counting occrances of Each Character In String
public static void main(String[] args) {
String str = "Welcome To Java Welcome To Programming";
IntStream charStream = new String(str).chars();
Map<Character, Long> map=charStream.mapToObj(c -> (char)c).map(Character::toLowerCase).
collect(Collectors.groupingBy(Function.identity(), HashMap::new,Collectors.counting()));
map.forEach((key, value) -> System.out.println("Chaacter :" + key + "Occurnace" + value));
}
}
Q 3 ) Create Your Own Immutable Class In Java ? (Asked In Java Developer Interview)?
// Mark Class As Final
public final class Immutable {
// Make All Parameters As Private
private int id;
private String name;
private String mobileNo;
private String age;
// Make Constructor
public Immutable(int id, String name, String mobileNo, String age) {
this.id = id;
this.name = name;
this.mobileNo = mobileNo;
this.age = age;
}
// Create All Fields As Getters
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getMobileNo() {
return mobileNo;
}
public String getAge() {
return age;
}
@Override
public String toString() {
return "Immutable [id=" + id + ", name=" + name + ", mobileNo=" + mobileNo + ", age=" + age + "]";
}
}
public class ImmutableCalling {
public static void main(String[] args) {
Immutable obj1 = new Immutable(1, "Sandeep", "8087365306", "30");
System.out.println(obj1);
}
}
Q 4) Given a String, find first non-repeated character in it using Stream
functions.
import java.util.LinkedHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;
public class FirstNonRepeatedCharacter {
// Given a String, find first non-repeated character in it using Stream
// functions.
public static void main(String[] args) {
String str = "Welcome To Java";
Character result = str.chars().mapToObj(s -> Character.toLowerCase(Character.valueOf((char) s)))
.collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()))
.entrySet()
.stream()
.filter(entry -> entry.getValue() == 1L)
.map(entry -> entry.getKey())
.findFirst()
.get();
System.out.println("The First Non-Repeating Character in String is:" + result);
}
}
Q 5) Given list of integers. Find Out even Numbers from A list
import java.util.Arrays;
import java.util.List;
public class EvenNumbers {
// Given list of integers. Find Out even Numbers from A list
public static void main(String[] args) {
List<Integer> list = Arrays.asList(10, 12, 8, 49, 75, 3, 6); // Take A List
list.stream().filter(n -> n % 2 == 0).forEach(System.out::println); // On Stream we are using intermediate operations
// Intermediate operational methods does not produce any results
// They usually accepts the functional interface as parameters and always returns a new Stream.
}
}
Q 6) Find Out the Duplicate Elements From A list?
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class DuplicateIntegers {
// Find Out the Duplicate Elements From A list
public static void main(String[] args) {
List<Integer> list=Arrays.asList(10,15,24,7,24,74,10,7);
Set<Integer> set = new HashSet<>();
// Hashset Object does not allow duplicates and doesnot preserve insertion order.
list.stream().filter(n -> !set.add(n)).forEach(System.out::println);
}
}
Q 7) Consider the Array. If it contains the repeated elements then it returns true
And If it contains distinct elements then return false
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.Collectors;
public class DuplicateElementFound {
// Consider the Array. If it contains the repeated elements then it returns true
// And If it contains distinct elements then return false
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of an Array :");
int size = sc.nextInt();
int arr[] = new int[size];
System.out.println("Enter the element of An Array:");
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
System.out.println("Entered Array:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ",");
}
System.out.println();
boolean flag=DuplicateElementFound.duplicateFound(arr);
if(flag == true)
{
System.out.println("The Elements are Not Distince In Array");
}
else{
System.out.println("Elements are Distinct");
}
sc.close();
}
public static boolean duplicateFound(int arr[])
{
List<Integer> list = Arrays.stream(arr).boxed().collect(Collectors.toList());
Set<Integer> set = new HashSet<>(list);
if(set.size() == list.size())
{
return false;
}
return true;
}
}
Q 8) Java 8 Program for Counting Duplicate Elements From the String.
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class DuplicateElementCount {
// Java 8 Program for Counting Duplicate Elements From the String.
public static void main(String[] args) {
List<String> names = Arrays.asList("AA", "BB", "CC", "AA", "CC");
Map<String, Long> namesCount = names.stream().filter(x -> Collections.frequency(names, x) > 1)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(namesCount);
}
}
Q 9) How will you get Current Time And Date Using Java 8 Date And Time API
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class CurrentDateAndTime {
// How will you get Current Time And Date Using Java 8 Date And Time API
public static void main(String[] args) {
// Using LocalDate API to get the Date
System.out.println("Current Local Date :" + LocalDate.now());
// Using LocalTime API to get current Time
System.out.println("Current Local Time :" + LocalTime.now());
//Using LocalDateTime API to get the Local Date And Time
System.out.println("Current Date and Time: " + LocalDateTime.now());
}
}
Q 10) Write A Java 8 program to perform cube on list elements and filter numbers which
are greater than 50.
import java.util.List;
import java.util.stream.Collectors;
public class CubeOfElements {
// Write A Java 8 program to perform cube on list elements and filter numbers which
// are greater than 50.
public static void main(String[] args) {
List<Integer> list=List.of(1,2,3,4,5,6,7);
list.stream().map( e -> e*e*e ).filter(element -> (element > 50)).collect(Collectors.toList()).forEach(System.out::println);
}
}
Q 11) Count the words inside the String Using Java 8 Program.
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
public class CountStringWords {
// Count the words inside the String Using Java 8 Program.
public static void main(String[] args) {
String str = " Welcome To Java Welcome To Programming"; // Given String is here
String arr[] = str.split(" "); // We are using spilt method for conversion of String into arr
List<String> words = Arrays.asList(arr); // String arr converted into list
// Apply Stream() function on list for creation of Stream Of Strings.
Map<String, Long> wordCouuMap = words.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
Set<Map.Entry<String, Long>> entrySet = wordCouuMap.entrySet();
for (Map.Entry<String, Long> entry : entrySet) {
String key = entry.getKey();
Long value = entry.getValue();
System.out.println("Word :" + key + " , Count :" + value);
}
}
}
Q 12) Write A Java Program To Count Each Word / Element From the 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 CountEachWordFromString {
// Write A Java Program To Count Each Word / Element From the String
public static void main(String[] args) {
List<String> names=Arrays.asList("AA","BB","AA","CC", "BB","BB");
Map<String, Long> namesCount =names.stream().collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));
System.out.println(namesCount);
}
}
Q 13 ) Write a Java Program to Count Each Character In A String.
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class CountEachCharacterString {
// Write a Java Program to Count Each Character In A String.
public static void main(String[] args) {
String str = "Welcome To Java Welcome To Programming";
Map<Character, Long> charMapCount = str.chars().mapToObj(c -> (char) c)
.filter(Character::isLetterOrDigit)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
charMapCount.forEach((character, count) -> {
System.out.println("Character: " + character + ", count: " + count);
});
}
}
Q 14) Write a java Program to concatenate Two Streams.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class ConcatenateTwoStreams {
// Write a java Program to concatenate Two Streams.
public static void main(String[] args) {
List<String> list1 = Arrays.asList("Java", "8");
List<String> list2 = Arrays.asList("explained", "through", "programs");
//Concatenated the list1 and list2 by concatenating into Stream
Stream<String> concatStream = Stream.concat(list1.stream(), list2.stream());
// Printing the Concatenated Streams.
concatStream.forEach( str -> System.out.print(str + " "));
}
}
Q 15 ) Write a Java Program To Find Out the Character Occurnaces In A String.
import java.util.HashMap;
import java.util.Map;
public class CharOccurnacesInString {
// Write a Java Program To Find Out the Character Occurnaces In A String.
public static void main(String[] args) {
String str = "I am Sandeep Aswar";
str = str.replaceAll(" ", "").toLowerCase();
char[] ch = str.toCharArray();
Map<Character, Integer> charMap = new HashMap<>();
for (char c : ch) {
if (charMap.containsKey(c)) {
charMap.put(c, charMap.get(c) + 1);
} else {
charMap.put(c, 1);
}
}
for (Map.Entry<Character, Integer> entry : charMap.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
}
}
Comments
Post a Comment