Java 11 Collection Methods Interview Questions and Answers

Master Java 11 Collection Methods with production-ready interview questions covering toArray(IntFunction), Predicate.not(), Optional.isEmpty(), Files.readString(), Files.writeString(), immutable collections, and enterprise use cases.

Introduction

Java 11 introduced several enhancements that make working with collections and collection-related APIs more concise and expressive.

Although Java 11 did not introduce an entirely new Collections Framework, it enhanced APIs commonly used alongside collections, including:

  • Collection toArray()
  • Predicate.not()
  • Optional.isEmpty()
  • Files.readString()
  • Files.writeString()

These features reduce boilerplate code and improve readability in enterprise applications.

They are widely used in:

  • Spring Boot
  • Batch Processing
  • REST APIs
  • File Processing
  • Data Validation
  • Stream Processing
  • Microservices

This guide covers the most frequently asked Java 11 collection-related interview questions.


1. What collection-related improvements were introduced in Java 11?

Answer

Java 11 introduced several enhancements commonly used with collections.

Major improvements include:

  • Collection.toArray(IntFunction)
  • Predicate.not()
  • Optional.isEmpty()
  • Files.readString()
  • Files.writeString()

These additions simplify common collection processing and improve code readability.


2. What is the new toArray(IntFunction) method?

Answer

Before Java 11

String[] array =
list.toArray(
new String[0]
);

Java 11

String[] array =
list.toArray(
String[]::new
);

Advantages

  • Cleaner syntax
  • Better readability
  • Uses constructor references
  • Avoids unnecessary array creation logic

This style is now commonly seen in enterprise codebases.


3. Why is toArray(String[]::new) preferred?

Answer

Using constructor references makes the code more expressive.

Example

List<String> names =
List.of(
"Java",
"Spring"
);

String[] result =
names.toArray(
String[]::new
);

Benefits

  • Type-safe
  • Cleaner code
  • Better integration with Method References
  • Modern Java style

Most static analysis tools recommend this approach.


4. What is Predicate.not()?

Answer

Predicate.not() improves readability when negating predicates.

Before Java 11

names.stream()
     .filter(
         s -> !s.isBlank()
     )
     .toList();

Java 11

names.stream()
     .filter(
         Predicate.not(
             String::isBlank
         )
     )
     .toList();

The intent becomes much clearer.


5. What are the advantages of Predicate.not()?

Answer

Benefits include:

  • Better readability
  • Cleaner Streams code
  • Reusable predicates
  • Less complex Lambda expressions

Example

.filter(
Predicate.not(
Employee::isInactive
)
)

This is easier to understand than using the negation operator.


6. What is Optional.isEmpty()?

Answer

Before Java 11

if(!optional.isPresent()){

}

Java 11

if(optional.isEmpty()){

}

Benefits

  • Easier to read
  • More expressive
  • Matches collection APIs such as isEmpty()

This small improvement significantly improves readability.


7. What is Files.readString()?

Answer

Java 11 introduced a simple way to read an entire file.

Example

String content =
Files.readString(
Path.of("config.txt")
);

Before Java 11

Developers typically used:

  • BufferedReader
  • FileInputStream
  • Scanner

Now, one method is sufficient.


8. What is Files.writeString()?

Answer

Java 11 also introduced a simple way to write text to a file.

Example

Files.writeString(
Path.of("output.txt"),
"Hello Java 11"
);

Benefits

  • Less code
  • Better readability
  • Easier file handling

These APIs are ideal for configuration files, logs, and reports.


9. How do these improvements benefit Stream processing?

Answer

Example

List<String> names =
Files.readString(path)
     .lines()
     .filter(
         Predicate.not(
             String::isBlank
         )
     )
     .toList();

This demonstrates how multiple Java 11 enhancements work together:

  • Files.readString()
  • lines()
  • Predicate.not()
  • Streams API

The resulting code is concise and easy to maintain.


10. What immutable collection features are available?

Answer

Java 9 introduced immutable collections.

Examples

List.of()

Set.of()

Map.of()

Java 11 continues to encourage using immutable collections with the newer APIs.

Benefits

  • Thread safety
  • Simpler code
  • Reduced accidental modification
  • Better functional programming support

Immutable collections are widely used in modern Java applications.


11. Explain a production use case of Java 11 collection-related APIs.

Answer

Scenario

A Spring Boot application loads configuration data from a file.

Implementation

List<String> configs =
Files.readString(path)
     .lines()
     .filter(
         Predicate.not(
             String::isBlank
         )
     )
     .toList();

Writing processed output

Files.writeString(
outputPath,
String.join(
System.lineSeparator(),
configs
)
);

Result

  • Cleaner code
  • Better readability
  • Reduced boilerplate
  • Easier maintenance

12. What are the advantages of these Java 11 APIs?

Answer

Advantages include:

  • Less boilerplate
  • Better readability
  • Functional programming support
  • Cleaner Streams
  • Easier file handling
  • Improved Optional usage
  • Better integration with Method References
  • More maintainable code

These APIs improve developer productivity.


13. What are common mistakes while using these APIs?

Answer

Common mistakes include:

Using

!optional.isPresent()

instead of

optional.isEmpty()

Writing manual file-reading logic instead of using Files.readString().

Using verbose Lambda expressions instead of Predicate.not().

Continuing to use

new String[0]

without considering constructor references.

Modern Java code should prefer the newer APIs when appropriate.


14. What are the best practices for collection-related APIs?

Answer

Recommended practices:

  • Prefer Predicate.not() for negated predicates.
  • Use Optional.isEmpty() for readability.
  • Use constructor references with toArray().
  • Use Files.readString() for small to medium text files.
  • Use Files.writeString() for text output.
  • Prefer immutable collections where possible.
  • Combine these APIs with Streams for clean, functional code.

Following these practices results in simpler and more maintainable applications.


15. What interview tips should you remember about Java 11 collection-related APIs?

Answer

Interviewers commonly ask:

  • Predicate.not()
  • Optional.isEmpty()
  • toArray(String[]::new)
  • Files.readString()
  • Files.writeString()
  • Immutable collections
  • Java 8 vs Java 11 improvements
  • Production examples

Remember

  • Java 11 focused on improving existing APIs rather than introducing a new Collections Framework.
  • Use Predicate.not() to improve Stream readability.
  • Use Optional.isEmpty() instead of negating isPresent().
  • Prefer constructor references with toArray().
  • Use Files.readString() and Files.writeString() for simple text file operations.
  • Combine these APIs with Streams for clean and expressive code.

Summary

Java 11 enhanced collection-related APIs by introducing concise, readable, and developer-friendly methods. These improvements simplify file handling, stream processing, Optional usage, and array conversions while promoting modern Java coding practices.

Key Takeaways

  • Learn the collection-related enhancements introduced in Java 11.
  • Use toArray(String[]::new) for cleaner array conversion.
  • Prefer Predicate.not() over manual negation.
  • Use Optional.isEmpty() for expressive null handling.
  • Simplify file operations with Files.readString() and Files.writeString().
  • Continue using immutable collections for safer code.
  • Combine these APIs with Streams and Method References.
  • Replace verbose legacy code with modern Java APIs.
  • Follow best practices for readability and maintainability.
  • Support interview answers with practical enterprise examples.