Sunday, 1 June 2014

ArrayList : replaceAll : Replace All Elements with the result of applying the operator

public void replaceAll(UnaryOperator<E> operator)
Replaces each element of this list with the result of applying the operator to that element. Errors or runtime exceptions thrown by the operator are relayed to the caller.

import java.util.function.*;

class MyOperator<T> implements UnaryOperator<T>{
 T var1;
 public T apply(T var){
  return var1;
 }
}

import java.util.*;

class ArrayListReplaceAll{
 public static void main(String args[]){
  ArrayList<Integer> myList;
  MyOperator<Integer> operator;
  
  myList = new ArrayList<>();
  operator = new MyOperator<>();
  operator.var1 = 99;
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
  myList.add(50);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
  
  myList.replaceAll(operator);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
 }
}

Output
Elements in myList are
[10, 20, 30, 40, 50]
Elements in myList are
[99, 99, 99, 99, 99]


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment