The use of Lists in Java

The use of lists in Java is a useful way to store and manipulate large volumes of data, just as we would in an array, but with a series of advantages that make these types of variables the preferred ones for processing large amounts of information.

Lists in Java are variables that allow you to store large amounts of data. They are similar to Arrays. However, talking to my friend Aristides Villarreal (one of the best Java programmers today) tells me that the new trends in programming are oriented to the use of Lists to handle large volumes of data. In fact, Java 8, within its new programming tools (specifically Lambda expressions), has many interesting functionalities implementing lists, which will allow us to achieve many great things in a much faster and easier way than before (however, this is content for another post).

This has led me to look for ways to add the Lists in Java to my programming tools. Actually I’m not very good at using these types of variables since I’m just starting to use them, but I feel the need to share what little I know since with this I help other people while making my own notebook for future use.

Something that amazes me about lists in Java is the fact that you don’t need to set a specific size for the variable, unlike traditional arrays. Lists are very versatile and much easier to handle than other types of data grouping variables.

So… how do we declare a list? Well, we follow the following structure:

This type of list can store any type of data, but this approach is now obsolete. It is preferred that the type of data to be stored is specified. So, to declare a list where we will store data type String, we do the following:

With our list created, we can start entering data into it. Suppose we want to add the following names: Juan, Pedro, José, María, Sofía. We do the following:

It is also possible to add the index in which we want to add said element. We can also obtain the number of elements that the list has:

To extract elements from the list we use:

Where 0 is the index in which the information we want is located. In this case, the index 0 would be Pedro. If we want to delete a certain element:

Where again the 0 represents the index that we want to eliminate. Another way to delete a record is by name:

If we want to print the elements of the list in the console:

This will produce the following output:

lists in java

We can also print all the elements of the list individually with the help of a for loop.

lists in java

We could also use an iterator to loop through the list and print all its values:

To remove all the elements from the list we use:

If we want to know if our list contains any element we use:

This will return a true or a false. In case it contains an element, we can verify if we have a specific one among those elements. For example, if we want to know if José‘s name is written in our list, we use:

This will also return true or false. And if for some reason we want to modify any data in our list, for example index 1 that contains Pedro‘s name, we use the following method:

With this we will have changed the name in index 1 (Pedro) to the new name (Felix). If we want to extract a list that contains the names between one index and another we can use:

Let’s look at the following example:

lists in java

The result of this is:

listas en java

These are the elementary uses that we can give to the lists. In future posts I will be explaining how to create lists of more than one dimension and the use of Lambda expressions, in Java 8.

For now, I hope this information is useful for you. Greetings.

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments