[Go to site: main page, start]

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

Java Streams Assignment Solutions

The document contains a Java assignment focused on practicing synchronization and stream operations. It includes ten questions with code examples that demonstrate filtering, transforming, counting, and manipulating lists of numbers and names using Java Streams. Each question is accompanied by its respective output, showcasing the results of the operations performed.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Java Streams Assignment Solutions

The document contains a Java assignment focused on practicing synchronization and stream operations. It includes ten questions with code examples that demonstrate filtering, transforming, counting, and manipulating lists of numbers and names using Java Streams. Each question is accompanied by its respective output, showcasing the results of the operations performed.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JAVA ASSIGNMENT(STREAMS)

Practice Synchronization method ,block class level and object level


Q1. Filter out all the odd numbers from the numbers list.

ANS: package Stream;

import [Link].*;

import [Link].*;

public class QUE1 {

private static List<Integer> numbers = [Link](1,2,3,4,5,6,7,8,9,10);

public synchronized static void main(String[] args) {

List<Integer> even = [Link]().filter(n -> n % 2 ==


0).collect([Link]());

[Link](even);

OUTPUT: [2, 4, 6, 8, 10]

Q2. Take the numbers list and transform each element into its square root (as a Double).

ANS: package Stream;


import [Link].*;
import [Link].*;
public class QUE2 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
synchronized ([Link]) {
[Link]("Enter numbers:");
String input = [Link]();
List<Integer> numbers = [Link]([Link](" "))
.map(Integer::parseInt)
.collect([Link]());
List<Double> roots = [Link]()
.map(Math::sqrt)
.collect([Link]());
[Link]("Square roots: " + roots);
}
[Link]();
}
}

OUTPUT: Enter numbers:


4 6 9 26 36 64
Square roots: [2.0, 2.449489742783178, 3.0, 5.0990195135927845, 6.0, 8.0]

Q3. Take the names list, convert all strings to uppercase, and then filter to only include
names that start with 'C' (after the uppercase transformation).

ANS: package Stream;


import [Link].*;
import [Link].*;
public class QUE3 {
private List<String> names = [Link]("Alice","Bob","Charlie","Catherine","David");
public void process() {
synchronized(this) {
List<String> result = [Link]()
.map(String::toUpperCase)
.filter(n -> [Link]("C"))
.collect([Link]());
[Link](result);
}
}
public static void main(String[] args) {
new QUE3().process();
}
}

OUTPUT: [CHARLIE, CATHERINE]

[Link] every element from the numbers list to the console, followed by a
space, without using a traditional for loop.

ANS: package Stream;


import [Link].*;
public class QUE4 {
private static List<Integer> numbers = [Link](1,2,3,4,5,6);
public static void main(String[] args) {
synchronized([Link]) {
[Link](n -> [Link](n + " "));
}
}
}

OUTPUT: 1 2 3 4 5 6

[Link] how many names in the names list are longer than 5 characters.

ANS: package Stream;


import [Link].*;
import [Link].*;
public class QUE5 {
private static List<String> names =
[Link]("Alice","Bob","Catherine","Charlie","Danielle");
public static void main(String[] args) {
synchronized([Link]) {
long count = [Link]().filter(n -> [Link]() > 5).count();
[Link](count);
}
}
}
OUTPUT: 3

[Link] a list with duplicates, return a new list containing only the unique elements.

ANS: package Stream;


import [Link].*;
import [Link].*;
public class QUE6 {
private static List<Integer> numbers = [Link](1,2,2,3,3,4,5,5);
public static void main(String[] args) {
synchronized([Link]) {
List<Integer> unique = [Link]().distinct().collect([Link]());
[Link](unique);
}
}
}

OUTPUT: [1, 2, 3, 4, 5]
[Link] the names list in its natural alphabetical order.

ANS: package Stream;


import [Link].*;
import [Link].*;
public class QUE7 {
private static List<String> names = [Link]("Charlie","Alice","Bob","David");
public static synchronized void main(String[] args) {
List<String> sorted = [Link]().sorted().collect([Link]());
[Link](sorted);
}
}

OUTPUT: [Alice, Bob, Charlie, David]

[Link] only the first 3 elements of the numbers list. using limit.

ANS: package Stream;


import [Link].*;
import [Link].*;
public class QUE8 {
private static List<Integer> numbers = [Link](10,20,30,40,50);
public static void main(String[] args) {
synchronized([Link]) {
[Link]().limit(3).forEach(n -> [Link](n + " "));
}
}
}

OUTPUT: 10 20 30

[Link] the product (multiplication) of all numbers in the numbers list using reduce().

ANS: package Stream;


import [Link].*;
import [Link].*;
public class QUE9 {
private static List<Integer> numbers = [Link](2,3,4,5);
public static void main(String[] args) {
synchronized([Link]) {
int product = [Link]().reduce(1, (a,b) -> a*b);
[Link](product);
}
}
}

OUTPUT: 120

[Link] if any number in the numbers list is greater than 50. (any Match).

ANS: package Stream;


import [Link].*;
import [Link].*;
public class QUE10 {
private static List<Integer> numbers = [Link](10,20,30,60,70);
public static void main(String[] args) {
synchronized([Link]) {
boolean result = [Link]().anyMatch(n -> n > 50);
[Link](result);
}
}
}

OUTPUT: true

You might also like