Java List String

List in Java is a powerful data structure because it can hold arbitrary number of Objects. Unlike Arrays, List in Java can be resized at any point in time. For example, if we have a Java List of String, depending on the implementation, we can add as many String object as we want into the List. Contrast it with Arrays, if we declared an array of size 10, then we can only have around 10 Objects in an Array. If we want to have more, then we need to recreate the array. But for example, in Java List of String, we can add as many String instance as we want into the List as memory limitation would allow, without needing to recreate the data structure. Below are some examples on how we work with Java List of String.

Java List of String Declaration

List is an interface and not an implementation. It means List is not an actual class or instance in itself. It just represent a contract on how a List should behave. Thus, we can declare a variable as List, but we can assign to it any of the popular implementation. For example, ArrayList. Below is an example on how to declare a List of String in Java.
List<String> myListOfString;
The above list is null at this point of time, as we only declared myListOfString as a List of String in Java. We can assign an instance to it by creating any of implementations available.

ArrayList of String

The most common implementation of List in Java is the class ArrayList. Below is an example on how to use it.

List<String> myListOfString = new ArrayList<String>();

Now that we have an actual instance, we can start using it. For example.
List<String> myListOfString = new ArrayList<String>();
myListOfString.add("Apple");
myListOfString.add("Banana");
myListOfString.add("Carrots");
myListOfString.add("Dates");
Note that this is a resizable array implementation of the List interface, as discussed in the official Java Docs. It starts with an initial capacity, which defaults to 10 if not specified. And it automatically grows the List as more element is added.

LinkedList of String

Another popular choice of implementation is the LinkedList class. It is backed by a doubly linked list data structure beneath. This means the implementation is more efficient as the number of items grows as the penalty is very low. Using LinkedList is the same as ArrayList when specifying a List of String in Java.
List<String> myListOfString = new LinkedList<String>();
myListOfString.add("Car");
myListOfString.add("Truck");

Vector of String

The shortcoming of ArrayList and LinkedList is that they are both non thread safe objects. If thread safety is required, then synchronization must be done by the application using it. If we want the List to manage it's own thread safety, then Vector is suitable.
List<String> myListOfString = new Vector<String>();
myListOfString.add("Eggplant");
myListOfString.add("Bamboo");
As showm, using Vector is the same as ArrayList and LinkedList

Add to List of String

As shown above, adding a String to a List of String in Java is simple - we just use the add method of List. Below is an example on how to do that:
List<String> myListOfString = new ArrayList<String>();
myListOfString.add("Rock");
myListOfString.add("Paper");
myListOfString.add("Scissors");
System.out.println(myListOfString);

What happens to the code above is we initially have an empty List, and then we added three Strings one by one. When we run this, we get the output:

[Rock, Paper, Scissors]

Remove from List of String

If we want the opposite of adding, which means if we want to remove an item from a List of String, we use the remove method. Below is the syntax:

remove(int index)

From the syntax above, we see that we need to pass an integer which is the index of the item in the List. Remember that this is 0 based. Meaning the first item is as index 0, the second at index 1 and so on. Below is an example:
List<String> myListOfString = new ArrayList<String>();
myListOfString.add("Rock");
myListOfString.add("Paper");
myListOfString.add("Scissors");
System.out.println(myListOfString);
myListOfString.remove(1);
System.out.println(myListOfString);
Since we have three items at the firstr println invocation, index 1 means the second item. So remove(1) means remove the second item from the List. Hence Paper is removed. Below is the output of the code above that removes an item from a List of String in Java.
[Rock, Paper, scissors]
[Rock, scissors]