LinkedList Class

LinkedList Class Example with Iterator

In this blog, we will see how to LinkedList class work with Iterator class with hasNext() and next() method.

In this blog, we will also see how to add data in first and last location of list using addFirst() and addLast() methods.

These two function do not work in ArrayList class.

 

Let’s see Example on LinkedList Class

 

import java.util.*;

class collection7

{

                public static void main(String args[])

                {

                                LinkedList<String> list1 = new LinkedList<String>();

 

                                list1.add(“Rakesh Shah”);

                                list1.add(“Vipin Singh”);

                                // We can add data in 1st and last position

                                list1.addFirst(“Hitesh Vyas”);

                                list1.addLast(“Vipul Chaudhary”);

                                System.out.println(list1);

 

                                Iterator itr = list1.iterator();

                                while(itr.hasNext())

                                {

                                                System.out.println(((String)itr.next()).toLowerCase());

                                }

                }

}

Output

[Hitesh Vyas, Rakesh Shah,  Vipin Singh, Vipul Chaudhary ]

hitesh vyas

rakesh shah

vipin singh

vipul chaudhary

 

 

For more videos on Java visit our YouTube Channel

 

Earn Money Online

1516total visits,1visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Comments are closed.