0% found this document useful (0 votes)
4 views6 pages

Assignment Java8features

The document contains a series of Java 8 programming assignments that demonstrate various features of the Stream API, including filtering, sorting, and finding elements in lists. Each assignment includes code snippets and their respective outputs, covering tasks such as finding numbers starting with 1, identifying duplicates, and printing the first 10 natural numbers. Additionally, it showcases operations like counting elements, sorting in ascending and descending order, and finding common elements between two lists.

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)
4 views6 pages

Assignment Java8features

The document contains a series of Java 8 programming assignments that demonstrate various features of the Stream API, including filtering, sorting, and finding elements in lists. Each assignment includes code snippets and their respective outputs, covering tasks such as finding numbers starting with 1, identifying duplicates, and printing the first 10 natural numbers. Additionally, it showcases operations like counting elements, sorting in ascending and descending order, and finding common elements between two lists.

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 Features Assignment‬ ‭04/07/2024‬

‭1. Given a list of integers, find out all the numbers starting with 1 using Stream functions‬

i‭mport [Link].*;‬
‭public class StartsWithInteger {‬
‭ public static void main(String args[]) {‬
‭ List<Integer> myList = [Link](10,15,8,49,25,98,32);‬
‭ [Link]()‬
‭ .map(s -> s + "")‬
‭ .filter(s -> [Link]("1"))‬
‭ .forEach([Link]::println);‬
‭ }‬
‭ }‬

‭O utput:‬

‭ 0‬
1
‭15‬

‭[Link] duplicate elements in a given integers list‬

i‭mport [Link].*;‬
‭public class FindDuplicates {‬
‭ public static void main(String[] args){‬
‭ List<Integer> myList= [Link](10,32,34,23,32,11,10,54,37,88);‬
‭ Set<Integer> set=new HashSet<>();‬
‭ [Link]().filter(n->![Link](n))‬
‭ .forEach([Link]::println);‬

‭ }‬
‭}‬

‭O utput:‬

‭ 2‬
3
‭10‬
‭[Link] the first element of the list‬

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

‭ ublic class FindFirstElement {‬


p
‭ public static void main(String[] args){‬
‭ List<Integer> myList= [Link](2,8,20,1,76,45,32,54,66);‬
‭ [Link]().findFirst().ifPresent([Link]::println);‬

‭ }‬
‭}‬

‭O utput:‬

‭2‬

‭[Link] the total number of elements present in the list‬

i‭mport [Link];‬
‭import [Link];‬
‭public class CountNumberOfElements {‬
‭ public static void main(String[] args){‬
‭ List<Integer> myList= [Link](10,3,87,33,19,45,22,11,768949);‬
‭ long count=[Link]().count();‬
‭ [Link](count);‬
‭ List<String> names=[Link]("Sneha","Shweta","Srusti","Shreya");‬
‭ long countNames=[Link]().count();‬
‭ [Link](countNames);‬
‭ }‬

‭}‬

‭O utput:‬

‭‬
9
‭4‬
‭[Link] a list of integers, sort all the values present in it‬

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

‭ ublic class SortElements {‬


p
‭ public static void main(String[] args){‬
‭ List<Integer> list= [Link](1,87,65,43,67,55,44,821,890,32);‬
‭ [Link]().sorted().forEach([Link]::println);‬
‭ }‬
‭}‬

‭O utput:‬

‭‬
1
‭32‬
‭43‬
‭44‬
‭55‬
‭65‬
‭67‬
‭87‬
‭821‬
‭890‬

‭[Link] all the values present in it in descending order‬

i‭mport [Link];‬
‭import [Link];‬
‭import [Link];‬
‭public class ReverseOrderSort {‬
‭ public static void main(String[] args){‬
‭ List<Integer> list= [Link](19,67,5443,293,45,67,21,55);‬
‭ [Link]().sorted([Link]())‬
‭ .forEach([Link]::println);‬

‭ }‬
‭}‬

‭O utput:‬

‭ 443‬
5
‭293‬
‭67‬
‭67‬
‭ 5‬
5
‭45‬
‭21‬
‭19‬

‭[Link] current date and time‬

‭ ublic class DateAndTime {‬


p
‭ public static void main(String[] args){‬
‭ [Link]("Current date : "+[Link]());‬
‭ [Link]("Current time :"+[Link]());‬
‭ [Link]("current date and time : "+[Link]());‬
‭ }‬
‭}‬

‭O utput:‬

‭ urrent date : 2024-07-04‬


C
‭Current time :22:45:45.248845100‬
‭c urrent date and time : 2024-07-04T22:45:45.248845100‬

‭[Link] cube on list elements and filter numbers greater than 100.‬

i‭mport [Link];‬
‭import [Link];‬
‭public class CubeTheNumber {‬
‭ public static void main(String[] args){‬
‭ List<Integer> myList= [Link](16,17,18,3,5,21,56,23,1,2,7);‬
‭ [Link]().map(i->i*i*i)‬
‭ .filter(i->i>100)‬
‭ .forEach([Link]::println);‬
‭ }‬
‭}‬

‭O utput:‬

‭ 096‬
4
‭4913‬
‭5832‬
‭125‬
‭9261‬
‭175616‬
‭12167‬
‭343‬

‭[Link] the second largest element in given list‬

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

‭ ublic class SecondLargest {‬


p
‭ public static void main(String[] args){‬
‭ List<Integer> myList= [Link](19,13,54,67,48,20);‬
‭ [Link]().sorted([Link]())‬
‭ .skip(1).findFirst()‬
‭ .ifPresent([Link]::println);‬

‭ }‬
‭}‬
‭output:‬
‭54‬

‭[Link] common elements in two lists‬

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

‭ ublic class CommonElementsInArray {‬


p
‭ public static void main(String[] args){‬
‭ List<Integer> list1= [Link](18,16,55,43,29,33,97);‬
‭ List<Integer> list2=[Link](11,76,33,23,43,29,97,54);‬
‭ [Link]().filter(list2::contains).forEach([Link]::println);‬
‭ }‬
‭}‬

‭ utput:‬
o
‭43‬
‭29‬
‭33‬
‭97‬

‭[Link] First 10 Natural numbers‬

‭import [Link];‬

‭ ublic class NaturalNumbers {‬


p
‭ public static void main(String[] args){‬
‭ [Link](1,11).forEach([Link]::println);‬
‭ }‬
‭}‬

‭O utput:‬

‭‬
1
‭2‬
‭3‬
‭4‬
‭5‬
‭6‬
‭7‬
‭8‬
‭9‬
‭10‬

You might also like