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

ArrayList : remove(int index) : Removes the element at the specified position in this list.

public E remove(int index)
Removes the element at the specified position in this list. Shifts any subsequent elements to the left.

import java.util.*;

class ArrayListRemoveAtIndex{
 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("\nRemove Element at index 2");
  System.out.print("Element removed is ");
  System.out.println(myList.remove(2));
  
  System.out.println("\nElements in myList are");
  System.out.println(myList);
 }
}

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

Remove Element at index 2
Element removed is 30

Elements in myList are
[10, 20, 40]

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

class ArrayListRemoveAtIndexOut{
 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("\nRemove Element at index 5");
  System.out.print("Element removed is ");
  System.out.println(myList.remove(5));
  
  System.out.println("\nElements in myList are");
  System.out.println(myList);
 }
}

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

Remove Element at index 5
Element removed is Exception in thread "main" java.lang.IndexOutOfBoundsExceptio
n: Index: 5, Size: 4
        at java.util.ArrayList.rangeCheck(ArrayList.java:638)
        at java.util.ArrayList.remove(ArrayList.java:477)
        at ArrayListRemoveAtIndexOut.main(ArrayListRemoveAtIndexOut.java:19)


Prevoius                                                 Next                                                 Home

Friday, 30 May 2014

ArrayList : listIterator : Iterate the List

ListIterator<E> listIterator()
List Provides way to iterate the List both forward and backward way. Methods equals, hashCode, listIterator, removeRange, subList are inherited from AbstractList.

import java.util.*;

class ArrayListIterator{
 public static void main(String args[]){
  ArrayList<Integer> myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
  myList.add(50);
  
  /* traverse the list in Forward */
  ListIterator<Integer> iter = myList.listIterator();
  System.out.println("Forward Traversal Of the List is");
  while(iter.hasNext()){
   System.out.print(iter.next() + " ");
  }
  System.out.println();
  
  /* traverse the list in Backward */
  System.out.println("Backward Traversal Of the List is");
  while(iter.hasPrevious()){
   System.out.print(iter.previous() + " ");
  }
  System.out.println();
  
 }
}


Output
Forward Traversal Of the List is
10 20 30 40 50
Backward Traversal Of the List is
50 40 30 20 10


Prevoius                                                 Next                                                 Home

ArrayList : lastIndexOf : Get the last occurrence index of Element

public int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

import java.util.*;
 
class ArrayListLastIndexOf{
 public static void main(String args[]){
  ArrayList<Integer> myList;
  myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(30);
  myList.add(20);
  myList.add(30);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
  
  System.out.print("Last Index of 30 is ");
  System.out.println(myList.lastIndexOf(30));
 }
}

Output
Elements in myList are
[30, 20, 30]
Last Index of 30 is 2


Prevoius                                                 Next                                                 Home

ArrayList : iterator : Returns Iterator over List

public Iterator<E> iterator()
Returns an iterator over the elements in this list. This implementation returns a list iterator over the list.

import java.util.*;
class ArrayListIterator1{
 public static void main(String args[]){
  ArrayList<Integer> myList;
  Iterator<Integer> myIter;
  
  myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(123);
  myList.add(321);
  myList.add(456);
  
  myIter = myList.iterator();
  
  System.out.println("Elements in myList are");
  while(myIter.hasNext()){
   System.out.println(myIter.next());
  }
 }
}

Output
Elements in myList are
10
123
321
456

Prevoius                                                 Next                                                 Home

ArrayList : isEmpty : Check Whether List is empty

boolean isEmpty()
Returns true if this list contains no elements.

import java.util.*;
class ArrayListEmpty{
 public static void main(String args[]){
  ArrayList<Double> myList = new ArrayList<> ();
  
  System.out.println("Number Of Elements in the List are " + myList.size());
  System.out.println("Is the List Empty " + myList.isEmpty());
  
  /* Add Elements to the List */
  myList.add(10.01);
  myList.add(null);
  myList.add(123.01);
  myList.add(null);
  
  System.out.println("Number Of Elements in the List are " + myList.size());
  System.out.println("Is the List Empty " + myList.isEmpty());
 }
}

Output
Number Of Elements in the List are 0
Is the List Empty true
Number Of Elements in the List are 4
Is the List Empty false

Prevoius                                                 Next                                                 Home

ArrayList : indexOf : get the index of Element

public int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

import java.util.*;
 
class ArrayListIndexOf{
 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);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
  
  System.out.print("Index of 30 is ");
  System.out.println(myList.indexOf(30));
 }
}

Output
Elements in myList are
[10, 20, 30]
Index of 30 is 2


Prevoius                                                 Next                                                 Home

ArrayList : get : Get the Element at Specific index

public E get(int index)
Returns the element at the specified position in this list.

import java.util.*;

class ArrayListGet{
  public static void main(String args[]){
    ArrayList<Integer> myList;
    myList = new ArrayList<Integer> ();
  
    /* Add Elements to myList */
    myList.add(10);
    myList.add(20);
    myList.add(30);
    myList.add(40);
  
    System.out.println("Elements in myList are " + myList);
    System.out.println("\nElement at index 1 is " + myList.get(1));
  }
}


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

Element at index 1 is 20

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

class ArrayListGetIndexOut{
  public static void main(String args[]){
    ArrayList<Integer> myList;
    myList = new ArrayList<Integer> ();
  
    /* Add Elements to myList */
    myList.add(10);
    myList.add(20);
    myList.add(30);
    myList.add(40);
  
    System.out.println("Elements in myList are " + myList);
    System.out.println("\nElement at index -1 is " + myList.get(-1));
  }
}


Output
Elements in myList are [10, 20, 30, 40]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
        at java.util.ArrayList.elementData(ArrayList.java:371)
        at java.util.ArrayList.get(ArrayList.java:384)
        at ArrayListGetIndexOut.main(ArrayListGetIndexOut.java:15)

Prevoius                                                 Next                                                 Home