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

Unit 4

This document covers key concepts of Java's Collection API and Lambda expressions, including wrapper classes, autoboxing, unboxing, and the various types of collections such as List, Set, and Map. It explains functional interfaces, lambda expressions, and the Stream API, detailing their syntax and usage in Java. Additionally, the document includes a series of questions and topics for further exploration and understanding of these concepts.
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)
12 views6 pages

Unit 4

This document covers key concepts of Java's Collection API and Lambda expressions, including wrapper classes, autoboxing, unboxing, and the various types of collections such as List, Set, and Map. It explains functional interfaces, lambda expressions, and the Stream API, detailing their syntax and usage in Java. Additionally, the document includes a series of questions and topics for further exploration and understanding of these concepts.
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
You are on page 1/ 6

UNIT IV – COLLECTION API AND LAMBDA

PART - A

1. What is a wrapper class in Java?

A wrapper class in Java is used to convert primitive data types into objects. Java provides
wrapper classes for all eight primitive types (e.g., int → Integer, char → Character). These
classes enable the use of primitives in collections (like ArrayList) and provide utility methods
for type conversion and manipulation.

2. Name any four predefined wrapper classes in Java.

Integer for int

• Double for double


• Boolean for boolean
• Character for char

These are part of the java.lang package and provide methods like parseInt(), compareTo(),
and more.

3. What is autoboxing in Java?

Autoboxing is the automatic conversion of a primitive type to its corresponding wrapper class
object by the Java compiler.

Example:
int a = 10;
Integer b = a; // autoboxing

4. What is unboxing in Java?

Unboxing is the reverse of autoboxing — it is the automatic conversion of a wrapper class


object back to a primitive type.

Example:

Integer a = 20;

int b = a; // unboxing

5. Why are wrapper classes needed in Java?

Wrapper classes are needed to:

- Use primitive types in collections (e.g., ArrayList<Integer>)

- Convert between types (e.g., Integer.parseInt())

- Enable object-based operations on primitives


6. What is Java Collections API?

The Java Collections API is a set of classes and interfaces that implement commonly reusable
data structures like Lists, Sets, and Maps. It allows efficient storage, retrieval, and
manipulation of data in collections.

7. What is the purpose of the Collection interface?

The Collection interface is the root interface for most collection classes. It defines basic
operations like add(), remove(), clear(), size(), and is extended by interfaces such as List, Set,
and Queue.

8. What is a generic in Java?

Generics allow classes, interfaces, and methods to operate on types specified by the
programmer at runtime, improving type safety and reusability.

Example:

ArrayList<String> list = new ArrayList<>();

9. Write the syntax to create a generic ArrayList of Strings.

ArrayList<String> names = new ArrayList<String>();

This ensures that only String objects can be added to the list.

10. Differentiate between List and Set.

List: Allows duplicate elements and maintains insertion order (e.g., ArrayList, LinkedList).

Set: Does not allow duplicates and may not maintain order (e.g., HashSet, TreeSet).

11. What are the types of Lists in Java Collections?

• ArrayList: Resizable array implementation


• LinkedList: Doubly linked list
• Vector: Thread-safe array-based list
• Stack: Last-in-first-out list built on Vector

12. What are the types of Sets in Java?

• HashSet: Unordered set with no duplicates


• LinkedHashSet: Maintains insertion order
• TreeSet: Sorted set

13. What is a Map in Java?

A Map stores key-value pairs where each key is unique and maps to a specific value. It is not
part of the Collection interface but is part of the collections framework.
14. How does HashMap work?

HashMap uses a hash table to store key-value pairs. Keys are hashed to determine their
bucket location. Collisions are handled using chaining (linked list or tree).

15. What is a functional interface?

A functional interface in Java has exactly one abstract method. They can have default and
static methods and are used primarily with lambda expressions.

Example: Runnable, Predicate<T>

16. Name any two built-in functional interfaces.

• Predicate<T>: Represents a boolean-valued function


• Function<T, R>: Takes input of type T and returns type R
• Others: Supplier<T>, Consumer<T>

17. What is a lambda expression?

A lambda expression is a concise way to represent a method using an expression. It enables


functional programming features by providing implementations for functional interfaces.

Syntax: (parameters) -> expression

18. What is the syntax of a lambda expression?

(parameter1, parameter2) -> { body }

Example:

(x, y) -> x + y

19. What is the use of the Predicate interface?

Predicate<T> represents a function that takes a single argument and returns a boolean. It is
commonly used for filtering collections.

Example: list.stream().filter(x -> x > 10);

20. What is the use of Consumer interface?

Consumer<T> accepts a single input and returns no result. It is used to perform operations
like printing or modifying objects.

Example:

Consumer<String> print = x -> System.out.println(x);


21. What is a Supplier in Java?

Supplier<T> is a functional interface that represents a function that supplies a value without
taking any input.

Example:

Supplier<String> s = () -> "Hello";

22. What is the Function<T, R> interface used for?

Function<T, R> represents a function that takes an input of type T and returns a result of type
R.

Example:

Function<Integer, String> f = x -> "Value: " + x;

23. What is the Stream API in Java?

The Stream API provides a high-level abstraction for processing sequences of elements. It
supports functional-style operations such as map(), filter(), and reduce() on collections.

24. What does the filter() method in Stream do?

It filters elements of the stream based on a condition (predicate).

Example:

list.stream().filter(x -> x > 10)

25. What is the use of sorted() in streams?

The sorted() method sorts the elements of the stream in natural or custom order.

Example:

list.stream().sorted().forEach(System.out::println);

26. What is map() in Stream API?

The map() function transforms each element of the stream using a given function.

Example:

list.stream().map(x -> x * 2)

27. What is reduce() used for in Java streams?

reduce() combines elements of a stream into a single result using a binary operation (e.g.,
sum, max).
Example:

list.stream().reduce(0, (a, b) -> a + b)

28. What does count() return in streams?

count() returns the number of elements in the stream.

Example:

long c = list.stream().filter(x -> x > 10).count();

29. What is a parallel stream?

A parallel stream divides the source into multiple parts and processes them in parallel using
multiple threads, which can speed up execution on multicore systems.

30. Differentiate between sequential and parallel streams.

• Sequential Stream: Processes elements in one thread, in the order of the source.
• Parallel Stream: Processes elements in parallel across multiple threads for potential
performance gains.

PART - B

1. Explain wrapper classes in Java with examples. Discuss autoboxing and unboxing in detail.

2. Discuss the Java Collections API. What are the key interfaces and classes?

3. Explain the concept of generics in Java. How do they improve type safety and
performance?

4. Differentiate between List, Set, and Map in Java Collections. Provide examples for each.

5. Describe the ArrayList and LinkedList classes in Java. Compare their performance and use
cases.

6. Explain the implementation and use of HashSet and TreeSet in Java with examples.

7. Discuss the Map interface and compare HashMap and TreeMap with example code.

8. What are functional interfaces? List and describe the commonly used ones in Java 8.

9. Explain how lambda expressions work in Java. Write programs using lambdas with
different functional interfaces.

10. How are local and class variables accessed in lambda expressions? What are the
restrictions?

11. Explain the use of Predicate, Function, Consumer, and Supplier interfaces in Java 8.

12. Write a program to demonstrate stream operations like filter, map, and sorted on a list.
13. Explain the working of the reduce() method in Java Stream API with examples.

14. How does parallel stream processing work in Java? Compare it with sequential stream
processing.

15. Write a Java program to count, filter, and sort elements in a list using Stream API.

16. Compare and contrast String, StringBuffer, and StringBuilder in terms of performance
and mutability.

17. Describe the internal working of HashMap. How are collisions handled?

18. Discuss the advantages of using lambda expressions in Java. When should they be
avoided?

19. Explain type conversion with and without generics in collections. Provide appropriate
examples.

20. Describe how Stream API helps in writing concise and functional-style Java code. Use a
real-time example.

You might also like