Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 944053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:31:51+00:00 2026-05-15T22:31:51+00:00

How do I replace elements in a list with another? For example I want

  • 0

How do I replace elements in a list with another?

For example I want all two to become one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:31:51+00:00Added an answer on May 15, 2026 at 10:31 pm

    You can use:

    Collections.replaceAll(list, "two", "one");
    

    From the documentation:

    Replaces all occurrences of one specified value in a list with another. More formally, replaces with newVal each element e in list such that (oldVal==null ? e==null : oldVal.equals(e)). (This method has no effect on the size of the list.)

    The method also return a boolean to indicate whether or not any replacement was actually made.

    java.util.Collections has many more static utility methods that you can use on List (e.g. sort, binarySearch, shuffle, etc).


    Snippet

    The following shows how Collections.replaceAll works; it also shows that you can replace to/from null as well:

        List<String> list = Arrays.asList(
            "one", "two", "three", null, "two", null, "five"
        );
        System.out.println(list);
        // [one, two, three, null, two, null, five]
    
        Collections.replaceAll(list, "two", "one");
        System.out.println(list);
        // [one, one, three, null, one, null, five]
    
        Collections.replaceAll(list, "five", null);
        System.out.println(list);
        // [one, one, three, null, one, null, null]
    
        Collections.replaceAll(list, null, "none");
        System.out.println(list);
        // [one, one, three, none, one, none, none]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

sum( int(i.replace(',',''))if re.search('\d',i)!=None for i in list) I would like to sum all elements
I have to search through a list and replace all occurrences of one element
I want to recursively search and replace elements in an array. The array is
I want to replace duplicate elements from a vector with 0, and keep only
I want to replace some text to other text which contains elements of basic
I want to take an XML file and replace an element's value. For example
I want to write a function which takes a list of elements l, a
Possible Duplicate: Replace individual list elements in Haskell? I have managed to make some
I have two JList s. List A has these elements: 1 two 78 item4
I'm trying to replace a div with created elements, going from: <div id='links'></div> to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.