Wednesday, September 4, 2024

Stream API Coding Interview-2

 String API Operations On List Of Integers.

Q- Convert int array to integer list java 8.

package com.demo.example;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class IntegerStreamExample {
	public static void main(String[] args) {
		// Int Array to Integer List
		int[] array = { 1, 3, 5, 2, 4, 9, 4 };
		List<Integer> integerList = Arrays.stream(array).boxed().collect(Collectors.toList());
		System.out.println("Integer List : " + integerList.toString());

		// Print integer list
		System.out.println("###### Way-1 :: Print integer list element ##########");
		Arrays.stream(array).boxed().collect(Collectors.toList()).forEach(System.out::println);

		// Print integer list
		System.out.println("###### Way-2 :: Print integer list element ##########");
		Arrays.stream(array).boxed().collect(Collectors.toList()).forEach(e -> System.out.println(e));
	}
}
// Output
Integer List : [1, 3, 5, 2, 4, 9, 4]
###### Way-1 :: Print integer list element ##########
1
3
5
2
4
9
4
###### Way-2 :: Print integer list element ##########
1
3
5
2
4
9
4

Q- How to convert integer[] to int[] in java

package com.demo.example;

import java.util.Arrays;

public class IntegerStreamExample {
	public static void main(String[] args) {
		// Convert Integer[] to int[]
		Integer[] array = { 1, 3, 5, 2, 4, 9, 4 };
		int[] intArray = Arrays.stream(array).mapToInt(Integer::intValue).toArray();
		System.out.println("Int Array : " + Arrays.toString(intArray));
	}
}
// Output
Int Array : [1, 3, 5, 2, 4, 9, 4]

Q- Convert list of integers to int[] in java.

package com.demo.example;

import java.util.Arrays;
import java.util.List;

public class IntegerStreamExample {
	public static void main(String[] args) {
		
		//Convert -  List of Integers to int[]
		List<Integer> integerList = Arrays.asList( 1, 3, 5, 2, 4, 9, 4 );
		int[] intArray = integerList.stream().mapToInt(Integer::intValue).toArray();
		System.out.println("Int Array : " + Arrays.toString(intArray));
		
		// Print int[]
		// Way -1
		integerList.stream().mapToInt(Integer::intValue).forEach(System.out::println);
		
		// Way -2
		integerList.stream().mapToInt(Integer::intValue).forEach(e->System.out.println(e));
		
		// Way -3
		Arrays.stream(intArray).forEach(System.out::println);
	}
}
// Output
Int Array : [1, 3, 5, 2, 4, 9, 4]

Q- Intersection of two lists using Java 8 stream api.

package com.demo.example;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class IntegerStreamExample {
	public static void main(String[] args) {
		List<Integer> list1 = Arrays.asList(1, 7, 3, 9, 5);
		List<Integer> list2 = Arrays.asList(3, 4, 5, 6, 7);
		List<Integer> intersection = list1.stream().filter(list2::contains).collect(Collectors.toList());
		System.out.println(intersection.toString());
	}
}
// Output
[7, 3, 5]

Q- Remove duplicates elements from a list of integers using Java 8 streams

package com.demo.example;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class IntegerStreamExample {
	public static void main(String[] args) {

		List<Integer> listWithDuplicates = Arrays.asList(1, 2, 9, 2, 8, 1, 5, 9, 5);
		List<Integer> uniqueElements = listWithDuplicates.stream().distinct().collect(Collectors.toList());
		System.out.println("Unique Elements : " + uniqueElements.toString());
	}
}
// Output
Unique Elements : [1, 2, 9, 8, 5]

Q- Remove duplicates elements from a list of integers OR  int[] using Java 8 streams

package com.demo.example;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class IntegerStreamExample {
	public static void main(String[] args) {

		// Return list of unique elements
		int[] arrWithDuplicates = {1, 2, 9, 2, 8, 1, 5, 9, 5};
		List<Integer> uniqueElements = Arrays.stream(arrWithDuplicates).boxed().distinct().collect(Collectors.toList());
		System.out.println("Unique Elements List : " + uniqueElements.toString());
		
		// Return array of unique elements
		int[] uniqueElementsArr = Arrays.stream(arrWithDuplicates).boxed().mapToInt(i->i).distinct().toArray();
		System.out.println("Unique Elements Array : " + Arrays.toString(uniqueElementsArr));
	}
}
// Output
Unique Elements List : [1, 2, 9, 8, 5]
Unique Elements Array : [1, 2, 9, 8, 5]

Q- Find the duplicate element in an array   OR List using Java 8 stream.

int[] arr = { 9, 3, 9, 3, 9, 7, 9 };
System.out.println("## Int Array : " + Arrays.toString(arr));

// Collect duplicate elements in Array
int[] duplicateElements = Arrays.stream(arr).boxed()
			.collect(Collectors.groupingBy(Function.identity(), Collectors.counting())).entrySet().stream()
			.filter(ent -> ent.getValue() % 2 == 0).mapToInt(ent -> ent.getKey()).toArray();
System.out.println("## Duplicate Elements Array : " + Arrays.toString(duplicateElements));

// Collect duplicate elements in List
List<Integer> duplicateElementsList = Arrays.stream(arr).boxed()
			.collect(Collectors.groupingBy(Function.identity(), Collectors.counting())).entrySet().stream()
			.filter(ent -> ent.getValue() % 2 == 0).mapToInt(ent -> ent.getKey()).boxed()
			.collect(Collectors.toList());
System.out.println("## DuplicateElements List : " + duplicateElementsList);

// Way-1 :: Find duplicate elements from Array
System.out.println("## Way-1 : duplicate elements ##");
Arrays.stream(arr).boxed().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())).entrySet()
			.stream().filter(ent -> ent.getValue() % 2 == 0).forEach(e -> System.out.println(e.getKey()));

// Way-2 :: Find duplicate elements from Array
System.out.println("### Way-2 : duplicate elements ##");
Arrays.stream(arr).boxed().collect(Collectors.groupingBy(e -> e, Collectors.counting())).entrySet().stream()
		.filter(ent -> ent.getValue() % 2 == 0).forEach(e -> System.out.println(e.getKey()));

// Way-3 :: Find duplicate elements from List using HshSet
List<Integer> duplicateList = Arrays.asList(9, 3, 9, 3, 9, 7, 9);
Set<Integer> set1 = new HashSet();
List<Integer> duplicateEle = duplicateList.stream().filter(n -> !set1.add(n)).distinct()
		.collect(Collectors.toList());
System.out.println("## Duplicate elements List Using Set : " + duplicateEle);
// Output
## Int Array : [9, 3, 9, 3, 9, 7, 9]
## Duplicate Elements Array : [3, 9]
## DuplicateElements List : [3, 9]
## Way-1 : duplicate elements ##
3
9
### Way-2 : duplicate elements ##
3
9
## Duplicate elements List Using Set : [9, 3]

Q- Finding Odd Occurrence of a Number in array  Java 8 stream

package com.demo.example;

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

public class IntegerStreamExample {
	public static void main(String[] args) {

		int[] arr = { 9, 3, 9, 3, 9, 7, 9 };
		// using stream API
		Arrays.stream(arr).boxed().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())).entrySet()
				.stream().filter(ent -> ent.getValue() % 2 == 1).forEach(e -> System.out.println(e.getKey()));
	}
}
// Output
7

Q- Find the nth smallest element from an array using Java 8 stream.

package com.demo.example;

import java.util.Arrays;

public class IntegerStreamExample {
	public static void main(String[] args) {

		/*
		 * with Duplicate and unsorted Array : 1. make the array unique. 2. Sort array.
		 * 3. skip n-1 elements. 4. find first element
		 */
		int[] withDuplicateArray = { 9, 3, 9, 3, 9, 7, 9, 10, 12, 18 };
		int[] uniqueSortedElements = Arrays.stream(withDuplicateArray).boxed().mapToInt(i -> i).distinct().sorted()
				.toArray();
		System.out.println(" Unique Sorted Array : " + Arrays.toString(uniqueSortedElements));

		// nth element withDuplicateArray
		int n = 3; // Find the 3rd smallest element
		int nthSmallest = Arrays.stream(withDuplicateArray).boxed().mapToInt(i -> i).distinct().sorted().skip(n - 1)
				.findFirst().orElse(-1);
		System.out.println(" nth Smallest number : " + nthSmallest);

		// nth element withUniqueArray
		System.out.println("########### nth element withUniqueArray ######");
		int[] withUniqueArray = { 1, 2, 3, 7, 9, 10, 12, 18 };
		System.out.println(" Print UniqueArray : " + Arrays.toString(withUniqueArray));
		int n1 = 3; // Find the 3rd smallest element
		int nthSmallest2 = Arrays.stream(withUniqueArray).sorted().skip(n1 - 1).findFirst().orElse(-1);
		System.out.println(" nth Smallest number : " + nthSmallest2);
	}
}
// Output
 Unique Sorted Array : [3, 7, 9, 10, 12, 18]
 nth Smallest number : 9
########### nth element withUniqueArray ######
 Print UniqueArray : [1, 2, 3, 7, 9, 10, 12, 18]
 nth Smallest number : 3

Q - Find Max/Min/Avg from int[] Array.

// Find Max/Min/Avg from int[] Array
		int[] array = { 4, 2, 7, 1, 5, 3, 6 };
		int maxElement = Arrays.stream(array).max().getAsInt();
		System.out.println("Max Element from int array : " + maxElement);

		int minElement = Arrays.stream(array).min().getAsInt();
		System.out.println("Min Element from int array : " + maxElement);

		double avgOfArrayElements = Arrays.stream(array).average().getAsDouble();
		System.out.println("avgOfArrayElements : " + avgOfArrayElements);
// Output
Max Element from int array : 7
Min Element from int array : 7
avgOfArrayElements : 4.0

No comments:

Post a Comment