adsense


Showing posts with label JAVA EXAMPLES REPLACE AN ELEMENT IN A LIST. Show all posts
Showing posts with label JAVA EXAMPLES REPLACE AN ELEMENT IN A LIST. Show all posts

Saturday, 31 October 2015

JAVA EXAMPLES REPLACE AN ELEMENT IN A LIST PAGE 169

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



JAVA EXAMPLES REPLACE AN ELEMENT IN A LIST






Java Examples - Replace an element in a list



Problem Description:
How to replace an element in a list
Solution:
Following example uses replaceAll() method to replace all the 
occurance of an element with a different element in a list.
import java.util.*;

public class Main {
   public static void main(String[] args) {
      List list = Arrays.asList("one Two three Four five six
      one three Four".split(" "));
      System.out.println("List :"+list);
      Collections.replaceAll(list, "one", "hundread");
      System.out.println("replaceAll: " + list);
   }
}
Result:
The above code sample will produce the following result.
List :[one, Two, three, Four, five, six, one, three, Four]
replaceAll: [hundread, Two, three, Four, five, six,
hundread, three, Four]