Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java list replace/fill elements

farenda 2015-07-20 0

Problem:

Sometimes we want on Java List replace/fill all/selected elements. It can be done manually, but much better way is to reuse Java Collections API. See how!

Solution:

There are two cases:

  1. Replace all occurrences of element with the new value.
  2. Replace all elements in a list with the new value.

The following Java example leverages Java Collections API to do the job:

package com.farenda.java;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class CollectionsFill {

    public static void main(String[] args) {
        replaceSelectedElement();

        replaceAllElementsWithOne();
    }

    private static void replaceSelectedElement() {
        List<String> items = Arrays.asList("a", "b", "c", "b");
        System.out.println("Original: " + items);

        Collections.replaceAll(items, "b", "B");
        System.out.println("Updated: " + items);
    }

    private static void replaceAllElementsWithOne() {
        List<String> languages = new LinkedList<>();

        System.out.println("\nLanguages: " + languages);

        Collections.fill(languages, "Esperanto");
        System.out.println("Updated empty: " + languages);

        Collections.addAll(languages, "English", "Espanol");
        System.out.println("Languages: " + languages);

        Collections.fill(languages, "Esperanto");
        System.out.println("Updated: " + languages);
    }
}

In the first method (replaceSelectedElement) we use Collections.replaceAll(List<T>, T from, T to) to replace all occurrences of selected element with the new value.

In the replaceAllElementsWithOne method we use Collections.fill(List<T>, T obj) to replace all elements in the list with the new value.

Of course, in both cases the list implementations must support set operation, else UnsupportedOperationException will be thrown!

And here’s the result:

Original: [a, b, c, b]
Updated: [a, B, c, B]

Languages: []
Updated empty: []
Languages: [English, Espanol]
Updated: [Esperanto, Esperanto]

As you can see Collections.fill() replaces only existing elements, so can’t be used to create a new List with initial values. ;-)

Share with the World!
Categories Java Tags java-collections
Previous: Java find/count occurrences
Next: Java empty List/Collection and null check

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored