Java Lists: Basic Operations
Zander Bailey
Posted on January 27, 2020
Java’s Lists and other collection types can be very powerful objects, with methods for simplifying a great deal of tasks. In order to use them properly and make the most out of Java collections, there are some basic methods common to most collection types that it is important to know about and also to understand how they sometimes differ between collection types. To start with, you probably know how to add, get, and remove objects from a List, but it’s a good place to start, and from there we’ll talk about some variations on those operations. Adding an item is pretty straight forward, just use the method .add(item)
. Unlike an array or a List in some other languages, you cannot simply index into a Java List. Java Lists also require a special ‘getter’ method to retrieve an item from a List, called .get(index)
. To remove an item you have to use .remove(index)
, where ‘index’ is the index of the item you wish to remove. Let’s see the code for this:
/* First we create the List. Notice that we cannot fill the list upon instantiation */
ArrayList<String> fruit = new ArrayList<String>();
/* Here we add several items */
fruit.add("Orange");
fruit.add("Apple");
fruit.add("Banana");
/* Retrieve an item */
apple = fruit.get(1);
/* Remove an item */
fruit.remove(2);
Because Java Lists cannot be indexed into like an array, you cannot reassign their values the way you would normally. Instead, we have to use .set(index,”new entry”)
. Say we want to alter the previous List without disturbing the overall order:
fruit.set(0, "Cherry");
Okay, so now we know how to make some basic changes to a List, but let’s make it a little harder. Let’s say we have a List of unknown length, and the contents are unordered, so we don’t know the index of anything. Now we want to remove a specific string, but we need the index, right? There is a way we can get around this, with .indexOf(item)
. This will return the index of the specified item. Here’s how we would use it in this situation:
fruit.remove(fruit.indexOf("Cherry");
Alternatively we could also use this with .set
to change the value of an item at an unknown index. There’s another problem though, what if the item doesn’t exist? In this case we happen to know that ‘fruit’ contains an item called ‘cherry’, so it will return a valid index. But what if we can’t be sure that the item will actually exist in the list? Fortunately there’s a way we can handle that. If indexOf
receives an item that doesn’t exist in the List it just returns a -1. However, that will cause .remove
to throw an IndexOutOfBoundsException, so we’ll need to do some checking first:
If(fruit.indexOf("Cherry") > -1){
fruit.remove(fruit.indexOf("Cherry");
}
We can also check this with a method called .contains(item)
, which is designed for this purpose. Instead of a -1 it returns True if the item is contained in the List. It should be noted that the efficiency of this method is the same as writing out this operation with a for loop to check each item in the list. The exception to this is a Set. Set also has a .contains
method, but because Sets hash instead of index, it can check for an item considerably faster. However, depending on the intended usage this may or may not be an issue.
Posted on January 27, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024