[Go to site: main page, start]

0% found this document useful (0 votes)
2 views5 pages

Java 8 Stream API

The document contains Java 8 programming exercises that utilize the Stream API for various tasks, including converting strings to lowercase, finding the first odd number, squaring numbers, converting arrays to lists, and finding distinct elements. It also includes examples of skipping and limiting elements, grouping strings by length, checking for positive numbers, and generating Fibonacci series. Each exercise is accompanied by code snippets and expected output.

Uploaded by

Afsha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Java 8 Stream API

The document contains Java 8 programming exercises that utilize the Stream API for various tasks, including converting strings to lowercase, finding the first odd number, squaring numbers, converting arrays to lists, and finding distinct elements. It also includes examples of skipping and limiting elements, grouping strings by length, checking for positive numbers, and generating Fibonacci series. Each exercise is accompanied by code snippets and expected output.

Uploaded by

Afsha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

‭Java 8 Feature Assignment 05/07/2024‬

‭[Link] a program to convert all the strings to lowercase using the Stream API.‬

i‭mport [Link];‬
‭import [Link];‬
‭public class LowerCase {‬
‭ public static void main(String[] args){‬
‭ List<String> list= [Link]("SNeha","ShewTA","SoUmYa","MaheSHWari");‬
‭ [Link]().map(String::toLowerCase).forEach([Link]::println);‬
‭ }‬
‭}‬

‭O utput:‬

‭ neha‬
s
‭s hewta‬
‭s oumya‬
‭m aheshwari‬

‭[Link] a program to find the first odd number‬

i‭mport [Link];‬
‭import [Link];‬

‭ ublic class FirstOddNum {‬


p
‭ public static void main(String[] args){‬
‭ List<Integer> list= [Link](1,2,3,4,5,67,11,1);‬
‭ [Link]().filter(i->i%2!=0).findFirst().ifPresent([Link]::println);‬
‭ }‬
‭}‬

‭O utput:‬

‭1‬

‭[Link] of numbers in given list‬

i‭mport [Link];‬
‭import [Link];‬

‭ ublic class SqureOfNum {‬


p
‭ public static void main(String[] args){‬
‭ List<Integer> list= [Link](17,18,19,20,12,21,34);‬
‭ [Link]().map(i->i*i).forEach([Link]::println);‬
‭ }‬
‭}‬

‭O utput:‬

‭ 89‬
2
‭324‬
‭361‬
‭400‬
‭144‬
‭441‬
‭1156‬

‭[Link] an array of integers, write a program to convert it to a list using the Stream API.‬

i‭mport [Link];‬
‭import [Link];‬
‭import [Link];‬
‭public class StreamOfArray {‬
‭ public static void main(String[] args){‬
‭ Integer[] array={1,2,3,4,5,6};‬
‭ List<Integer> list= [Link](array).collect([Link]());‬
‭ [Link](list);‬
‭ }‬
‭}‬

‭O utput:‬

‭[1, 2, 3, 4, 5, 6]‬

‭[Link] a list of integers, write a program to find all distinct elements using the Stream API.‬

i‭mport [Link];‬
‭import [Link];‬

‭ ublic class PrintDistinct {‬


p
‭ public static void main(String[] args){‬
‭ List<Integer> list= [Link](15,15,64,74,22,42,64);‬
‭ [Link]().distinct().forEach([Link]::println);‬
‭ }‬
‭}‬

‭O utput:‬

‭15‬
‭ 4‬
6
‭74‬
‭22‬
‭42‬

‭ .Given a list of integers, write a program to skip the first 3 elements and limit the result to the‬
6
‭next 3 elements using the Stream API.‬

i‭mport [Link];‬
‭import [Link];‬
‭import [Link];‬
‭public class LimitAndSkip {‬
‭ public static void main(String[] args){‬
‭ List<Integer> list= [Link](1,0,87,54,76,32,76,88);‬
‭ List<Integer> num=[Link]().skip(3).limit(4).collect([Link]());‬
‭ [Link](num);‬
‭ }‬
‭}‬

‭O utput:‬

‭[54, 76, 32, 76]‬

‭[Link] a list of strings, write a program to group them by their lengths using the Stream API.‬

i‭mport [Link];‬
‭import [Link];‬
‭import [Link];‬
‭import [Link];‬

‭ ublic class GroupByLength {‬


p
‭ public static void main(String[] args){‬
‭ List<String> fruits= [Link]("apple","orange","banana","grapes");‬
‭ Map<Integer,List<String>> groupByFruits =‬
‭[Link]().collect([Link](String::length));‬
‭ [Link](groupByFruits);‬

‭ }‬
‭}‬

‭O utput:‬

‭{5=[apple], 6=[orange, banana, grapes]}‬


‭[Link] a program to find any element that is greater than 4‬

i‭mport [Link];‬
‭import [Link];‬

‭ ublic class FindAnyMatch {‬


p
‭ public static void main(String[] args){‬
‭ List<Integer> list= [Link](11,2,3,4,5,6,7,8,9,10);‬
‭ [Link]().filter(i->i>4).findAny().ifPresent([Link]::println);‬
‭ }‬
‭}‬

‭O utput:‬

‭11‬

‭[Link] a program to check if all elements are positive‬

i‭mport [Link];‬
‭import [Link];‬

‭ ublic class CheckPositiveNum {‬


p
‭ public static void main(String[] args){‬
‭ List<Integer> list1= [Link](1,2,3,4,5);‬
‭ List<Integer> list2=[Link](-1,7,8,5,-4);‬
‭ boolean allPostive= [Link]().allMatch(i->i>0);‬
‭ boolean allPostiveValue=[Link]().allMatch(i->i>0);‬
‭ [Link]("All postive in list1:" + allPostive );‬
‭ [Link]("All postive in list2:" + allPostiveValue );‬
‭ }‬
‭}‬

‭O utput:‬

‭ ll postive in list1:true‬
A
‭All postive in list2:false‬

‭[Link] first 10 fibonacci series‬

i‭mport [Link];‬
‭public class Fibonacci {‬
‭ public static void main(String[] args){‬
‭ [Link](new int[]{0,1}, fib->new int[] {fib[1],fib[0]+fib[1]})‬
‭ .limit(10).map(fib->fib[0])‬
‭ .forEach([Link]::println);‬
‭ }‬
‭}‬
‭O utput:‬

‭‬
0
‭1‬
‭1‬
‭2‬
‭3‬
‭5‬
‭8‬
‭13‬
‭21‬
‭34‬

You might also like