ArrayList Tutorial




import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import java.util.ListIterator;

import java.util.Optional;

import java.util.stream.Collectors; 

public class Test {

public static void main(String[] args) {

 

// This is a syntax of generic collection because we define type as String

ArrayList<String> list = new ArrayList<>();

 

// add string type of objects into a collection

boolean addfirst = list.add("suresh"); // 0 index

boolean addsecond = list.add("ashok"); // 1 index

 

// add item in specific position

list.add(1, "harry");

 

// iterate a collection

Iterator<String> iterator = list.iterator();

 

while (iterator.hasNext()) {

// In case of generic collection we dont need to convert explicit to perform some other logic.

String item =iterator.next();

System.out.println(item); 

}

 

// remove an item from collection based on index

String removeitem=list.remove(0);

 

// remove an item from collection based on object

boolean status=list.remove("harry");

 

// get item from collection based on index

String getitem=list.get(0);

 

// get position of an first item

int index=list.indexOf("suresh");

 

// get position of a last item

int lastindex=list.lastIndexOf("suresh");

 

// get sub list

List<String> subitems=list.subList(0, 2);

 

// clear all items from a collection

list.clear();

 

// check collection is empty or not

boolean check=list.isEmpty();

 

// get the length of a collection

int length=list.size();

 

// search an item from collection

boolean exist=list.contains("suresh");

 

// iterate a collection forward and backward both direction

ListIterator<String>listIterator=list.listIterator();

// iterate in forward direction

while(listIterator.hasNext()){

System.out.println(listIterator.next());

}

 

// iterate in backward direction

while(listIterator.hasPrevious()){

System.out.println(listIterator.previous());

}

 

// replace item based on position

String replaceitem=list.set(0,"ambuj");

 

// convert a collection into array

Object [] items=list.toArray();

 

ArrayList<String> anothercollection=new ArrayList<>();

anothercollection.add("nitish");

anothercollection.add("punit");

 

// add a collection into another collection

boolean addcollection=list.addAll(anothercollection);

 

// add a collection in given index

boolean addcollectionposition=list.addAll(0, anothercollection);

 

// remove collection from another collection

boolean removecollection=list.removeAll(anothercollection);

 

// get a particular collection from group of collection

boolean retainallcollection=list.retainAll(anothercollection);

 

 

// iterate a collection using lambda expression

list.forEach(x->System.out.println(x));

 

// how to perform filter using lambda expression

Optional<String> data = list.stream().filter(x -> x.equals("auresh")).findAny();

if (data.isPresent()) {

System.out.println("exist");

} else {

System.out.println("not exist");

}

 

// how to perform filter with limit using lambda expression

long count=list.stream().filter(x->x.length()>3).limit(2).count();

System.out.println(count);

 

// how to get list based on filter using lambda expression

List<String> itemsascollection=list.stream().filter(x->x.length()>0).collect(Collectors.toList());

itemsascollection.forEach(x->System.out.println(x));

 

// Parallel Stream with filter using lambda expression

List<String> ll=list.parallelStream().filter(x->x.contains("suresh")).collect(Collectors.toList());

System.out.println(ll.size()); 

}

}

 


No comments: