Reverse contents of arraylist in java

We can use Collections.reverse() method to reverse contents of arraylist in java. Syntax: Collections.reverse(arrayList); Example: package com.w3schools;   import java.util.ArrayList; import java.util.Collections;   public class Test { public static void main(String args[]){ ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Jai"); arrayList.add("Mahesh"); arrayList.add("Vivek"); arrayList.add("Naren"); arrayList.add("Hemant"); arrayList.add("Vishal"); System.out.println("Actual ArrayList:"+arrayList); Collections.reverse(arrayList); System.out.println("ArrayList after reverse operation:" + arrayList); } }package com.w3schools; … Read more

Short arraylist using comparator in java

We can use Collections.sort() method to sort arraylist using comparator in java. We have to pass Comparator object in it. Example: package com.w3schools;   import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List;   class Employee{ private String name; private int salary;   public Employee(String name, int salary){ this.name = name; this.salary = salary; }   … Read more

Get sublist from arraylist in java

We can get sublist from arraylist using subList() method by passing the start and end index of the range. Syntax: arrayList.subList(startIndex, endIndex); Example: package com.w3schools;   import java.util.ArrayList; import java.util.List;   public class Test { public static void main(String args[]){ ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Jai"); arrayList.add("Mahesh"); arrayList.add("Vivek"); arrayList.add("Naren"); arrayList.add("Hemant"); arrayList.add("Vishal"); System.out.println("Actual ArrayList:"+arrayList); List<String> list … Read more

Arraylist to array in java

The toArray() method is used to convert arraylist to array in java. Syntax: arrayList.toArray(strArray); Example: package com.w3schools;   import java.util.ArrayList;   public class Test { public static void main(String args[]){ ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Jai"); arrayList.add("Mahesh"); arrayList.add("Vivek"); arrayList.add("Naren"); arrayList.add("Hemant"); arrayList.add("Vishal"); System.out.println("Actual ArrayList:"+arrayList); String[] strArray = new String[arrayList.size()]; arrayList.toArray(strArray); System.out.println("Created Array content:"); for(String string:strArray){ System.out.println(string); … Read more

Remove all elements from arraylist in java

The clear() method is used to remove all elements from arraylist in java. Syntax: arrayList.clear(); Example: package com.w3schools;   import java.util.ArrayList;   public class Test { public static void main(String args[]){ ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Jai"); arrayList.add("Mahesh"); arrayList.add("Vivek"); arrayList.add("Naren"); arrayList.add("Hemant"); arrayList.add("Vishal"); System.out.println("Actual ArrayList:"+arrayList); arrayList.clear(); System.out.println("ArrayList after clear :"+arrayList); } }package com.w3schools; import java.util.ArrayList; public … Read more

Add all elements of a list to arraylist in java

The addAll() method is used to add all elements of a list to arraylist in java. Syntax: arrayList.addAll(list); Example: package com.w3schools;   import java.util.ArrayList; import java.util.List;   public class Test { public static void main(String args[]){ ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Jai"); arrayList.add("Mahesh"); arrayList.add("Vivek"); arrayList.add("Naren"); arrayList.add("Hemant"); arrayList.add("Vishal"); System.out.println("Actual ArrayList:"+arrayList); List<String> list = new ArrayList<String>(); list.add("Vikas"); … Read more

Copy or clone a arraylist in java

The clone() method is used to copy or clone a arraylist in java. Syntax: list.clone(); Example: package com.w3schools;   import java.util.ArrayList;   public class Test { public static void main(String args[]){ ArrayList<String> list = new ArrayList<String>(); list.add("Jai"); list.add("Mahesh"); list.add("Vivek"); list.add("Naren"); list.add("Hemant"); list.add("Vishal"); System.out.println("Actual ArrayList:"+list); ArrayList<String> copy = (ArrayList<String>) list.clone(); System.out.println("Cloned ArrayList:"+copy); } }package com.w3schools; import … Read more

Display arraylist values by using iterator in java

Example: package com.w3schools;   import java.util.ArrayList; import java.util.Iterator;   public class Test { public static void main(String args[]){ ArrayList<String> list = new ArrayList<String>(); list.add("Jai"); list.add("Mahesh"); list.add("Vivek"); list.add("Naren"); list.add("Hemant"); list.add("Vishal"); Iterator<String> iterator = list.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } } }package com.w3schools; import java.util.ArrayList; import java.util.Iterator; public class Test { public static void main(String args[]){ ArrayList<String> list = … Read more

Get sub list from vector in java

The subList() method is used to get sub list from vector in java. Syntax: Vector.subList(int startIndex, int endIndex) Example: package com.w3schools;   import java.util.List; import java.util.Vector;   public class Test { public static void main(String args[]){ Vector<String> vector = new Vector<String>(); //adding elements to the end vector.add("Jai"); vector.add("Mahesh"); vector.add("Hemant"); vector.add("Vishal"); vector.add("Vivek"); vector.add("Naren"); System.out.println("Actual vector:"+vector); List<String> … Read more

Copy vector to array in java

The copyInto() method is used to copy vector to array in java. Syntax: vector.copyInto(copyArray); Example: package com.w3schools;   import java.util.Vector;   public class Test { public static void main(String args[]){ Vector<String> vector = new Vector<String>(); //adding elements to the end vector.add("Jai"); vector.add("Mahesh"); vector.add("Hemant"); vector.add("Vishal"); System.out.println("Actual vector:"+vector); String[] copyArray = new String[vector.size()]; vector.copyInto(copyArray); System.out.println("Copied Array content:"); … Read more