Monday, 13 January 2025

Reduce method in java8

 The reduce() method in Java 8 Streams is a terminal operation that takes a binary operator and applies it repeatedly to the elements of the stream to reduce the stream to a single result. It's a very powerful method for performing aggregation operations like summing, multiplying, concatenating, and many others

Signature of reduce():

Optional<T> reduce(BinaryOperator<T> accumulator);

T reduce(T identity, BinaryOperator<T> accumulator);

1. Optional<T> reduce(BinaryOperator<T> accumulator):

This version of reduce() does not have an identity element and returns an Optional<T>. This is because the result might be empty if the stream is empty.


2. T reduce(T identity, BinaryOperator<T> accumulator):

This version includes an identity value, which is a default value that is returned if the stream is empty. This version returns the result directly, not wrapped in an Optional.


BinaryOperator:


A BinaryOperator<T> is a functional interface that takes two arguments of type T and returns a result of type T.


Example Usage of reduce():


Example 1: Sum of Numbers


Let’s start by using reduce() to calculate the sum of numbers in a list.


import java.util.*;

import java.util.stream.*;


public class Main {

    public static void main(String[] args) {

        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);


        // Using reduce to sum all elements in the list

        int sum = numbers.stream()

                          .reduce(0, (a, b) -> a + b); // Identity is 0, accumulator is a + b


        System.out.println("Sum: " + sum); // Output: 15

    }

}


Explanation:


The identity value is 0, so if the stream is empty, the result will be 0.


The accumulator function (a, b) -> a + b sums the elements in the stream.



Example 2: Multiplication of Numbers


Here’s how you can use reduce() to multiply the elements of a stream.


import java.util.*;

import java.util.stream.*;


public class Main {

    public static void main(String[] args) {

        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);


        // Using reduce to multiply all elements in the list

        int product = numbers.stream()

                             .reduce(1, (a, b) -> a * b); // Identity is 1, accumulator is a * b


        System.out.println("Product: " + product); // Output: 120

    }

}


Explanation:


The identity value is 1 because multiplying by 1 does not change the result.


The accumulator function (a, b) -> a * b multiplies the elements in the stream.



Example 3: Concatenating Strings


You can also use reduce() to concatenate strings in a list.


import java.util.*;

import java.util.stream.*;


public class Main {

    public static void main(String[] args) {

        List<String> words = Arrays.asList("Java", "8", "Streams");


        // Using reduce to concatenate strings with a space

        String result = words.stream()

                             .reduce("", (a, b) -> a + " " + b).trim(); // Identity is "", accumulator is a + " " + b


        System.out.println("Concatenated: " + result); // Output: Java 8 Streams

    }

}


Explanation:


The identity value is an empty string "".


The accumulator function (a, b) -> a + " " + b adds a space between each string element.


The trim() removes the leading space.



Example 4: Finding Maximum Value


Let’s use reduce() to find the maximum value in a stream.


import java.util.*;

import java.util.stream.*;


public class Main {

    public static void main(String[] args) {

        List<Integer> numbers = Arrays.asList(5, 12, 3, 7, 8);


        // Using reduce to find the maximum element

        int max = numbers.stream()

                         .reduce(Integer.MIN_VALUE, (a, b) -> a > b ? a : b); // Identity is Integer.MIN_VALUE


        System.out.println("Maximum value: " + max); // Output: 12

    }

}


Explanation:


The identity value is Integer.MIN_VALUE, ensuring that any value in the stream will be greater than it.


The accumulator function (a, b) -> a > b ? a : b compares the elements and keeps the larger one.



Example 5: Handling Optional with reduce()


If you don’t provide an identity value, reduce() will return an Optional<T> because the stream may be empty.


import java.util.*;

import java.util.stream.*;


public class Main {

    public static void main(String[] args) {

        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);


        // Using reduce to find the sum of numbers with Optional return type

        Optional<Integer> sum = numbers.stream()

                                       .reduce((a, b) -> a + b);


        sum.ifPresent(value -> System.out.println("Sum: " + value)); // Output: Sum: 15

    }

}


Explanation:


Since there is no identity value, the result is wrapped in an Optional to handle the case where the stream might be empty.


We use ifPresent() to print the result.



Why Use reduce()?


Aggregation: It’s used for any kind of aggregation, such as summing, multiplying, or finding minimum/maximum values.


Immutable result: reduce() returns a single result (a reduced value), which can be useful when you need to compute a cumulative value.


Versatility: You can use it for a wide range of operations, from simple mathematical operations to complex transformations.



Common Use Cases of reduce():


Summing numbers.


Multiplying numbers.


Concatenating strings.


Finding minimum/maximum values.


Combining lists or collections into a single object.



Important Points:


1. Identity: The identity value should be chosen carefully as it serves as the starting point for the aggregation and also the default value if the stream is empty.



2. Optional: Without an identity, the result is wrapped in an Optional to handle the case of an empty stream.



3. Associativity: The reduce() operation should be associative, meaning the result should be the same regardless of the order in which the elements are combined. This is important when using parallel streams.




Conclusion:


The reduce() method is a

 powerful and flexible way to perform aggregation on streams in Java 8. Whether you're summing, multiplying, concatenating, or finding max/min values, reduce() is essential for many operations.


Sunday, 15 December 2024

Sed command

 

The sed command in Unix is a stream editor used for performing basic text transformations on an input stream (a file or input from a pipeline). It is often used for tasks such as search and replace, deleting lines, inserting lines, and more.

Syntax of sed command

sed [options] 'command' file
  • command: The operation to perform on the input text.
  • file: The file on which sed will operate. If no file is specified, sed reads from standard input (stdin).
  • options: Optional flags that modify sed's behavior.

Common sed Commands and Usage:

1. Search and Replace

The most common use of sed is for search and replace. It uses the syntax:

sed 's/old_text/new_text/' file
  • s/old_text/new_text/: This command searches for old_text and replaces it with new_text.

Example:


sed 's/apple/orange/' fruits.txt

This will replace the first occurrence of the word "apple" with "orange" in each line of fruits.txt


2 .Global Replacement

To replace all occurrences of a pattern in a line, use the g flag:



sed 's/old_text/new_text/g' file

Example:

sed 's/apple/orange/g' fruits.txt
  • This will replace all occurrences of "apple" with "orange" in each line of fruits.txt

Custom Comparator using java8 streams

 



l

Why override equals() and hashCode()?

  • equals(): Determines whether two objects are considered equal. When you add an object to a Set, the set uses this method to check if an object already exists in the set.
  • hashCode(): Provides a hash code that is used for efficient lookups in hash-based collections like HashSet. If you override equals(), you should also override hashCode() to maintain the general contract between these methods.



1 ) import java.util.HashSet;

import java.util.Set;


class Person {

    private String name;

    private int age;


    // Constructor

    public Person(String name, int age) {

        this.name = name;

        this.age = age;

    }


    // Getters

    public String getName() {

        return name;

    }


    public int getAge() {

        return age;

    }


    // Override equals() to compare name and age for equality

    @Override

    public boolean equals(Object o) {

        if (this == o) return true;

        if (o == null || getClass() != o.getClass()) return false;

        Person person = (Person) o;

        return age == person.age && name.equals(person.name);

    }


    // Override hashCode() to generate a consistent hash code based on name and age

    @Override

    public int hashCode() {

        return 31 * name.hashCode() + Integer.hashCode(age);

    }


    @Override

    public String toString() {

        return "Person{name='" + name + "', age=" + age + '}';

    }

}


public class Main {

    public static void main(String[] args) {

        // Create a Set of Person objects

        Set<Person> people = new HashSet<>();


        // Add some custom objects to the Set

        people.add(new Person("Alice", 30));

        people.add(new Person("Bob", 25));

        people.add(new Person("Alice", 30)); // Duplicate (same name and age)

        people.add(new Person("Charlie", 35));


        // Output the set to see unique objects

        for (Person person : people) {

            System.out.println(person);

        }

    }

}

 


2 )

import java.util.Set;

import java.util.TreeSet;

import java.util.Comparator;


class Person {

    private String name;

    private int age;


    // Constructor

    public Person(String name, int age) {

        this.name = name;

        this.age = age;

    }


    // Getters

    public String getName() {

        return name;

    }


    public int getAge() {

        return age;

    }


    @Override

    public String toString() {

        return "Person{name='" + name + "', age=" + age + '}';

    }

}


public class Main {

    public static void main(String[] args) {

        // Comparator to sort by name in ascending order

        Comparator<Person> nameComparator = (p1, p2) -> p1.getName().compareTo(p2.getName());


        // Create a TreeSet with a custom comparator (sorting by name)

        Set<Person> people = new TreeSet<>(nameComparator);


        // Add some custom objects to the Set

        people.add(new Person("Alice", 30));

        people.add(new Person("Bob", 25));

        people.add(new Person("Charlie", 35));


        // Output the set to see ordered elements

        for (Person person : people) {

            System.out.println(person);

        }

    }

}



3 ) 

import java.util.*;
import java.util.stream.Collectors;

class Person {
    private String name;
    private int age;

    // Constructor
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Getters
    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    // Override equals() and hashCode() for unique elements in Set
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && name.equals(person.name);
    }

    @Override
    public int hashCode() {
        return 31 * name.hashCode() + Integer.hashCode(age);
    }

    @Override
    public String toString() {
        return "Person{name='" + name + "', age=" + age + '}';
    }
}

public class Main {
    public static void main(String[] args) {
        List<Person> peopleList = Arrays.asList(
                new Person("Alice", 30),
                new Person("Bob", 25),
                new Person("Alice", 30),  // Duplicate
                new Person("Charlie", 35)
        );

        // Using Stream to add unique custom objects to a Set
        Set<Person> uniquePeople = peopleList.stream()
                .collect(Collectors.toSet()); // Collect into a Set to ensure uniqueness

        // Output the unique Set
        uniquePeople.forEach(System.out::println);
    }
}


4 ) 
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        List<Person> peopleList = Arrays.asList(
                new Person("Alice", 30),
                new Person("Bob", 25),
                new Person("Charlie", 35)
        );

        // Using Stream to sort by name (custom Comparator)
        List<Person> sortedByName = peopleList.stream()
                .sorted(Comparator.comparing(Person::getName)) // Sorting by name
                .collect(Collectors.toList());

        // Output the sorted list
        sortedByName.forEach(System.out::println);
    }
}

5 ) import java.util.*;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        List<Person> peopleList = Arrays.asList(
                new Person("Alice", 30),
                new Person("Bob", 25),
                new Person("Charlie", 35)
        );

        // Using Stream to map Person objects to their names
        List<String> names = peopleList.stream()
                .map(Person::getName) // Map Person to String (name)
                .collect(Collectors.toList());

        // Output the mapped names
        names.forEach(System.out::println);
    }
}

Saturday, 14 December 2024

Find the even and odd numbers using streams and multiply even numbers with 3 and odd one with 2

 

Find the even and odd numbers in list along with multiply even number with 3 and odd number with 

To achieve this using Java 8 Streams, there are multiple ways to process the list of integers, separate the odd and even numbers, and perform the required transformations (multiplying even numbers by 3 and odd numbers by 2

1 )


import java.util.*;

import java.util.stream.*;


public class Main {

    public static void main(String[] args) {

        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);


        List<Integer> result = numbers.stream()

            .map(n -> (n % 2 == 0) ? n * 3 : n * 2) // Multiply even by 3 and odd by 2

            .collect(Collectors.toList()); // Collect the results into a list


        System.out.println(result);

    }

}




2 ) import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
        List<Integer> result = new ArrayList<>();

        numbers.stream()
            .forEach(n -> {
                if (n % 2 == 0) {
                    result.add(n * 3); // Multiply even by 3
                } else {
                    result.add(n * 2); // Multiply odd by 2
                }
            });

        System.out.println(result);
    }
}


3 )  import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

        List<Integer> result = numbers.stream()
            .flatMap(n -> Stream.of((n % 2 == 0) ? n * 3 : n * 2)) // FlatMap example
            .collect(Collectors.toList());

        System.out.println(result);
    }
}

4 ) import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

        List<Integer> result = numbers.stream()
            .mapToInt(n -> (n % 2 == 0) ? n * 3 : n * 2) // Use mapToInt for primitive operations
            .boxed() // Box the result back into Integer
            .collect(Collectors.toList());

        System.out.println(result);
    }
}


5 ) import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
        
        Optional<List<Integer>> result = Optional.ofNullable(numbers)
            .filter(list -> !list.isEmpty())
            .map(list -> list.stream()
                .map(n -> (n % 2 == 0) ? n * 3 : n * 2)
                .collect(Collectors.toList()));

        result.ifPresent(System.out::println); // Output the result if present
    }
}

6) import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

        Map<Boolean, List<Integer>> grouped = numbers.stream()
            .collect(Collectors.groupingBy(n -> n % 2 == 0)); // Group by even or odd

        List<Integer> result = new ArrayList<>();
        
        grouped.get(true).forEach(n -> result.add(n * 3)); // Multiply even by 3
        grouped.get(false).forEach(n -> result.add(n * 2)); // Multiply odd by 2

        System.out.println(result);
    }
}

7 ) 
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

        List<Integer> result = numbers.stream()
            .collect(Collectors.mapping(n -> (n % 2 == 0) ? n * 3 : n * 2, Collectors.toList()));

        System.out.println(result);
    }
}


Tuesday, 14 June 2022

Java8::Stream


Java8::Steeam ::

Stream :: A stream is a sequence of data elements supporting sequential and parallel aggregate operations.To perform a computation, stream operations are composed into a stream pipeline. A stream pipeline consists of a source (which might be an array, a collection, a generator function, an I/O channel, etc), zero or more intermediate operations (which transform a stream into another stream, such as filter(Predicate)), and a terminal operation (which produces a result or side-effect, such as count() or forEach(Consumer))

how do streams differ from collections?
Both are abstractions for a collection of data elements. Collections focus on storage of data elements
for efficient access whereas streams focus on aggregate computations on data elements from a data source that is typically, but not necessarily, collections.

features of streams, comparing them with collections when necessary:

 Streams have no storage A collection is an in-memory data structure that stores all its elements. All elements must exist in memory before they are added to the collection. A stream has no storage; it does not store elements. A stream pulls elements from a data source on-demand and passes them to a pipeline of operations for processing.
Streams can represent a sequence of infinite elements : A collection cannot represent a group of infinite elements whereas a stream can. A collection stores all its elements in memory, and therefore, it is not possible to have an infinite number of elements in a collection. Having a collection of an infinite  number of elements will require an infinite amount of memory and the storage process will continue forever. A stream pulls its elements from a data source that can be a collection, a function that generates data, an I/O channel, etc.
 The design of streams is based on internal iteration.
 Streams are designed to be processed in parallel with no additional work from the developers.

 Streams are designed to support functional programming.
 Streams support lazy operations.
 Streams can be ordered or unordered.

 Streams cannot be reused.

How Streams Work Internally

Streams process data in a pipeline consisting of:

  1. Source: Collection, arrays, or any sequence.

  2. Intermediate Operations: Transformations like filter() and map() (Lazy execution).

  3. Terminal Operation: Triggers execution, e.g., forEach(), reduce().

Streams do not store data and process elements one at a time, improving memory efficiency. They often use Spliterators and internal iteration mechanisms, which differ from external loops.


Creating Streams:
There are many ways to create streams. Many existing classes in the Java libraries have received new methods that return a stream. Based on the data source, stream creation can be categorized as follows:


Streams from values: The Stream interface contains the following two static of() methods to create a sequential Stream from a single value and multiple values:
• <T> Stream<T> of(T t)
• <T> Stream<T> of(T...values)

public class Stram {

public static void main(String[] args) {

Stream<String> singleValue=Stream.of("Strmewithsinglevalue");
Stream<String> MutpleValues=Stream.of("x","y","zz","aa");
Stream<Integer> MultPleIntValues=Stream.of(2,4,6,8,10);

}

}

The following snippet of code creates a stream of strings from a String array returned from the split() method of the String class:
String str = "Ken,Jeff,Chris,Ellen";
// The stream will contain fur elements: "Ken", "Jeff", "Chris", and "Ellen"

Stream<String> stream = Stream.of(str.split(","));

Empty Streams::
An empty stream is a stream with no elements. The Stream interface contains an empty() static method to create an empty sequential stream.
// Creates an empty stream of strings
Stream<String> stream = Stream.empty();

The IntStream, LongStream, and DoubleStream interfaces also contain an empty() static method to create an empty stream of primitive types.
// Creates an empty stream of integers

IntStream numbers = IntStream.empty();


The Stream interface also supports creating a stream using the builder pattern using the  Stream.Builder<T> interface whose instance represents a stream builder. The builder() static method of the Stream interface returns a stream builder.
// Gets a stream builder
Stream.Builder<String> builder = Stream.builder();
The Stream.Builder<T> interface contains the following methods:
• void accept(T t)
• Stream.Builder<T> add(T t)

• Stream<T> build()

Stream<String> stream = Stream.<String>builder()
.add("Ken")
.add("Jeff")
.add("Chris")

.add("Ellen")
.build();

(or )
// Obtain a builder
Stream.Builder<String> builder = Stream.builder();
// Add elements and build the stream
Stream<String> stream = builder.add("Ken")
.add("Jeff")
.add("Chris")
.add("Ellen")

.build();

Streams from Functions ::  An infinite stream is a stream with a data source capable of generating infinite number of elements. aying that the data source should be “capable of generating” infinite number of elements, rather the data source should have or contain an infinite number of elements. It is impossible to generate and store an infinite number of elements of any kind because of memory and time constraints. However, it is possible to have a function that can generate infinite number of values on demand.

The Stream interface contains the following two static methods to generate an infinite stream:
• <T> Stream<T> iterate(T seed, UnaryOperator<T> f)
• <T> Stream<T> generate(Supplier<T> s)
The iterator() method creates a sequential ordered stream whereas the generate() method creates a

sequential unordered stream.

Using the Stream.iterate() Method::
The iterator() method takes two arguments: a seed and a function. The first argument is a seed that is the first element of the stream. The second element is generated by applying the function to the first element. The third element is generated by applying the function on the second element and so on. Its elements are seed, f(seed), f(f(seed)), f(f(f(seed))), and so on.

// Creates a stream of natural numbers
Stream<Long> naturalNumbers = Stream.iterate(1L, n -> n + 1);
// Creates a stream of odd natural numbers

Stream<Long> oddNaturalNumbers = Stream.iterate(1L, n -> n + 2);

Stream<Integer> streamIterated = Stream.iterate(40, n -> n + 2).limit(20);

streamIterated.forEach(System.out::println);



























Streams from Arrays::
The Arrays class in the java.util package contains an overloaded stream() static method to create sequential streams from arrays. You can use it to create an IntStream from an int array, a LongStream from a long array, a DoubleStream from a double array, and a Stream<T> from an array of the reference type T. The following snippet of code creates an IntStream and a Stream<String> from an int array and a String array:

// Creates a stream from an int array with elements 1, 2, and 3
IntStream numbers = Arrays.stream(new int[]{1, 2, 3});
// Creates a stream from a String array with elements "Ken", and "Jeff"
Stream<String> names = Arrays.stream(new String[] {"Ken", "Jeff"});

Streams from Collections ::
The Collection interface contains the stream() and parallelStream() methods that create sequential and parallel streams from a Collection, respectively.

import java.util.ArrayList;
import java.util.List;

public class LstStrm {

public static void main(String args[]) {
List<String> names=new ArrayList<String>();
names.add("one");
names.add("two");
names.add("three");
names.add("four");
names.add("five");
System.out.println(" display list of elements in java6 ::"+names);
names.stream().forEach(x->System.out.println("displaying list elements in java8" +x));

}
}

Stream Operations ::
A stream supports two types of operations:
• Intermediate operations
• Terminal operations

Intermediate operations are also known as lazy operations. Terminal operations are also known as eager operations. Operations are known as lazy and eager based on the way they pull the data elements from the data source. A lazy operation on a stream does not process the elements of the stream until another eager operation is called on the stream.
Streams connect though a chain of operations forming a stream pipeline. A stream is inherently lazy until you call a terminal operation on it. An intermediate operation on a stream produces another stream. When you call a terminal operation on a stream, the elements are pulled from the data source and pass through the stream pipeline.
Each intermediate operation takes elements from an input stream and transforms the elements to produce an output stream. The terminal operation takes inputs from a stream and produces the result.


There are multiple ways to create a stream in Java 8. Here are some common approaches:

1 . From a Collection (List, Set, etc.)

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class StreamFromCollection {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie");

        Stream<String> nameStream = names.stream(); // Create stream from List
        nameStream.forEach(System.out::println);
    }
}

2.  Using Stream.of()


import java.util.stream.Stream;

public class StreamOfExample {
    public static void main(String[] args) {
        Stream<String> stream = Stream.of("Apple", "Banana", "Cherry");

        stream.forEach(System.out::println);
    }
}

3 . Using Array 

import java.util.Arrays;
import java.util.stream.Stream;

public class StreamFromArray {
    public static void main(String[] args) {
        String[] fruits = {"Mango", "Orange", "Grapes"};

        Stream<String> fruitStream = Arrays.stream(fruits);
        fruitStream.forEach(System.out::println);
    }
}



4 . Using IntStream, LongStream, DoubleStream (Primitive Streams)

import java.util.stream.IntStream;

public class PrimitiveStreamExample {
    public static void main(String[] args) {
        IntStream.range(1, 6).forEach(System.out::println);
    }
}






Saturday, 10 February 2018

List Interface


List interface::

An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.
The List interface places additional stipulations, beyond those specified in the Collection interface, on the contracts of the iteratoraddremoveequals, and hashCode methods. Declarations for other inherited methods are also included here for convenience.
The List interface provides four methods for positional (indexed) access to list elements. Lists (like Java arrays) are zero based. Note that these operations may execute in time proportional to the index value for some implementations (the LinkedList class, for example). Thus, iterating over the elements in a list is typically preferable to indexing through it if the caller does not know the implementation.
The List interface provides a special iterator, called a ListIterator, that allows element insertion and replacement, and bidirectional access in addition to the normal operations that the Iterator interface provides. A method is provided to obtain a list iterator that starts at a specified position in the list.

ArrayList::
Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is roughly equivalent to Vector, except that it is unsynchronized.)
The sizeisEmptygetsetiterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation.
Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.

How to create Arraylist:

public class LstStrm {

public static void main(String args[]) {
         
      //declare both right and left generics in <> in java6
List<String> names=new ArrayList<String>();
names.add("one");
names.add("two");
names.add("three");
names.add("four");
names.add("five");
System.out.println(" display list of elements in java6 ::"+names);

         // in java7you can't declare generics in right side <> ,compiler infers the left hand generic which type it is.
               List<String> names=new ArrayList<>();
names.add("one");
names.add("two");
names.add("three");
names.add("four");
names.add("five");
System.out.println(" display list of elements in java7 ::"+names);
}
}

LinkedList::

Linked list implementation of the List interface. Implements all optional list operations, and permits all elements (including null). In addition to implementing the List interface, the LinkedList class provides uniformly named methods to getremove and insert an element at the beginning and end of the list.By using this you can add elements in both forward and backward,also add elements using while iterating elements.

LinkedList<String> namesList=new LinkedList<>();
namesList.add("one");
namesList.add("two");
namesList.add("three");
namesList.add("four");
namesList.add("five");
namesList.addFirst("first");
namesList.addLast("lastele");

System.out.println(" display list of elements in java6 ::"+namesList);