Skip to main content

LinkedList Collection in java

LinkedList is Type of collection.

public class LinkedList
extends AbstractSequentialList
implements List, Deque, Cloneable, Serializable

Overloaded constructor in LinkedList


1. public LinkedList()
Constructs an empty list.

2. public LinkedList(Collection c)

LinkedList Method


Reference link:
https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html

As we know LinkedList implements both List and Queue interface
so you can use method of both list and queue
.

1. boolean add(E e)
Appends the specified element to the end of this list.

2. void add(int index, E element)
Inserts the specified element at the specified position in this list.

3. boolean addAll(Collection c)
Appends all of the elements in the specified collection to the end of this list,
in the order that they are returned by the specified collection’s iterator.

4. boolean addAll(int index, Collection c)
Inserts all of the elements in the specified collection into this list,
starting at the specified position.

5. void addFirst(E e)
Inserts the specified element at the beginning of this list.

6. void addLast(E e)
Appends the specified element to the end of this list.

7. void clear()
Removes all of the elements from this list.

8. Object clone()
Returns a shallow copy of this LinkedList.

9. boolean contains(Object o)
Returns true if this list contains the specified element.

10. Iterator descendingIterator()
Returns an iterator over the elements in this deque in reverse sequential order.

11. E element()
Retrieves, but does not remove, the head (first element) of this list.

12. E get(int index)
Returns the element at the specified position in this list.

13. E getFirst()
Returns the first element in this list.

14. E getLast()
Returns the last element in this list.

15. int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list,
or -1 if this list does not contain the element.

16. int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list,
or -1 if this list does not contain the element.

17. ListIterator listIterator(int index)
Returns a list-iterator of the elements in this list (in proper sequence),
starting at the specified position in the list.

18. boolean offer(E e)
Adds the specified element as the tail (last element) of this list.

19. boolean offerFirst(E e)
Inserts the specified element at the front of this list.

20. boolean offerLast(E e)
Inserts the specified element at the end of this list.

21. E peek()
Retrieves, but does not remove, the head (first element) of this list.

22. E peekFirst()
Retrieves, but does not remove, the first element of this list, or returns null if this list is empty.

23. E peekLast()
Retrieves, but does not remove, the last element of this list, or returns null if this list is empty.

24. E poll()
Retrieves and removes the head (first element) of this list.

25. E pollFirst()
Retrieves and removes the first element of this list, or returns null if this list is empty.

26. E pollLast()
Retrieves and removes the last element of this list, or returns null if this list is empty.

27. E pop()
Pops an element from the stack represented by this list.

28. void push(E e)
Pushes an element onto the stack represented by this list.

29. E remove()
Retrieves and removes the head (first element) of this list.

30. E remove(int index)
Removes the element at the specified position in this list.

31. boolean remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present.

32. E removeFirst()
Removes and returns the first element from this list.

33. boolean removeFirstOccurrence(Object o)
Removes the first occurrence of the specified element in this list (when traversing the list from head to tail).

34. E removeLast()
Removes and returns the last element from this list.

35. boolean removeLastOccurrence(Object o)
Removes the last occurrence of the specified element in this list (when traversing the list from head to tail).

36. E set(int index, E element)
Replaces the element at the specified position in this list with the specified element.

37. int size()
Returns the number of elements in this list.

38. Object[] toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).

39. T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element);
the runtime type of the returned array is that of the specified array.

Let’s Understand LinkedList Method with Example

1. boolean add(E e)
Appends the specified element to the end of this list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class AddElemnt 
{
 
	public static void main(String[] args) {
 
 
		LinkedList<String> list=new LinkedList<String>();
 
 
		//Adding element into LinkedList
 
			list.add("Bhavesh");
 
			list.add("Abhi");
 
			list.add("vivek");
 
			list.add("milan");
 
 
			//Retrieving Array list Element
			for (String s : list)
			{
 
				System.out.println(s);
			}
 
	}
}

output

1
2
3
4
Bhavesh
Abhi
vivek
milan

2. void add(int index, E element)
Inserts the specified element at the specified position in this list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class AddElemntThroughIndex 
{
 
	public static void main(String[] args) 
	{
 
 
		LinkedList<String> list=new LinkedList<String>();
 
	   //Adding element into LinkedList
 
		list.add("Bhavesh");
 
		list.add("Abhi");
 
		list.add("vivek");
 
		list.add("milan");
 
		System.out.println("Size of LinkedList: "+list.size());
 
		for (String s : list) 
		{
 
			System.out.println(s);
		}
 
 
		//Adding elemnt at Index 1
 
		list.add(1,"Vaibhav");
 
		System.out.println("After adding elemnt at index 1");
 
		System.out.println("Size of LinkedList: "+list.size());
 
 
		for (String s : list) 
		{
 
			System.out.println(s);
		}
 
	 }
}

output

1
2
3
4
5
6
7
8
9
10
11
12
Size of LinkedList: 4
Bhavesh
Abhi
vivek
milan
After adding elemnt at index 1
Size of LinkedList: 5
Bhavesh
Vaibhav
Abhi
vivek
milan

3. boolean addAll(Collection c)
Appends all of the elements in the specified collection to the end of this list,
in the order that they are returned by the specified collection’s iterator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
 
public class AddAllMethod 
{
 
	public static void main(String[] args) 
	{
 
		LinkedList<String> list1=new LinkedList<String>();
 
        list1.add("Bhavesh");
        list1.add("Abhilash");
 
 
        //retrieving list1 element
 
        System.out.println("Retrieving list 1 element");
        for (String s : list1) 
        {
 
        	System.out.println(s);
 
		}
 
 
        LinkedList<String> list2=new LinkedList<String>();
 
        list2.add("Vivek");
        list2.add("milan");
 
        //Adding All element of list 1 into list 2
 
        list2.addAll(list1);
 
 
        System.out.println("Retrieving list 2:");
 
        for (String s : list2) 
        {
 
        	System.out.println(s);
		}
	}
}

output

1
2
3
4
5
6
7
8
Retrieving list 1 element
Bhavesh
Abhilash
Retrieving list 2:
Vivek
milan
Bhavesh
Abhilash

4. boolean addAll(int index, Collection c)
Inserts all of the elements in the specified collection into this list,
starting at the specified position.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.spy.method.LinkedList;
 
import java.util.ArrayList;
import java.util.LinkedList;
 
public class AddAllThroughindex 
{
	public static void main(String[] args) 
 
	{
 
		LinkedList<String> list1=new LinkedList<String>();
 
      list1.add("Bhavesh");
      list1.add("Abhilash");
 
 
      //retrieving list1 element
 
      System.out.println("Retrieving list 1 element");
      for (String s : list1) 
      {
 
      	System.out.println(s);
 
      }
 
 
      LinkedList<String> list2=new LinkedList<String>();
 
      list2.add("Vivek");
      list2.add("milan");
 
      //Adding collection into list 2 at index 0
 
      list2.addAll(0,list1);
 
      System.out.println("Retrieving list 2:");
 
      for (String s : list2) 
      {
 
      	System.out.println(s);
 
	  }
 
	}
}

output

1
2
3
4
5
6
7
8
Retrieving list 1 element
Bhavesh
Abhilash
Retrieving list 2:
Bhavesh
Abhilash
Vivek
milan

5. void addFirst(E e)
Inserts the specified element at the beginning of this list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class AddFirst 
{
 
public static void main(String[] args) 
{
 
		LinkedList<String> list=new LinkedList<String>();
 
 
		//Adding element into LinkedList
 
			list.add("Bhavesh");
 
			list.add("Abhi");
 
			list.add("vivek");
 
			list.add("milan");
 
 
			//Retrieving Linkedlist Element
			for (String s : list)
			{
 
				System.out.println(s);
			}
 
 
			list.addFirst("alex");
			System.out.println("After adding new Element at First position..");
 
			//Retrieving LinkedList Element
			for (String s : list)
			{
 
				System.out.println(s);
			}
	}
}

output

1
2
3
4
5
6
7
8
vivek
milan
After adding new Element at First position..
alex
Bhavesh
Abhi
vivek
milan

6. void addLast(E e)
Appends the specified element to the end of this list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class AddLast
{
 
public static void main(String[] args) 
{
 
		LinkedList<String> list=new LinkedList<String>();
 
 
		//Adding element into LinkedList
 
			list.add("Bhavesh");
 
			list.add("Abhi");
 
			list.add("vivek");
 
			list.add("milan");
 
 
			//Retrieving Linkedlist Element
			for (String s : list)
			{
 
				System.out.println(s);
			}
 
 
			list.addLast("John");
			System.out.println("After adding new Element at Last position..");
 
			//Retrieving LinkedList Element
			for (String s : list)
			{
 
				System.out.println(s);
			}
 
	}
}

output

1
2
3
4
5
6
7
8
vivek
milan
After adding new Element at Last position..
Bhavesh
Abhi
vivek
milan
John

7. void clear()
Removes all of the elements from this list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 
//This Example shows the use of isEmpty and Clear method of LinkedList
 
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class Clear 
{
 
	public static void main(String[] args) {
 
		System.out.println("Starting Program");
 
		LinkedList<String> emp_list=new LinkedList<>();
 
		emp_list.add("Bhavesh Lakhani");
		emp_list.add("Abhilash Shah");
		emp_list.add("Nilay Shah");
		emp_list.add("Vaibhav Prajapati");
 
 
 
		//Clearing LinkedList
		  emp_list.clear();
 
		System.out.println("After clearing LinkedList");
 
		//Retrieving LinkedList
		  for (String s : emp_list) 
			{
 
				System.out.println(s);
			}
 
 
		  //method returns true if this list contains no elements
 
		   boolean b=emp_list.isEmpty();
		   System.out.println("LinkedList Empty :"+b);
 
 
		   System.out.println("ending program");
	  }
}

output

1
2
3
4
Starting Program
After clearing LinkedList
LinkedList Empty :true
ending program

8. Object clone()
Returns a shallow copy of this LinkedList.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.spy.method.LinkedList;
 
import java.util.ArrayList;
import java.util.LinkedList;
 
public class Clone 
{
 
	public static void main(String[] args)
	{
 
		LinkedList<String> emp_list=new LinkedList<>();
 
		emp_list.add("Bhavesh Lakhani");
		emp_list.add("Abhilash Shah");
		emp_list.add("Nilay Shah");
		emp_list.add("Vaibhav Prajapati");
 
		//Retrieving LinkedList element
 
		for (String s : emp_list) 
		{
 
			System.out.println(s);
		}
 
		//Making shallow copy
 
		LinkedList<String> copy_emp=(LinkedList)emp_list.clone();	
 
		System.out.println("Retrieving element from shallo copy of Arraylist");
 
		//Retrieving Arraylist element of shallow copy
		for (String s : copy_emp) 
		{
 
			System.out.println(s);
		}
 
	}
 
}

output

1
2
3
4
5
6
7
8
Abhilash Shah
Nilay Shah
Vaibhav Prajapati
Retrieving element from shallo copy of Arraylist
Bhavesh Lakhani
Abhilash Shah
Nilay Shah
Vaibhav Prajapati

9. boolean contains(Object o)
Returns true if this list contains the specified element.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class ContainMethod 
{
 
	public static void main(String[] args) 
	{
 
		LinkedList<String> emp_list=new LinkedList<>();
 
		emp_list.add("Bhavesh Lakhani");
		emp_list.add("Abhilash Shah");
		emp_list.add("Nilay Shah");
		emp_list.add("Vaibhav Prajapati");
 
		//This method returns true if element present in Arraylist
		boolean b=emp_list.contains("Bhavesh Lakhani");
 
	    if(b)
	    {
	    	System.out.println("Element present in LinkedList");
	    }
 
	    else
	    {
	    	System.out.println("Element not present in LinkedList");
	    }
 
	}
}

output

1
Element present in LinkedList

10. Iterator descendingIterator()
Returns an iterator over the elements in this deque in reverse sequential order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.spy.method.LinkedList;
 
import java.util.Iterator;
import java.util.LinkedList;
 
public class DescendingIterator 
{
 
	public static void main(String[] args) 
	{
 
 
	LinkedList<String> list=new LinkedList<String>();
 
 
	//Adding element into LinkedList
 
		list.add("Bhavesh");
 
		list.add("Abhi");
 
		list.add("vivek");
 
		list.add("milan");
 
 
		Iterator<String> it=list.descendingIterator();
 
		while(it.hasNext())
		{
 
			System.out.println(it.next());
		}
 
	}
}

output

1
2
3
4
milan
vivek
Abhi
Bhavesh

11. E element()
Retrieves, but does not remove, the head (first element) of this list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class ElementMethod 
{
 
	public static void main(String[] args) 
	{
 
		LinkedList<String> list=new LinkedList<String>();
 
 
		//Adding element into LinkedList
 
			list.add("Bhavesh");
 
			list.add("Abhi");
 
			list.add("vivek");
 
			list.add("milan");
 
 
			//Retrieving LinkedList
			for (String s : list) 
			{
 
				System.out.println(s);
			}
 
 
			System.out.println("Retrieving element");
 
			//element method returns element at first position
			String emp=list.element();
 
			System.out.println(emp);
 
 
	}
}

output

1
2
3
4
5
6
Bhavesh
Abhi
vivek
milan
Retrieving element
Bhavesh

12. E get(int index)
Returns the element at the specified position in this list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.spy.method.LinkedList;
 
import java.util.ArrayList;
import java.util.LinkedList;
 
public class GetElemnt 
{
	public static void main(String[] args) 
 
	{
 
		LinkedList<String> list=new LinkedList<String>();
 
 
		list.add("Bhavesh");
 
		list.add("Abhi");
 
		list.add("vivek");
 
		list.add("milan");
 
 
 
		//getting element at an index 0
		System.out.println("Getting element at index 0: "+list.get(0));
 
 
		System.out.println("Retrieving LinkedList element using for loop ");
		System.out.println("****************************************");
 
	   // Retrieving LinkedList element using for loop
 
		for(int i=0;i<list.size();i++)
		{
 
			String name=list.get(i);
 
			System.out.println(name);
 
		}	
 
	}
}

output

1
2
3
4
5
6
7
Getting element at index 0: Bhavesh
Retrieving LinkedList element using for loop 
****************************************
Bhavesh
Abhi
vivek
milan

13. E getFirst()
Returns the first element in this list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class Getfirst 
{
 
	public static void main(String[] args) 
	{
 
		LinkedList<String> list=new LinkedList<String>();
 
 
		//Adding element into LinkedList
 
			list.add("Bhavesh");
 
			list.add("Abhi");
 
			list.add("vivek");
 
			list.add("milan");
 
 
			//Retrieving LinkedList
			for (String s : list) 
			{
 
				System.out.println(s);
			}
 
 
			System.out.println("getting element at first position");
 
			//element method returns element at first position
			String emp=list.getFirst();
 
			System.out.println(emp);
 
 
	}
}

output

1
2
3
4
5
6
Bhavesh
Abhi
vivek
milan
getting element at first position
Bhavesh

14. E getLast()
Returns the last element in this list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class GetLast 
{
 
	public static void main(String[] args) 
	{
 
		LinkedList<String> list=new LinkedList<String>();
 
 
		//Adding element into LinkedList
 
			list.add("Bhavesh");
 
			list.add("Abhi");
 
			list.add("vivek");
 
			list.add("milan");
 
 
			//Retrieving LinkedList
			for (String s : list) 
			{
 
				System.out.println(s);
			}
 
 
			System.out.println("getting element at Last position");
 
			//element method returns element at last position
			String emp=list.getLast();
 
			System.out.println(emp);
	  }
}

output

1
2
3
4
5
6
Bhavesh
Abhi
vivek
milan
getting element at Last position
milan

15. int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list,
or -1 if this list does not contain the element.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.spy.method.LinkedList;
 
import java.util.ArrayList;
import java.util.LinkedList;
 
public class indexofMethod 
{
	public static void main(String[] args) 
	{
 
		LinkedList<String> emp_list=new LinkedList<>();
 
		emp_list.add("Bhavesh Lakhani");
		emp_list.add("Abhilash Shah");
		emp_list.add("Nilay Shah");
		emp_list.add("Vaibhav Prajapati");
 
 
	   int index=emp_list.indexOf("Bhavesh Lakhani");
 
	   System.out.println("Index number of Bhavesh Lakhani: "+index);
 
 
 
	   int i=emp_list.indexOf("Vaibhav Prajapati");
 
	   System.out.println("Index number of Vaibhav Prajapati: "+i);
 
	}
}

output

1
2
Index number of Bhavesh Lakhani: 0
Index number of Vaibhav Prajapati: 3

16. int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list,
or -1 if this list does not contain the element.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.spy.method.LinkedList;
 
 
import java.util.LinkedList;
 
public class LastIndex 
{
 
	public static void main(String[] args) 
	{
 
		LinkedList<String> emp_list=new LinkedList<>();
 
			emp_list.add("Bhavesh Lakhani");
			emp_list.add("Abhilash Shah");
			emp_list.add("Nilay Shah");
			emp_list.add("Bhavesh Lakhani");
			emp_list.add("Vaibhav Prajapati");
 
	 	   System.out.println("Retrieving element........");	
 
		 for (String s : emp_list) 
		 {
 
			 System.out.println(s);
 
		 }	
 
		//getting the last index of Object or element 
		int index= emp_list.lastIndexOf("Bhavesh Lakhani");
 
		System.out.println("last index of Bhavesh Lakhani: "+index);
 
 
	}
}

output

1
2
3
4
5
6
7
Retrieving element........
Bhavesh Lakhani
Abhilash Shah
Nilay Shah
Bhavesh Lakhani
Vaibhav Prajapati
last index of Bhavesh Lakhani: 3

17. ListIterator listIterator(int index)
Returns a list-iterator of the elements in this list (in proper sequence),
starting at the specified position in the list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.spy.method.LinkedList;
 
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
 
 
public class ListIteratorMethod
{
 
	public static void main(String[] args) 
	{
 
 
		LinkedList<String> list=new LinkedList<String>();
 
 
		//Adding element into LinkedList
 
			list.add("Bhavesh");
 
			list.add("Abhi");
 
			list.add("vivek");
 
			list.add("milan");
 
		ListIterator<String> it= list.listIterator();	
 
 
		System.out.println("Retrieving element forward direction");
		while(it.hasNext())
		{
 
			System.out.println(it.next());
		}
 
		System.out.println("Retrieving element in reverse direction");
		while(it.hasPrevious())
		{
 
			System.out.println(it.previous());
		}
	}
}

output

1
2
3
4
5
6
7
8
9
10
Retrieving element forward direction
Bhavesh
Abhi
vivek
milan
Retrieving element in reverse direction
milan
vivek
Abhi
Bhavesh

18. boolean offer(E e)
Adds the specified element as the tail (last element) of this list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class OfferMethod 
{
 
	public static void main(String[] args) 
	{
 
			LinkedList<String> list=new LinkedList<String>();
 
 
			//Adding element into LinkedList
 
				list.add("Bhavesh");
 
				list.add("Abhi");
 
				list.add("vivek");
 
				list.add("milan");
 
 
				//Retrieving Linkedlist Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
 
 
				list.offer("Nikul");
				System.out.println("After adding new Element at Tail ..");
 
				//Retrieving LinkedList Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
 
 
		}
 
}
 
Output:
vivek
milan
After adding new Element at Tail ..
Bhavesh
Abhi
vivek
milan
Nikul

19. boolean offerFirst(E e)
Inserts the specified element at the front of this list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class offerFirst 
{
 
	public static void main(String[] args) 
	{
 
			LinkedList<String> list=new LinkedList<String>();
 
 
			//Adding element into LinkedList
 
				list.add("Bhavesh");
 
				list.add("Abhi");
 
				list.add("vivek");
 
				list.add("milan");
 
 
				//Retrieving Linkedlist Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
 
 
				list.offerFirst("Ankur");
				System.out.println("After adding new Element at First position ..");
 
				//Retrieving LinkedList Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
 
 
		}
}

output

1
2
3
4
5
6
7
8
vivek
milan
After adding new Element at First position ..
Ankur
Bhavesh
Abhi
vivek
milan

20. boolean offerLast(E e)
Inserts the specified element at the end of this list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class Offerlast 
{
 
	public static void main(String[] args) 
	{
 
			LinkedList<String> list=new LinkedList<String>();
 
 
			//Adding element into LinkedList
 
				list.add("Bhavesh");
 
				list.add("Abhi");
 
				list.add("vivek");
 
				list.add("milan");
 
 
				//Retrieving Linkedlist Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
 
 
				list.offerLast("Sahil");
				System.out.println("After adding new Element at last position ..");
 
				//Retrieving LinkedList Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
 
		}
}

output

1
2
3
4
5
6
7
8
vivek
milan
After adding new Element at last position ..
Bhavesh
Abhi
vivek
milan
Sahil

21. E peek()
Retrieves, but does not remove, the head (first element) of this list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class PeekMethod 
{
 
	public static void main(String[] args) 
	{
 
			LinkedList<String> list=new LinkedList<String>();
 
 
			//Adding element into LinkedList
 
				list.add("Bhavesh");
 
				list.add("Abhi");
 
				list.add("vivek");
 
				list.add("milan");
 
 
				//Retrieving Linkedlist Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
 
				//getting first element from list
				String f=list.peek();
				System.out.println("First element: "+f);
 
 
		}
 
}

output

1
2
3
4
5
Bhavesh
Abhi
vivek
milan
First element: Bhavesh

22. E peekFirst()
Retrieves, but does not remove, the first element of this list, or returns null if this list is empty.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class Peekfirst 
{
 
	public static void main(String[] args) 
	{
 
			LinkedList<String> list=new LinkedList<String>();
 
 
			//Adding element into LinkedList
 
				list.add("Bhavesh");
 
				list.add("Abhi");
 
				list.add("vivek");
 
				list.add("milan");
 
 
				//Retrieving Linkedlist Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
 
				//getting first element from list
				String f=list.peekFirst();
				System.out.println("First element: "+f);
 
 
		}
 
}

output

1
2
3
4
5
Bhavesh
Abhi
vivek
milan
First element: Bhavesh

23. E peekLast()
Retrieves, but does not remove, the last element of this list, or returns null if this list is empty.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class PeekLast 
{
 
	public static void main(String[] args) 
	{
 
			LinkedList<String> list=new LinkedList<String>();
 
 
			//Adding element into LinkedList
 
				list.add("Bhavesh");
 
				list.add("Abhi");
 
				list.add("vivek");
 
				list.add("milan");
 
 
				//Retrieving Linkedlist Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
 
				//getting first element from list
				String f=list.peekLast();
				System.out.println("Last element: "+f);
 
	}
}

output

1
2
3
4
5
Bhavesh
Abhi
vivek
milan
Last element: milan

24. E poll()
Retrieves and removes the head (first element) of this list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class PollMethod 
{
 
	public static void main(String[] args) 
	{
 
			LinkedList<String> list=new LinkedList<String>();
 
 
			//Adding element into LinkedList
 
				list.add("Bhavesh");
 
				list.add("Abhi");
 
				list.add("vivek");
 
				list.add("milan");
 
 
				//Retrieving Linkedlist Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
 
				//getting first element from list
				String f=list.poll();
				System.out.println("Poll element: "+f);
 
 
				System.out.println("After polling element");
 
				//Retrieving Linkedlist Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
 
 
		}
}

output

1
2
3
4
5
6
7
8
Abhi
vivek
milan
Poll element: Bhavesh
After polling element
Abhi
vivek
milan

25. E pollFirst()
Retrieves and removes the first element of this list, or returns null if this list is empty.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class Pollfirst 
{
 
	public static void main(String[] args) 
	{
 
			LinkedList<String> list=new LinkedList<String>();
 
 
			//Adding element into LinkedList
 
				list.add("Bhavesh");
 
				list.add("Abhi");
 
				list.add("vivek");
 
				list.add("milan");
 
 
				//Retrieving Linkedlist Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
 
				//getting first element from list
				String f=list.pollFirst();
				System.out.println("Poll First element: "+f);
 
 
				System.out.println("After polling element");
 
				//Retrieving Linkedlist Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
 
 
		}
}

output

1
2
3
4
5
6
7
8
Abhi
vivek
milan
Poll First element: Bhavesh
After polling element
Abhi
vivek
milan

26. E pollLast()
Retrieves and removes the last element of this list, or returns null if this list is empty.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class PollLast 
{
 
	public static void main(String[] args) 
	{
 
			LinkedList<String> list=new LinkedList<String>();
 
 
			//Adding element into LinkedList
 
				list.add("Bhavesh");
 
				list.add("Abhi");
 
				list.add("vivek");
 
				list.add("milan");
 
 
				//Retrieving Linkedlist Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
 
				//getting first element from list
				String f=list.pollLast();
				System.out.println("Poll Last element: "+f);
 
 
				System.out.println("After polling element");
 
				//Retrieving Linkedlist Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
		}
}

output

1
2
3
4
5
6
7
8
Abhi
vivek
milan
Poll Last element: milan
After polling element
Bhavesh
Abhi
vivek

27. E pop()
Pops an element from the stack represented by this list.

1
 

output

1
 

28. void push(E e)
Pushes an element onto the stack represented by this list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class PushMethod 
{
 
	public static void main(String[] args) 
	{
 
			LinkedList<String> list=new LinkedList<String>();
 
 
			//Adding element into LinkedList
 
				list.add("Bhavesh");
 
				list.add("Abhi");
 
				list.add("vivek");
 
				list.add("milan");
 
 
				//Retrieving Linkedlist Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
 
				//pushing element
 
				list.push("Maulik");
 
 
				System.out.println("After pushing element");
 
				//Retrieving Linkedlist Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
		}
}

output

1
2
3
4
5
6
7
8
9
10
Bhavesh
Abhi
vivek
milan
After pushing element
Maulik
Bhavesh
Abhi
vivek
milan

29. E remove()
Retrieves and removes the head (first element) of this list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.spy.method.LinkedList;
 
 
import java.util.LinkedList;
 
public class Remove 
{
 
	public static void main(String[] args) 
	{
 
        LinkedList<String> emp_list=new LinkedList<>();
 
		emp_list.add("Bhavesh Lakhani");
		emp_list.add("Abhilash Shah");
		emp_list.add("Nilay Shah");
		emp_list.add("Bhavesh Lakhani");
		emp_list.add("Vaibhav Prajapati");
 
 
		System.out.println("Retrieving Element......");
 
		for (String s : emp_list) 
		{
		  System.out.println(s);
		}
 
		   emp_list.remove();
 
 
		System.out.println("After Removing ");
 
		for (String s : emp_list) 
		{
			System.out.println(s);
		}	
 
	}
}

output

1
2
3
4
5
6
7
8
9
10
11
Retrieving Element......
Bhavesh Lakhani
Abhilash Shah
Nilay Shah
Bhavesh Lakhani
Vaibhav Prajapati
After Removing 
Abhilash Shah
Nilay Shah
Bhavesh Lakhani
Vaibhav Prajapati

30. E remove(int index)
Removes the element at the specified position in this list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.spy.method.LinkedList;
 
import java.util.ArrayList;
import java.util.LinkedList;
 
public class RemoveElement 
{
 
	public static void main(String[] args) 
	{
 
		// Adding and Removing element from Arraylist
 
		LinkedList<String> list=new LinkedList<String>();
 
 
				//Adding element into Arraylist
 
				list.add("Bhavesh");
 
				list.add("Abhi");
 
				list.add("vivek");
 
				list.add("milan");
 
				System.out.println("size of LinkedList "+list.size());
 
				System.out.println("Retrieving LinkedList element");
 
				for(int i=0;i<list.size();i++)
				{
 
					String name=list.get(i);
 
					System.out.println(name);
 
				}
				//removing LinkedList element from index 2
 
				list.remove(2);
 
 
 
				System.out.println("After removing element from index 2");
 
				System.out.println("size of arraylist "+list.size());
 
				for(int i=0;i<list.size();i++)
				{
 
					String name=list.get(i);
 
					System.out.println(name);
 
				}
		}
}

output

1
2
3
4
5
6
7
8
Abhi
vivek
milan
After removing element from index 2
size of arraylist 3
Bhavesh
Abhi
milan

31. boolean remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.spy.method.LinkedList;
 
 
import java.util.LinkedList;
 
public class Remove2 
{
 
	public static void main(String[] args) 
	{
 
        LinkedList<String> emp_list=new LinkedList<>();
 
		emp_list.add("Bhavesh Lakhani");
		emp_list.add("Abhilash Shah");
		emp_list.add("Nilay Shah");
		emp_list.add("Bhavesh Lakhani");
		emp_list.add("Vaibhav Prajapati");
 
 
		System.out.println("Retrieving Element......");
 
		for (String s : emp_list) 
		{
		  System.out.println(s);
		}
 
		// This method removes the first occurrence of the specified element from this list, if it is present.
		   emp_list.remove("Bhavesh Lakhani");
 
 
		System.out.println("After Removing first occurence of Bhavesh Lakhani");
 
		for (String s : emp_list) 
		{
			System.out.println(s);
		}	
 
	}
}

output

1
2
3
4
5
6
7
8
Nilay Shah
Bhavesh Lakhani
Vaibhav Prajapati
After Removing first occurence of Bhavesh Lakhani
Abhilash Shah
Nilay Shah
Bhavesh Lakhani
Vaibhav Prajapati

32. E removeFirst()
Removes and returns the first element from this list.

1
 

output

1
 

33. boolean removeFirstOccurrence(Object o)
Removes the first occurrence of the specified element in this list (when traversing the list from head to tail).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class RemoveFirstOccurrence
{
 
	public static void main(String[] args) 
	{
 
			LinkedList<String> list=new LinkedList<String>();
 
 
			//Adding element into LinkedList
 
				list.add("Bhavesh");
 
				list.add("Abhi");
 
				list.add("vivek");
 
				list.add("Bhavesh");// 2nd Occurrence 
 
				list.add("milan");
 
 
				//Retrieving Linkedlist Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
 
				//Removing first Occurrence of Bhavesh
 
				list.removeFirstOccurrence("Bhavesh");
 
				System.out.println("After Removing first Occurrence of Bhavesh");
 
				//Retrieving Linkedlist Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
 
	    }
}

output

1
2
3
4
5
6
7
8
vivek
Bhavesh
milan
After Removing first Occurrence of Bhavesh
Abhi
vivek
Bhavesh
milan

35. E removeLast()
Removes and returns the last element from this list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class RemoveLast 
{
 
	public static void main(String[] args) 
	{
 
			LinkedList<String> list=new LinkedList<String>();
 
 
			//Adding element into LinkedList
 
				list.add("Bhavesh");
 
				list.add("Abhi");
 
				list.add("vivek");
 
				list.add("milan");
 
 
				//Retrieving Linkedlist Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
 
				//Removing first element from list
				String f=list.removeLast();
 
				System.out.println("After Removing Last Element");
 
				//Retrieving Linkedlist Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
 
	    }
}

output

1
2
3
4
5
6
7
8
Bhavesh
Abhi
vivek
milan
After Removing Last Element
Bhavesh
Abhi
vivek

36. boolean removeLastOccurrence(Object o)
Removes the last occurrence of the specified element in this list (when traversing the list from head to tail).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class RemoveLastOccurence
{
 
	public static void main(String[] args) 
	{
 
			LinkedList<String> list=new LinkedList<String>();
 
 
			//Adding element into LinkedList
 
				list.add("Bhavesh");
 
				list.add("Abhi");
 
				list.add("vivek");
 
				list.add("Bhavesh");// 2nd Occurrence 
 
				list.add("milan");
 
 
				//Retrieving Linkedlist Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
 
				//Removing last Occurrence of Bhavesh
 
				list.removeLastOccurrence("Bhavesh");
 
				System.out.println("After Removing last Occurrence of Bhavesh");
 
				//Retrieving Linkedlist Element
				for (String s : list)
				{
 
					System.out.println(s);
				}
 
 
	    }
}

output:

1
2
3
4
5
6
7
8
vivek
Bhavesh
milan
After Removing last Occurrence of Bhavesh
Bhavesh
Abhi
vivek
milan

37. E set(int index, E element)
Replaces the element at the specified position in this list with the specified element.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class Set 
{
 
public static void main(String[] args) 
{
 
		LinkedList<String> list=new LinkedList<String>();
 
 
		//Adding element into LinkedList
 
			list.add("Bhavesh");
 
			list.add("Abhi");
 
			list.add("vivek");
 
			list.add("milan");
 
 
			//Retrieving Linkedlist Element
			for (String s : list)
			{
 
				System.out.println(s);
			}
 
 
	    	list.set(0,"Bhavesh Lakhani");
 
			//Retrieving LinkedList Element
			for (String s : list)
			{
 
				System.out.println(s);
			}
	}
}

output

1
2
3
4
5
6
7
8
Bhavesh
Abhi
vivek
milan
Bhavesh Lakhani
Abhi
vivek
milan

38. int size()
Returns the number of elements in this list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class AddElemnt 
{
 
	public static void main(String[] args) {
 
 
		LinkedList<String> list=new LinkedList<String>();
 
 
		//Adding element into LinkedList
 
			list.add("Bhavesh");
 
			list.add("Abhi");
 
			list.add("vivek");
 
			list.add("milan");
 
			int size=list.size();
 
			System.out.println("list size: "+size);
 
			//Retrieving Array list Element
			for (String s : list)
			{
 
				System.out.println(s);
			}
 
	}
}

output

1
2
3
4
5
list size: 4
Bhavesh
Abhi
vivek
milan

39. Object[] toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class ToArray 
{
 
public static void main(String[] args) 
{
 
		LinkedList<String> list=new LinkedList<String>();
 
 
		//Adding element into LinkedList
 
			list.add("Bhavesh");
 
			list.add("Abhi");
 
			list.add("vivek");
 
			list.add("milan");
 
 
			//Retrieving Linkedlist Element
			for (String s : list)
			{
 
				System.out.println(s);
			}
 
 
	        Object[] ob=list.toArray();
 
			//Retrieving Array element
			for (Object s : ob)
			{
 
				System.out.println(s);
			}
	}
}

output

1
2
3
4
5
6
7
8
Bhavesh
Abhi
vivek
milan
Bhavesh
Abhi
vivek
milan

40. T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element);
the runtime type of the returned array is that of the specified array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class ToArrayMethod 
{
 
public static void main(String[] args) 
{
 
		LinkedList<String> list=new LinkedList<String>();
 
 
		//Adding element into LinkedList
 
			list.add("Bhavesh");
 
			list.add("Abhi");
 
			list.add("vivek");
 
			list.add("milan");
 
 
			//Retrieving Linkedlist Element
			for (String s : list)
			{
 
				System.out.println(s);
			}
 
 
 
			String[] s={"Hi","Hello"};
 
 
	       String [] l= list.toArray(s);
 
			//Retrieving Array element
			for (String s1 : l)
			{
 
				System.out.println(s1);
			}
	}
}

output

1
2
3
4
5
6
7
8
Bhavesh
Abhi
vivek
milan
Bhavesh
Abhi
vivek
milan