Sunday, 1 June 2014

ArrayList : subList : Get the Sub List

public List<E> subList(int fromIndex, int toIndex)
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.

import java.util.*;

class ArrayListSubList{
 public static void main(String args[]){
  ArrayList<Integer> myList = new ArrayList<> ();
  List<Integer> sub;
   
  /* Add Elements to myList */
  for(int i=0; i < 90; i+=5)
   myList.add(i);
   
  sub = myList.subList(5, 15);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
  System.out.println("Elements in sub List are");
  System.out.println(sub);
  
  System.out.println("\nAdd 1 to sub List");
  sub.add(1);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
  System.out.println("Elements in sub List are");
  System.out.println(sub);
  
  System.out.println("\nRemove 35 from the sub List");
  sub.remove(2);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
  System.out.println("Elements in sub List are");
  System.out.println(sub);

 } 
}

Output
Elements in myList are
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85]
Elements in sub List are
[25, 30, 35, 40, 45, 50, 55, 60, 65, 70]

Add 1 to sub List
Elements in myList are
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 1, 75, 80, 85]
Elements in sub List are
[25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 1]

Remove 35 from the sub List
Elements in myList are
[0, 5, 10, 15, 20, 25, 30, 40, 45, 50, 55, 60, 65, 70, 1, 75, 80, 85]
Elements in sub List are
[25, 30, 40, 45, 50, 55, 60, 65, 70, 1]

1. Throws IndexOutOfBoundsException for an illegal endpoint index value
import java.util.*;

class ArrayListSubListIndexOut{
 public static void main(String args[]){
  ArrayList<Integer> myList = new ArrayList<> ();
  List<Integer> sub;
   
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  
  sub = myList.subList(0, 5);
 } 
}

Output
Exception in thread "main" java.lang.IndexOutOfBoundsException: toIndex = 5
        at java.util.ArrayList.subListRangeCheck(ArrayList.java:989)
        at java.util.ArrayList.subList(ArrayList.java:981)
        at ArrayListSubListIndexOut.main(ArrayListSubListIndexOut.java:12)


Prevoius                                                 Next                                                 Home
public Spliterator<E> spliterator()
Creates a late-binding and fail-fast Spliterator over the elements in this list. 

import java.util.*;

class ArrayListSpliterator{
 public static void main(String args[]){
  ArrayList<Integer> myList;
  Spliterator<Integer> mySpliterator;
  
  myList = new ArrayList<> ();
  
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
  
  mySpliterator = myList.spliterator();
  
  System.out.print("characteristics Of Spliterator are ");
  System.out.println(mySpliterator.characteristics());
 }
}
 
Output
characteristics Of Spliterator are 16464

Prevoius                                                 Next                                                 Home

ArrayList : sort(Comparator c) : Sort this List using given Comparator

public void sort(Comparator<? super E> c)
Sorts this list using the supplied Comparator to compare elements.

class Employee{
 Integer number;
 String name;
 
 Employee(int number, String  name){
  this.number = number;
  this.name = name;
 } 
 
 public String toString(){
  return number + " " + name;
 }
}

import java.util.Comparator;
class EmployeeIdComparator implements Comparator<Employee>{

 public int compare(Employee e1, Employee e2){
  return e1.number.compareTo(e2.number);
 }
 
 public String toString(){
  return "EmployeeIdComparator";
 }
}

import java.util.*;

class ArrayListSort{
 public static void main(String args[]){
  ArrayList<Employee> myList;
  EmployeeIdComparator comparator;
  
  myList = new ArrayList<> ();
  comparator = new EmployeeIdComparator();
  
  myList.add(new Employee(2, "xyz"));
  myList.add(new Employee(1, "mno"));
  myList.add(new Employee(5, "abc"));
  myList.add(new Employee(4, "def"));
  myList.add(new Employee(3, "ghi"));
  
  System.out.println("Elements Before Sorting");
  System.out.println(myList); 
  
  myList.sort(comparator);
  
  System.out.println("Elements After Sorting");
  System.out.println(myList); 
 }
}

Output
Elements Before Sorting
[2 xyz, 1 mno, 5 abc, 4 def, 3 ghi]
Elements After Sorting
[1 mno, 2 xyz, 3 ghi, 4 def, 5 abc]


Prevoius                                                 Next                                                 Home

ArrayList : size : Get the size of the List

public int size()
Returns the number of elements in this list.

import java.util.*;

class ArrayListSize{
 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.print("Number Of Elements in myList are ");
  System.out.println(myList.size());
 }
}

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



Prevoius                                                 Next                                                 Home

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