Java: Remove an element from a List

One of the more common tasks in programming is removing a specific element from a list. Although this seems to be straight-forward in Java, it’s a bit more tricky.

Before we start, we should build our list:

   public ArrayList<String> createList(){
      ArrayList<String> myList = new ArrayList<String>();
      
      myList.add("String 1");
      myList.add("String 2");
      myList.add("String 3");
      myList.add("String 4");
      myList.add("String 5");
      
      
      return myList;
   }

Let’s say we want to remove the String “String 2”. The first thing that comes to mind is to loop through the list, until you find the element “String 2”, and remove that element:

   public List<String> removeFromListUsingForEach(List<String> sourceList){
      for(String s : sourceList){
         if (s.equals("String 2")){
            sourceList.remove(s);
         }
      }
      return sourceList;
   }

Unfortunately, in this case, this will throw a

java.util.ConcurrentModificationException

I said “in this case”, because the exception is not always thrown. The details of this strange behavior is out of scope for this blogpost, but can be found here.

There are several ways to remove an element from a list. Depending on your personal preference, and which version of Java you use, here are some examples.

1. Use a for-loop which loops backwards
You can use a for-loop, which runs from the end of the list to the beginning. The reason you want to loop in this direction is, that when you’ve found the element you want to remove, you remove the element at that index. Every element after this one will shift one position towards the beginning of the list. If you’d run the loop forward, you’d have to compensate for this, which just isn’t worth the effort.

   public List<String> removeFromListUsingReversedForLoop(List<String> sourceList){
      for(int i = sourceList.size()-1; i >= 0; i--){
         String s = sourceList.get(i);
         if (s.equals("String 2")){
            sourceList.remove(i);
         }
      }
      return sourceList;
   }

This works in every Java version since 1.2, although you can’t use generics until Java 1.5.

2. Use an Iterator
Another way to remove an element from a list is to use an Iterator. The Iterator will loop through the list, and, if needed, can remove the current element from that list. This is done by calling

Iterator.remove()
   public List<String> removeFromListUsingIterator(List<String> sourceList){
      Iterator<String> iter = sourceList.iterator();
      while (iter.hasNext()){
         if (iter.next().equals("String 2")){
            iter.remove();
         }
      }
      return sourceList;
   }

This works in every Java version since 1.2, although you can’t use generics until Java 1.5.

3. Use Java 8 Streams
What you’re essentially doing here is make a copy of the list, and filter out the unwanted elements.

   public List<String> removeFromListUsingStream(List<String> sourceList){
      List<String> targetList = sourceList.stream()
            .filter(s -> !s.equals("String 2"))
            .collect(Collectors.toList());
      return targetList;
   }

This works since Java 1.8. More about Java 8 can be found here.

Distributed Websearch

When it comes to searching the web, the first thing that comes to mind is Google. Or Bing. Or Yahoo. Sure, it’s easy, fast, and sort of reliable. However, there are a few problems with these kinds of services.

First of all, there’s the filter bubble. These search engines (and social networks) will present results that are specifically tailored for you. On the one hand, this is a good thing, because you’ll probably find what you’re looking for faster. However, you’ll never find information that COULD be relevant, but is filtered out for you. Your world view is being limited by this filter bubble.

You can work around this by using “anonymous” search engines, such as DuckDuckGo or Startpage. These search engines don’t store your personal data, and therefore can not create a filter bubble for you. As for Startpage, that search engine will act as a proxy between you and Google. You will usually get high quality results to your search query.

Another problem is “The Right To Be Forgotten”. The European court ruled that Google needs to provide the option to delete certain search results from its index. When you’ve had problems, which have been resolved, you shouldn’t be judged on the basis of those past problems. However, once information is on the Internet, it’s very hard to remove. The toothpaste is out of the tube, the genie out of the bottle. You’ll never get it back in. Google provides links to this information, it does not provide the information itself. Removing the links is not the same as removing the information. We have seen the same misunderstanding in the case of The Pirate Bay.

YaCy

Since a couple of years, there is YaCy. YaCy is a distributed search engine, that ensures TRUE anonimity, and is impossible to take down. You need to install a piece of software on your computer, browse to the local webserver (probably http://localhost:8090/), and start searching. At this moment, there are 1.7 billion documents in the public network. In contrast, the web is estimated to contain 15 to 55 billion documents.

There are a couple of other interesting features in YaCy, such as creating a search engine for one specific topic, or a search engine for your local intranet. Give it a try.