Partner – Orkes – NPI EA (cat=Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag=Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – LambdaTest – NPI EA (cat=Testing)
announcement - icon

Accessibility testing is a crucial aspect to ensure that your application is usable for everyone and meets accessibility standards that are required in many countries.

By automating these tests, teams can quickly detect issues related to screen reader compatibility, keyboard navigation, color contrast, and other aspects that could pose a barrier to using the software effectively for people with disabilities.

Learn how to automate accessibility testing with Selenium and the LambdaTest cloud-based testing platform that lets developers and testers perform accessibility automation on over 3000+ real environments:

Automated Accessibility Testing With Selenium

1. Introduction

In this tutorial, we’re going to discuss how to choose the proper collection interface and class in the Java library. We skip legacy collections, such as Vector, Stack, and Hashtable in our discussion as we need to avoid using them in favor of the new collections. Concurrent collections deserve a separate topic, so we don’t discuss them either.

2. Collection Interfaces in the Java Library

It’s very useful to know the organization of the collection interfaces and classes in the Java library before trying to use them efficiently. The Collection interface is the root of all the collection interfaces. List, Set, and Queue interfaces extend the Collection.

Maps in the Java library are not treated as regular collections, so the Map interface doesn’t extend Collection. Here’s the diagram for interface relationships in the Java library:

Collection

Any concrete collection implementation (collection class) is derived from one of the collection interfaces. The semantics of collection classes are defined by their interfaces, as concrete collections provide specific implementations for operations that their parent interfaces define. Consequently, we need to choose the proper collection interface before selecting the suitable collection class.

3. Choose the Right Collection Interface

Choosing the right collection interface is somewhat straightforward. Indeed, the diagram below shows a logical interface selection flow:

Interface Selection Diagram

To summarize, we use lists when the insertion order of elements matters and there are duplicate elements. Sets are used when elements are treated as a set of objects, there are no duplicates, and the insertion order doesn’t matter.

Queues are used when LIFO, FIFO, or removal by priority semantics is required, and finally, maps are used when the association of keys and values is needed.

4. Choose the Right Collection Implementation

Below we can find the comparison tables of collection classes separated by the interfaces they implement. The comparisons are made based on common operations and their performance. Specifically, the performance of operations is estimated using Big-O notation. A more practical guide to operations’ duration in Java collections can be found in the benchmark of collection operations.

4.1. Lists

Let’s start with a list comparison table. Common operations for lists are adding and removing elements, accessing an element by index, traversal of the elements, and finding an element:

Lists Comparison Table Add/remove element in the beginning Add/remove element in the middle Add/remove element in the end Get i-th element (random access) Find element Traversal order
ArrayList O(n) O(n) O(1) O(1) O(n), O(log(n)) if sorted as inserted
LinkedList O(1) O(1) O(1) O(n) O(n) as inserted

As we can see, ArrayList is good at adding and removing elements in the end, as well as having random access to elements. Conversely, it’s bad at adding and removing elements at arbitrary positions. Meanwhile, LinkedList is good at adding and removing elements at any position. However, it doesn’t support true O(1) random access. So, regarding lists, the default choice is ArrayList until we need fast element addition and removal at any position.

4.2. Sets

For sets, we’re interested in adding and removing elements, traversal of elements, and finding an element:

Sets Comparison Table Add element Remove element Find element Traversal order
HashSet amortized O(1) amortized O(1) O(1) random, scattered by the hash function
LinkedHashSet amortized O(1) amortized O(1) O(1) as inserted
TreeSet O(log(n)) O(log(n)) O(log(n)) sorted, according to elements comparison criterion
EnumSet O(1) O(1) O(1) according to the definition order of the enum values

As we can see, the default choice is the HashSet collection, as it’s very fast for all the operations it supports. Furthermore, if also the insertion order of elements matters, we go with LinkedHashSet. Basically, it’s an extension of HashSet, which keeps track of elements’ insertion order by using a linked list structure internally.

If the elements need to be sorted and the sorted order needs to be preserved while adding and removing elements, then we go with TreeSet.

If the elements of the set are just enumeration values of a single enum type, then the wisest choice is EnumSet.

4.3. Queues

Queues can be divided into two groups:

  1. LinkedList, ArrayDequeQueue interface implementations can act as the stack, queue, and dequeue data structures. Generally, ArrayDeque is faster than LinkedList. Hence it’s the default choice
  2. PriorityQueue – Queue interface implementation backed by the binary heap data structure. Used for fast (O(1)) element retrieval, which has the highest priority. Addition and removal work in O(log(n)) time

4.4. Maps

Similarly to sets, we consider the operations of adding and removing elements, traversal of elements, and finding an element for maps:

Maps Comparison Table Add element Remove element Find element Traversal order
HashMap amortized O(1) amortized O(1) O(1) random, scattered by the hash function
LinkedHashMap amortized O(1) amortized O(1) O(1) as inserted
TreeMap O(log(n)) O(log(n)) O(log(n)) sorted, according to elements comparison criterion
EnumMap O(1) O(1) O(1) according to the definition order of the enum values

The selection logic for maps is similar to the selection logic for sets: we use HashMap by default, LinkedHashMap if additionally, insertion order is important, TreeMap for sorting, and EnumMap when keys belong to values of a specific enum type.

Lastly, there are two implementations of the Map interface, which have very specific applications: IdentityHashMap, and WeakHashMap.

5. Concrete Collection Selection Diagram

We can extend the diagram for choosing the proper collection interface for selecting concrete collection implementations:

Concrete Collection Selection Diagram

6. Conclusion

In this article, we went through collection interfaces and collection classes in the Java library. Moreover, we proposed methods for selecting the correct interface and implementation.

Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

Partner – Orkes – NPI EA (cat = Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag = Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

eBook Jackson – NPI EA – 3 (cat = Jackson)