Sunday, 1 June 2014

ArrayList : set : Replace value at particular index

public E set(int index, E element)
Replaces the element at the specified position in this list with the specified element.

import java.util.*;

class ArrayListSet{
 public static void main(String args[]){
  ArrayList<Integer> myList;
  myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
  
  System.out.println("\nReplace 10 by 50");
  myList.set(0, 50);
  
  System.out.println("\nElements in myList are");
  System.out.println(myList);
 }
}

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

Replace 10 by 50

Elements in myList are
[50, 20, 30, 40]

1. Throws IndexOutOfBoundsException if index is out of range
import java.util.*;

class ArrayListSetIndexOut{
 public static void main(String args[]){
  ArrayList<Integer> myList;
  myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
  
  System.out.println("\nReplace Element at index 5 by 50");
  myList.set(5, 50);
  
  System.out.println("\nElements in myList are");
  System.out.println(myList);
 }
}

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

Replace Element at index 5 by 50
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size:
4
        at java.util.ArrayList.rangeCheck(ArrayList.java:638)
        at java.util.ArrayList.set(ArrayList.java:429)
        at ArrayListSetIndexOut.main(ArrayListSetIndexOut.java:18)


Prevoius                                                 Next                                                 Home

ArrayList : retainAll : Retain Elements

public boolean retainAll(Collection<?> c)
Retains only the elements in this list that are contained in the specified collection. Return true if this list changed as a result of the call.

import java.util.*;
class ArrayListRetainAll{
 public static void main(String args[]){
  ArrayList<Integer> myList = new ArrayList<> ();
  Set<Integer> mySet1 =new HashSet<> ();
  Set<Integer> mySet2 =new HashSet<> ();
  
  /* Add Elements to myList */
  for(int i=0; i < 10; i++){
   myList.add(i);
  }
  
  /* Add Elements to mySet1 */
  for(int i=5; i < 15; i++){
   mySet1.add(i);
  }
  
  /* Add Elements to mySet2 */
  for(int i=10; i < 15; i++){
   mySet2.add(i);
  }
  
  System.out.println("Elements in myList are ");
  System.out.println(myList +"\n");
  System.out.println("Elements in mySet1 are ");
  System.out.println(mySet1 +"\n");
  System.out.println("Elements in mySet2 are ");
  System.out.println(mySet2 +"\n");
  
  myList.retainAll(mySet1);
  System.out.println("After retaining mySet1 from myList");
  System.out.println(myList +"\n");
  
  myList.retainAll(mySet2);
  System.out.println("After retaining mySet2 from myList");
  System.out.println(myList +"\n");
  
 }
}

Output
Elements in myList are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Elements in mySet1 are
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Elements in mySet2 are
[10, 11, 12, 13, 14]

After retaining mySet1 from myList
[5, 6, 7, 8, 9]

After retaining mySet2 from myList
[]


Prevoius                                                 Next                                                 Home

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

Saturday, 31 May 2014

ArrayList : removeRange : Remove range Of Elements

protected void removeRange(int fromIndex, int toIndex)
Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive. Methods equals, hashCode, listIterator, removeRange, subList are inherited from AbstractList.

import java.util.*;

class ArrayListRemoveRange<T> extends ArrayList<T>{
 public static void main(String args[]){
  ArrayListRemoveRange<Integer> myList;
  myList = new ArrayListRemoveRange<>();
  
  /* 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);
  
  System.out.println("Removing elements from index 1 to 3");
  myList.removeRange(1,4);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
 }
}

Output
Elements in myList are
[10, 20, 30, 40, 50]
Removing elements from index 1 to 3
Elements in myList are
[10, 50]


Prevoius                                                 Next                                                 Home

ArrayList : removeIf : Remove element if condition satisfied

public boolean removeIf(Predicate<? super E> filter)
Removes all of the elements of this collection that satisfy the given predicate.

import java.util.function.*;

class MyPredicate<T> implements Predicate<T>{
  T var1;
  public boolean test(T var){
  if(var1.equals(var)){
   return true;
  }
  return false;
  }
}

import java.util.*;

class ArrayListRemoveIf{
 public static void main(String args[]){
  ArrayList<Integer> myList;
  MyPredicate<Integer> filter;
  
  myList = new ArrayList<> ();
  filter = new MyPredicate<> ();
  
  filter.var1 = 10;
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(10);
  myList.add(30);
  myList.add(10);
  myList.add(40);
  myList.add(10);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
  
  System.out.print("Removing all 10's from myList");
  System.out.println(myList.removeIf(filter));
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
 }
}

Output
Elements in myList are
[10, 20, 10, 30, 10, 40, 10]
Removing all 10's from myListtrue
Elements in myList are
[20, 30, 40]


Prevoius                                                 Next                                                 Home

ArrayList : removeAll : Remove collection of elements

boolean removeAll(Collection<?> c)
Removes from this list all of its elements that are contained in the specified collection (optional operation).

import java.util.*;
class ArrayListRemoveAll{
 public static void main(String args[]){
  ArrayList<Integer> myList = new ArrayList<> ();
  Set<Integer> mySet =new HashSet<> ();
  
  /* Add Elements to myList */
  for(int i=0; i < 10; i++){
   myList.add(i);
  }
  
  /* Add Elements to mySet */
  for(int i=5; i < 15; i++){
   mySet.add(i);
  }
  
  System.out.println("Elements in myList are ");
  System.out.println(myList +"\n");
  System.out.println("Elements in mySet are ");
  System.out.println(mySet +"\n");
  
  /* Remove mySet Data from myList */
  myList.removeAll(mySet);
  
  System.out.println("Elements in myList after removing mySet are");
  System.out.println(myList); 
 }
}

Output
Elements in myList are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Elements in mySet are
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Elements in myList after removing mySet are
[0, 1, 2, 3, 4]


Prevoius                                                 Next                                                 Home

ArrayList : remove(Object o) : Remove first occurrence of Specific Object

public boolean remove(Object o)
Removes the first occurrence of the specified element from this list if it is present. Return true if this list contained the specified element.

import java.util.*;

class ArrayListRemoveObject{
 public static void main(String args[]){
  ArrayList<Integer> myList;
  myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(10);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
  
  Integer i1 = new Integer(10);
  System.out.print("\nIs 10 removed ");
  System.out.println(myList.remove(i1));
  
  i1 = new Integer(50); 
  System.out.print("Is 50 removed ");
  System.out.println(myList.remove(i1));
  
  System.out.println("\nElements in myList are");
  System.out.println(myList);
 }
}

Output
Elements in myList are
[10, 20, 30, 10]

Is 10 removed true
Is 50 removed false

Elements in myList are
[20, 30, 10]



Prevoius                                                 Next                                                 Home