Collections and JDK8

Java – Introduction to Collection Framework

Introduction: Banking Application – Store customer’s information and transactions information.             Customers use Banking application to communicate with Banking Employee. How to store information in application? What are Data Structures? Use? Linear Data Structures: arrange the data sequentially in which elements are connected to its previous and next adjacent. Non-Linear Data Structures: in which one …

Java – Introduction to Collection Framework Read More »

Java – Wrapper Classes

Wrapper classes: Note: for every primitive type there is a wrapper class in java Primitive type Wrapper class byte Byte short Short int Integer long Long char Character float Float double Double boolean Boolean Boxing: Conversion of primitive type into object type int x = 10; Integer obj = new Integer(x); Un boxing: Conversion of …

Java – Wrapper Classes Read More »

Java – Generics

Generics: Collection without Generics: Allow to store any type of Objects. Syntax:            Collection c = new Collection();            c.add(10);            c.add(23.45);            c.add(“java”); Collection with Generics: Allow only specific type of data Objects. Syntax:            Collection<Integer> c = new Collection<Integer>();            c.add(10);            c.add(“java”);  // Error : Collection with Generics that allows any type of object: Syntax:            Collection<Object> c = new …

Java – Generics Read More »

Java – ArrayList

ArrayList: Methods: Name Description int size() Returns the number of elements in this list. boolean add(E e) Appends the specified element to the end of this list Object remove(int index) Removes the element at the specified position in this list void clear() Removes all of the elements from this list void add(int index, E element) …

Java – ArrayList Read More »

Java – Programs using ArrayList Methods

Program to display ArrayList and its size: import java.util.*;class Code{            public static void main(String[] args) {                        List<Integer> list = new ArrayList<Integer>();                        list.add(10);                        list.add(20);                        list.add(30);                        list.add(40);                        list.add(50);                        System.out.println(“List is : ” + list);                        System.out.println(“Size is : ” + list.size());            }} Program to check the list is empty or not: import java.util.*;class Code{            public static void main(String[] …

Java – Programs using ArrayList Methods Read More »

Java – For Each Loop

For-each loop: Limitations: Syntax: for (datatype var : Array/Collection ){            statements ;} Program to display ArrayList using for-each loop: import java.util.*;class Code{            public static void main(String[] args) {                        List<Integer> list = new ArrayList<Integer>();                        for(int i=1 ; i<=5 ; i++)                                    list.add(i*5);                         System.out.println(“List is : “);                        for(Integer x : list)                                    System.out.println(x);            }} Display ArrayList element by element …

Java – For Each Loop Read More »

Java – Iterator Interface

Iterator: Methods: Program to display ArrayList using Iterator: import java.util.*;class Code{            public static void main(String[] args)            {                        List<Integer> list = new ArrayList<Integer>();                        for(int i=1 ; i<=5 ; i++)                                    list.add(i*5);                                               System.out.println(“Display using Iterator :”);                        Iterator<Integer> itr = list.iterator();                        while(itr.hasNext())                        {                                    Integer ele = itr.next();                                    System.out.println(ele);                        }            }} When we use for/for-each/iterator? For-loop For-each loop Iterator Index …

Java – Iterator Interface Read More »

Java – ListIterator

ListIterator: Iterator List in Forward Direction using hasNext() and next() methods: List<Integer> list = new ArrayList<Integer>();for(int i=1 ; i<=5 ; i++){            list.add(i*5);}ListIterator<Integer> itr = list.listIterator();while(itr.hasNext()){            System.out.println(itr.next());} Iterator List in Backward Direction using hasPrevious() and previous() methods: List<Integer> list = new ArrayList<Integer>();for(int i=1 ; i<=5 ; i++){            list.add(i*5);}ListIterator<Integer> itr = list.listIterator(list.size());while(itr.hasPrevious()){            System.out.println(itr.previous());} Display list from specified …

Java – ListIterator Read More »

Java – List of Employee Objects

List of Objects: Program to create and display List of Employees: Employee.java: class Employee{            int id;            String name;            double salary;            Employee(int id, String name, double salary) {                        this.id = id;                        this.name = name;                        this.salary = salary;            }} Main.java: import java.util.*;class Main{            public static void main(String[] args) {                        List<Employee> list = new ArrayList<Employee>();                         Employee e1 = …

Java – List of Employee Objects Read More »

Scroll to Top