Click the below Image to Enlarge
Friday, 31 October 2014
Java : Collection Framework : ArrayList Hierarchy
Click the below Image to Enlarge
Saturday, 25 October 2014
Java : Collection Framework : LinkedList (Remove Group of Objects)
LinkedListRemoveAllExample.java
import java.util.ArrayList; import java.util.LinkedList; import java.util.List; /* * Example of removeAll(Collection<?> c) method */ public class LinkedListRemoveAllExample { public static void main(String[] args) { LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add("Ram"); linkedList.add("Dave"); linkedList.add("Peter"); linkedList.add("Julia"); linkedList.add("Akram"); System.out.println("linkedList : " + linkedList + "\n"); List<String> removeList = new ArrayList<String>(); removeList.add("Dave"); removeList.add("Peter"); System.out.println("RemoveList : " + removeList + "\n"); /* * Remove group of elements from the linkedList */ linkedList.removeAll(removeList); System.out .println("linkedList After removing elements which are present in the removeList : " + linkedList + "\n"); } }
linkedList : [Ram, Dave, Peter, Julia, Akram] RemoveList : [Dave, Peter] linkedList After removing elements which are present in the removeList : [Ram, Julia, Akram]
https://sites.google.com/site/javaee4321/java-collections/LinkedListDemoRemoveAll.zip?attredirects=0&d=1
See also:
Java : Collection Framework : LinkedList (Remove Object)
LinkedListRemoveExample.java
import java.util.LinkedList; /* * Example of remove(int index) and remove(Object o) methods */ public class LinkedListRemoveExample { public static void main(String[] args) { LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add("Ram"); linkedList.add("Dave"); linkedList.add("Peter"); linkedList.add("Julia"); linkedList.add("Akram"); System.out.println("linkedList : " + linkedList + "\n"); /* * Remove element from the index position 1. */ linkedList.remove(1); System.out .println("linkedList After removing element from the index 1 : " + linkedList + "\n"); /* * Remove the String object Julia. */ linkedList.remove("Julia"); System.out .println("linkedList After removing String object \"Julia\" : " + linkedList); } }
linkedList : [Ram, Dave, Peter, Julia, Akram] linkedList After removing element from the index 1 : [Ram, Peter, Julia, Akram] linkedList After removing String object "Julia" : [Ram, Peter, Akram]
https://sites.google.com/site/javaee4321/java-collections/LinkedListDemoRemove.zip?attredirects=0&d=1
See also:
Java : Collection Framework : LinkedList (Add Group of Objects based on Index)
LinkedListAddAllIndexBasedExample.java
import java.util.HashSet; import java.util.LinkedList; import java.util.Set; /* * Example of addAll(Collection<? extends E> c) method */ public class LinkedListAddAllIndexBasedExample { public static void main( String[] args ) { LinkedList<Integer> linkedList = new LinkedList<Integer>(); linkedList.add(1); linkedList.add(2); linkedList.add(3); System.out.println("linkedList : " + linkedList); Set<Integer> set = new HashSet<Integer>(); set.add(4); set.add(5); System.out.println("set : " + set); /* * In Position 1 adding Set collection elements to the list collection */ linkedList.addAll(1,set); System.out.println("linkedList : " + linkedList); } }
linkedList : [1, 2, 3] set : [4, 5] linkedList : [1, 4, 5, 2, 3]
https://sites.google.com/site/javaee4321/java-collections/LinkedListDemoAddAllIndex.zip?attredirects=0&d=1
See also:
Friday, 24 October 2014
Java : Collection Framework : LinkedList (Add Object based on Index)
Click here to watch in Youtube : https://www.youtube.com/watch?v=kvtH4b6UO_A
LinkedListAddIndexBasedExample.java
import java.util.LinkedList; /* * Example of add(int index, E element) method */ public class LinkedListAddIndexBasedExample { public static void main( String[] args ) { LinkedList<Integer> linkedList = new LinkedList<Integer>(); linkedList.add(1); linkedList.add(2); linkedList.add(3); System.out.println("linkedList : " + linkedList); /* * In Position 2 adding 10 */ linkedList.add(2, 10); System.out.println("linkedList : " + linkedList); } }
linkedList : [1, 2, 3] linkedList : [1, 2, 10, 3]
https://sites.google.com/site/javaee4321/java-collections/LinkedListDemoAddIndex.zip?attredirects=0&d=1
See also:
Saturday, 18 October 2014
Java : Collection Framework : LinkedList (Constructors)
Click here to watch in Youtube : https://www.youtube.com/watch?v=5ztiuM7_b7A
Click the below Image to Enlarge
Java : Collection Framework : LinkedList |
Java : Collection Framework : LinkedList |
Friday, 17 October 2014
Java : Collection Framework : LinkedList (Add Group of Objects)
Click here to watch in Youtube : https://www.youtube.com/watch?v=l-Y57lQT6lM
LinkedListAddAllListExample.java
import java.util.LinkedList; /* * Example of addAll(Collection<? extends E> c) method */ public class LinkedListAddAllListExample { public static void main( String[] args ) { LinkedList<Integer> linkedList1 = new LinkedList<Integer>(); linkedList1.add(1); linkedList1.add(2); linkedList1.add(3); System.out.println("linkedList1 : " + linkedList1); LinkedList<Integer> linkedList2 = new LinkedList<Integer>(); linkedList2.add(4); linkedList2.add(5); System.out.println("linkedList2 : " + linkedList2); /* * Adding linkedList2 collection elements to the linkedList1 collection */ linkedList1.addAll(linkedList2); System.out.println("linkedList1 : " + linkedList1); } }
linkedList1 : [1, 2, 3] linkedList2 : [4, 5] linkedList1 : [1, 2, 3, 4, 5]
LinkedListAddAllSetExample.java
import java.util.HashSet; import java.util.LinkedList; import java.util.Set; /* * Example of addAll(Collection<? extends E> c) method */ public class LinkedListAddAllSetExample { public static void main( String[] args ) { LinkedList<Integer> linkedList = new LinkedList<Integer>(); linkedList.add(1); linkedList.add(2); linkedList.add(3); System.out.println("linkedList : " + linkedList); Set<Integer> set = new HashSet<Integer>(); set.add(4); set.add(5); System.out.println("Set : " + set); /* * Adding Set collection elements to the list collection */ linkedList.addAll(set); System.out.println("linkedList : " + linkedList); } }
linkedList : [1, 2, 3] Set : [4, 5] linkedList : [1, 2, 3, 4, 5]
https://sites.google.com/site/javaee4321/java-collections/LinkedListDemoAddAll.zip?attredirects=0&d=1
See also:
Java : Collection Framework : LinkedList (Add User Defined Object)
public class Student { private String name; private int age; public Student(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + "]"; } }
import java.util.LinkedList; /* * Storing user-defined class objects. */ public class LinkedListExample { public static void main(String[] args) { LinkedList<Student> linkedList = new LinkedList<Student>(); Student john = new Student("John", 32); Student david = new Student("David", 42); Student peter = new Student("Peter", 52); linkedList.add(john); linkedList.add(david); linkedList.add(peter); /* * Using for each loop getting each student object from the linkedList */ for (Student student : linkedList) { System.out.println(student.toString()); } } }
Student [name=John, age=32] Student [name=David, age=42] Student [name=Peter, age=52]
https://sites.google.com/site/javaee4321/java-collections/LinkedListDemoUserDefined.zip?attredirects=0&d=1
See also:
Java : Collection Framework : LinkedList (Get each element using for-each loop)
Click here to watch in Youtube : https://www.youtube.com/watch?v=-UQw_YRP72c
LinkedListForEachExample.java
import java.util.LinkedList; public class LinkedListForEachExample { public static void main( String[] args ) { LinkedList<Integer> linkedList = new LinkedList<Integer>(); linkedList.add(100); linkedList.add(200); linkedList.add(300); linkedList.add(400); /* * Using for-each loop getting each element from the linkedList. */ for( Integer value : linkedList ) { System.out.println(value); } } }
100 200 300 400
https://sites.google.com/site/javaee4321/java-collections/LinkedListDemoForeach.zip?attredirects=0&d=1
See also:
Java : Collection Framework : LinkedList (Add Object)
LinkedListExample.java
import java.util.LinkedList; /* * Example of add(E e) method. */ public class LinkedListExample { public static void main( String[] args ) { LinkedList<Integer> linkedList = new LinkedList<Integer>(); /* * Appends the specified element to the end of this list. */ linkedList.add(100); linkedList.add(200); linkedList.add(300); linkedList.add(400); System.out.println("linkedList : "+linkedList); } }
linkedList : [100, 200, 300, 400]
https://sites.google.com/site/javaee4321/java-collections/LinkedListDemoAdd.zip?attredirects=0&d=1
See also:
Java : Collection Framework : ArrayList (Add or Replace element using ListIterator)
ArrayListExample.java
Output
import java.util.ArrayList; import java.util.ListIterator; /* * Example of add(E e) and set(E e) methods of ListIterator */ public class ArrayListExample { public static void main( String[] args ) { ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Ram"); arrayList.add("Dave"); arrayList.add("Peter"); arrayList.add("Julia"); arrayList.add("Akram"); System.out.println("arrayList : " + arrayList + "\n"); ListIterator<String> listIterator = arrayList.listIterator(); /* * Using ListIterator move the cursor in forward direction and add * element and replace element. */ while( listIterator.hasNext() ) { int indexPosition = listIterator.nextIndex(); if( indexPosition == 2 ) { /* * Inserts the specified element into the list */ listIterator.add("Virat"); } String name = listIterator.next(); if( name.equalsIgnoreCase("Ram") ) { /* * Replaces the last element returned by next() or previous() * with the specified element */ listIterator.set("Stephan"); } } System.out.println("List : " + arrayList + "\n"); } }
arrayList : [Ram, Dave, Peter, Julia, Akram] List : [Stephan, Dave, Virat, Peter, Julia, Akram]
https://sites.google.com/site/javaee4321/java-collections/ArrayListDemoListIteratorAddSet.zip?attredirects=0&d=1
See also:
Java : Collection Framework : ArrayList (ListIterator Index)
import java.util.ArrayList; import java.util.ListIterator; /* * Example of listIterator(int index) method */ public class ArrayListExample { public static void main(String[] args) { ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Ram"); arrayList.add("Dave"); arrayList.add("Peter"); arrayList.add("Julia"); arrayList.add("Akram"); System.out.println("arrayList : " + arrayList + "\n"); ListIterator<String> listIterator = arrayList.listIterator(2); /* * Using ListIterator move the cursor in forward direction and get each * element. */ System.out.println("Forward Direction -----" + "\n"); while (listIterator.hasNext()) { int nextIndex = listIterator.nextIndex(); String name = listIterator.next(); System.out.println(nextIndex + " : " + name); } } }
arrayList : [Ram, Dave, Peter, Julia, Akram] Forward Direction ----- 2 : Peter 3 : Julia 4 : Akram
https://sites.google.com/site/javaee4321/java-collections/ArrayListDemoListIteratorIndex.zip?attredirects=0&d=1
See also:
Java : Collection Framework : ArrayList (Remove elements using ListIterator)
import java.util.ArrayList; import java.util.ListIterator; /* * Example of remove() method of ListIterator */ public class ArrayListExample { public static void main(String[] args) { ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Ram"); arrayList.add("Dave"); arrayList.add("Peter"); arrayList.add("Julia"); arrayList.add("Akram"); System.out.println("arrayList : " + arrayList + "\n"); ListIterator<String> listIterator = arrayList.listIterator(); /* * Using ListIterator move the cursor in forward direction and remove * element one by one. */ while (listIterator.hasNext()) { listIterator.next(); /* * Removes from the list the last element that was returned by * next() or previous() methods */ listIterator.remove(); } System.out.println("arrayList : " + arrayList + "\n"); } }
arrayList : [Ram, Dave, Peter, Julia, Akram] arrayList : []
https://sites.google.com/site/javaee4321/java-collections/ArrayListDemoListIteratorRemove.zip?attredirects=0&d=1
See also:
Friday, 10 October 2014
Java : Collection Framework : ArrayList (ListIterator)
ArrayListExample.java
import java.util.ArrayList; import java.util.ListIterator; /* * Example of how to use ListIterator */ public class ArrayListExample { public static void main( String[] args ) { ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Ram"); arrayList.add("Dave"); arrayList.add("Peter"); arrayList.add("Julia"); arrayList.add("Akram"); System.out.println("arrayList : " + arrayList + "\n"); ListIterator<String> listIterator = arrayList.listIterator(); /* * Using ListIterator move the cursor in forward direction and get each * element. */ System.out.println("Forward Direction -----" + "\n"); while( listIterator.hasNext() ) { int nextIndex = listIterator.nextIndex(); String name = listIterator.next(); System.out.println(nextIndex + " : " + name); } System.out.println("\n" + "##############################" + "\n"); /* * Using ListIterator move the cursor in reverse direction and get each * element. */ System.out.println("Reverse Direction -----" + "\n"); while( listIterator.hasPrevious() ) { int previousIndex = listIterator.previousIndex(); String name = listIterator.previous(); System.out.println(previousIndex + " : " + name); } } }
arrayList : [Ram, Dave, Peter, Julia, Akram] Forward Direction ----- 0 : Ram 1 : Dave 2 : Peter 3 : Julia 4 : Akram ############################## Reverse Direction ----- 4 : Akram 3 : Julia 2 : Peter 1 : Dave 0 : Ram
https://sites.google.com/site/javaee4321/java-collections/ArrayListDemoListItrerator.zip?attredirects=0&d=1
See also:
Java : Collection Framework : ArrayList (Remove elements using Iterator)
ArrayListIteratorRemoveExample.java
import java.util.ArrayList; import java.util.Iterator; /* * Example of Iterator remove() method */ public class ArrayListIteratorRemoveExample { public static void main( String[] args ) { ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Ram"); arrayList.add("Dave"); arrayList.add("Peter"); arrayList.add("Julia"); arrayList.add("Akram"); System.out.println("arrayList : " + arrayList + "\n"); Iterator<String> iterator = arrayList.iterator(); while( iterator.hasNext() ) { iterator.next(); /* * Removes from the list the last element that was returned by * next() */ iterator.remove(); } System.out.println("arrayList : " + arrayList + "\n"); } }
arrayList : [Ram, Dave, Peter, Julia, Akram] arrayList : []
https://sites.google.com/site/javaee4321/java-collections/ArrayListDemoItrRemove.zip?attredirects=0&d=1
See also:
Java : Collection Framework : ArrayList (Convert to Array Object)
ArrayListToArrayExample.java
Output
import java.util.ArrayList; /* * Example of toArray(T[] a) method */ public class ArrayListToArrayExample { public static void main(String[] args) { ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Ram"); arrayList.add("Dave"); arrayList.add("Peter"); arrayList.add("Julia"); arrayList.add("Akram"); System.out.println("ArrayList : " + arrayList + "\n"); String[] strArray = new String[arrayList.size()]; /* * arrayList elements will be copied to StrArray */ arrayList.toArray(strArray); for (String name : strArray) { System.out.println(name); } } }
arrayList : [Ram, Dave, Peter, Julia, Akram]
Ram
Dave
Peter
Julia
Akram
import java.util.ArrayList; /* * Example of toArray() method */ public class ArrayListToObjArrayExample { public static void main( String[] args ) { ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Ram"); arrayList.add("Dave"); arrayList.add("Peter"); arrayList.add("Julia"); arrayList.add("Akram"); System.out.println("arrayList : " + arrayList + "\n"); /* * Returns an array containing all of the elements in this list in * proper sequence (from first to last element). */ Object[] objArray = arrayList.toArray(); for( Object object : objArray ) { String name = (String) object; System.out.println(name); } } }
arrayList : [Ram, Dave, Peter, Julia, Akram]
Ram
Dave
Peter
Julia
Akram
https://sites.google.com/site/javaee4321/java-collections/ArrayListDemoToArray.zip?attredirects=0&d=1
See also: