What is a Java list?
Java lists are one of the most fundamental data structures, helping you store and organize objects of the same class. But what is a Java list exactly, and how do Java lists work?
What are Java lists?
Java lists are an implementation of the list data structure for the Java programming language. A list is an abstract data type that contains a countable number of objects in a sequential order. Lists are different from sets, another fundamental data structure, in two important ways:
- Lists may contain duplicate elements, while sets cannot.
- Lists are ordered collections, while sets are unordered. Users may traverse the list from beginning to end, and may access the element stored at a particular location in the list.
Lists in Java (and other programming languages) have a wide variety of use cases. Lists are ideal for situations in which objects need to be connected to each other in a sequential order: for example, a user's history in a web browser, a music playlist consisting of multiple songs, or a list of actions in a software application that can be reverted using an undo command.
How do Java lists work?
Java lists are implemented by the java.util.List interface. The most important methods of Java lists include:
- add(): Adds an element to the specified location in the list, or at the end of the list if not specified.
- clear(): Removes all elements from the list.
- contains(): Returns a Boolean value based on whether the list contains the given element.
- get(): Retrieves the element in the list at the specified location.
- indexOf(): Returns the index of the first element in the list that matches the given element.
- isEmpty(): Returns a Boolean value based on whether the list is empty.
- remove(): Removes the given element, or the element at the specified index.
Because java.util.List is only an interface, it cannot be instantiated directly in Java. Instead, users need to instantiate one of the Java classes that implement the List interface, including ArrayList, LinkedList, Stack, or Vector. The differences between these classes is as follows:
- ArrayList: ArrayLists are resizable arrays that can dynamically expand or contract as the number of elements changes.
- LinkedList: LinkedLists are lists in which each element is doubly linked to the two elements before and after it in the list.
- Stack: Stacks are lists that obey the "last in, first out" (LIFO) principle: the most recently inserted element is at the head of the list.
- Vector: Vectors are similar to ArrayLists in that they are dynamically resizable arrays. Unlike ArrayLists, however, Vectors are synchronized (thread-safe), which means that they are less suitable for high-performance situations.
Java lists in Redis
Redis is an open-source, in-memory data structure store used to implement NoSQL key-value databases, caches, and message brokers. Along with strings, hashes, sets, and sorted sets, lists are one of the fundamental Redis data types.
The Redis platform includes several built-in commands for working with the list data structure, including:
- LPUSH: Inserts a new element at the head (beginning) of the list.
- RPUSH: Inserts a new element at the tail (end) of the list.
- LRANGE: Retrieves the elements in the list within the given range.
- LTRIM: "Trims" the list to only contain the elements within the given range.
- LPOP: Removes the element at the head of the list.
- LREM: Removes the specified element or elements from the list.
While Redis includes full functionality for working with list data structures, it's markedly different from list implementations in programming languages such as Java. The problem is that Redis isn't automatically compatible with Java out of the box. Instead, to lower the learning curve, many Java developers choose to install a third-party Redis Java client such as Redisson.
Redisson includes many implementations of familiar Java distributed objects and collections, including lists. In Redisson, the list data structure is available via the RList interface, which reimplements the java.util.List interface from standard Java.
Below is an example of how to use the RList interface in Redisson:
RList<SomeObject> list = redisson.getList("anyList"); list.add(new SomeObject()); list.get(0); list.remove(new SomeObject());
RList includes all of the familiar methods from the java.util.List interface. Note that Redis places a strict cap on the size of RLists at 4,294,967,295 (2^32 - 1) elements. In addition, RLists come with asynchronous, reactive, and RxJava2 interfaces, so that you can choose the programming model that best fits your needs.