java.util.LinkedList Class - HudaTutorials.com

lightnewsSoftware and s/w Development

Nov 18, 2013 (3 years and 6 months ago)

83 views

http://www.hudatutorials.com

Download link:
http://www.hudatutorials.com/ht/java/
util/
linked
list
.html


1


java.util.
Linked
List

Class


The LinkedList class is a doubly linked list, which internally maintains references to the
previous and next element at each node in the list.

Linked list implementation of the List
interface. Implements all optional list opera
tions, and permits all elements (including
null).

The class implements the Deque interface, providing first
-
in
-
first
-
out queue
operations for add, poll, along with other stack and deque operations.


Note that this implementation is not synchronized. If mu
ltiple threads access a linked list
concurrently, and at least one of the threads modifies the list structurally, it must be
synchronized externally.

if the list is structurally modified at any time after the iterator
is created, in any way except through

the Iterator's own remove or add methods, the
iterator will throw a ConcurrentModificationException.


Linked
List

class has following constructors.


LinkedList()

Constructs an empty list.


LinkedList(Collection<? extends E> c)

Constructs a list containing
the elements of the specified collection, in the order they are
returned by the collection's iterator.


In this tutorial you can learn about java.util.
Linked
List

class

and its examples
. And also
learn how to use java.util.
Linked
List

class.


/* java.util.
Li
nked
List

class Example */

/* Save with file name
Linked
List
Example.java */


import java.util.
LinkedList
;


public class
LinkedList
Example

{


public static void main(String args[])


{



//java.util.
LinkedList

DECLARATION



LinkedList

al
;




//java.util.
Linke
dList

OBJECT CREATION



//USING DEFAULT CONSTRUCTOR



al = new
LinkedList
();




//ADD AN ELEMENT



al
.add(
"
Huda Tutorials
"
);



al
.add("
Java

Tutorials");




//DUPLICATES ARE

ALLOWED



al
.add("Huda Tutorials");



al
.add("
C

Tutorials");



al
.add("CPP Tutorial
s");




//null ALLOWED



al.add(
null
);


http://www.hudatutorials.com

Download link:
http://www.hudatutorials.com/ht/java/
util/
linked
list
.html


2




//RETURNS COUNT OF ELEMENTS
LinkedList

CONTAINS



System.out.println(
"Elements Count : " +
al
.size());




//java.util.
LinkedList

OUTPUT



System.out.println
(
al
);


}

}


The following example shows how to use
LinkedL
ist

with Iterator.


/* java.util.
LinkedList

class Example
2
*/

/* Save with file name
LinkedList
Example
2
.java */


import java.util.
LinkedList
;

import java.util.Iterator;


public class
LinkedList
Example
2

{


public static void main(String args[])


{



//java
.util.
LinkedList

DECLARATION



LinkedList

al
;




//java.util.
LinkedList

OBJECT CREATION



//USING DEFAULT CONSTRUCTOR



al

= new
LinkedList
();




//ADD AN ELEMENT



al
.add("Huda Tutorials");



al
.add("Java Tutorials");




//DUPLICATES
ARE

ALLOWED



al
.add(
"Huda Tutorials");



al
.add("C Tutorials");



al
.add("CPP Tutorials");




//null ALLOWED



al.add(null);




//RETURNS COUNT OF ELEMENTS
LinkedList

CONTAINS



System.out.println("Elements Count : " +
al
.size());




//java.util.
LinkedList

OUTPUT



Iterator i
tr =
al
.iterator();



while(itr.has
Next
())



{




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



}


}

}


The following example shows how to use
LinkedList

with
Arrays,
List

and Iterator.


/* java.util.
LinkedList

class Example
3

*/

/* Save with file name
LinkedList
Examp
le
3
.java */


http://www.hudatutorials.com

Download link:
http://www.hudatutorials.com/ht/java/
util/
linked
list
.html


3


import java.util.
LinkedList
;

import java.util.
List
;

import java.util.Arrays;

import java.util.Iterator;


public class
LinkedList
Example
3

{


public static void main(String args[])


{



String elements
[] =
{
"Huda Tutorials","Java Tutorials"
,"C T
utorials"
};



List s
;

//java.util.
List

DECLARATION




//java.util.
List

OBJECT CREATION

USING ARRAY AS LIST



s = new
LinkedList
(
Arrays.asList(elements)
);




//java.util.
List

OUTPUT



Iterator itr =
s.iterator();



while(itr.hasNext())



{




System.out.pri
ntln(itr.next());



}


}

}


/* java.util.
LinkedList

class Example
4

*/

/* Save with file name
LinkedList
Example
4
.java */


import java.util.
LinkedList
;

import java.util.
List
;

import java.util.Arrays;

import java.util.Iterator;


public class
LinkedList
Exampl
e
4

{


public static void main(String args[])


{



String elements[] = {"Huda Tutorials","Java Tutorials","C Tutorials"};



List

s;

//java.util.
List

DECLARATION




//java.util.
List

OBJECT CREATION USING ARRAY AS LIST



s = new
LinkedList
(Arrays.asList(eleme
nts));




//ADD

ELEMENT TO
List

COLLECTION



s.add("NetBeans Tutorials");




//DISPLAY SIZE OF THE
List

COLLECTION



System.out.println("
List

Collection Size : "+s.size());




//CHECK THE
List

COLLECTION IS EMPTY



System.out.println("
List

Collection is Em
pty : "+s.isEmpty());




//CHECK THE GIVEN ELEMENT IN
List

COLLECTION



System.out.println("
\
"Huda Tutorials
\
"

Contains :
"+s.contains("Huda Tutorials"
)
);




//java.util.
List

OUTPUT



System.out.println();

http://www.hudatutorials.com

Download link:
http://www.hudatutorials.com/ht/java/
util/
linked
list
.html


4




System.out.println("
Elements
Before Element
Remov
e
");



System.out.println("
--------------------
----------
");



Iterator itr = s.iterator();



while(itr.hasNext())



{




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



}




s.remove
("C Tutorials");




System.out.println();



System.out.println("
Elements
After

Element
Remove
");



System.out.println("
------------------------------
");



itr = s.iterator();



while(itr.hasNext())



{




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



}


}

}


The following example show
s

how to compare two
List

Collections.


/* java.util.
LinkedList

class
Example
5

*/

/* Save with file name
LinkedList
Example
5
.java */


import java.util.
LinkedList
;

import java.util.
List
;

import java.util.Arrays;

import java.util.Iterator;


public class
LinkedList
Example
5

{


public static void main(String args[])


{



String e
lements[] = {"Huda Tutorials","Java Tutorials","C Tutorials"};



String elements2[] = {"Huda Tutorials","Java Tutorials",
"Android Tutorials"
};




//java.util.
List

DECLARATION



List

s
, s2
;




//java.util.
List

OBJECT CREATION USING ARRAY AS LIST



s = new
L
inkedList
(Arrays.asList(elements));



s2 = new
LinkedList
(Arrays.asList(elements
2
));




//COMPARE TWO COLLECTIONS



System.out.println("Equals : " + s
2.equals(s
));




//CONTAINS COLLECTION IN OTHER COLLECTION



System.out.println("
Contains

: " + s
2
.
contain
s
All
(s
));


}

}

The following example shows how to
save

List

Collection into file
.


/* java.util.
LinkedList

class Example
6

*/

/* Save with file name
LinkedList
Example
6
.java */

http://www.hudatutorials.com

Download link:
http://www.hudatutorials.com/ht/java/
util/
linked
list
.html


5



import java.util.
LinkedList
;

import java.util.
List
;

import java.util.Arrays;

im
port java.io.FileOutputStream;

import java.io.ObjectOutputStream;


public class
LinkedList
Example
6

{


public static void main(String args[])


{



try



{




String elements
[] = {"Huda Tutorials","Java Tutorials","Android Tutorials"};





//java.util.
Linked
List

DECLARATION




List

s;





//java.util.
List

OBJECT CREATION USING ARRAY AS LIST




s = new
LinkedList
(Arrays.asList(elements));





//FileOutputStream CREATION




FileOutputStream fos = new FileOutputStream("
List
.set");





//ObjectOutputStream CREATI
ON




ObjectOutputStream oos = new ObjectOutputStream(fos);





//WRITE
List

OBJECT TO ObjectOutputStream




oos.writeObject(s);






//CLOSE THE ObjectOutputStream




oos.close();




System.out.println("
List

Collection Saved into File Sucessfully");



}



catch(Exception e)



{




System.out.println("Error Occurred : " + e.getMessage());



}


}

}


The following example shows how to
retrieve

List

Collection
from

file.


/* java.util.
LinkedList

class Example
7

*/

/* Save with file name
LinkedList
Example
7
.java

*/


import java.util.
LinkedList
;

import java.util.
List
;

import java.util.Arrays;

import java.io.File
Input
Stream;

import java.io.Object
Input
Stream;


public class
LinkedList
Example
7

{


public static void main(String args[])


{

http://www.hudatutorials.com

Download link:
http://www.hudatutorials.com/ht/java/
util/
linked
list
.html


6




try



{




//java.util.
Linke
dList

DECLARATION




List

s;





//File
Input
Stream CREATION




File
InputStream fi
s = new File
Input
Stream("
List
.set");





//Object
Input
Stream CREATION




Object
Input
Stream o
i
s = new Object
Input
Stream(fi
s);





//
READ

List

OBJECT
FROM

Object
Input
Stream




s

= (
List
)
oi
s.
read
Object(
);




oi
s.close();




System.out.println(s
);



}



catch(Exception e)



{




System.out.println("Error Occurred : " + e.getMessage());



}


}

}


The following example shows how to use
LinkedList

with Arrays,
Iterator and
List
Iterat
or.


/* java.util.
LinkedList

class Example
8

*/

/* Save with file name
LinkedList
Example
8
.java */


import java.util.
LinkedList
;

import java.util.Arrays;

import java.util.
List
Iterator;

import java.util.Iterator;


public class
LinkedList
Example
8

{


public st
atic void main(String args[])


{



String elements[] = {"Huda Tutorials","Java Tutorials","C Tutorials"};



Linked
List s; //java.util.
Linked
List DECLARATION




//java.util.List OBJECT CREATION USING ARRAY AS LIST



s = new
LinkedList
(Arrays.asList(elements
));




//java.util.List OUTPUT



List
Iterator itr = s.
listI
terator();



while(itr.hasNext())



{




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



}




System.out.println();



System.out.println("
-----

Reverse Order
-----
");



System.out.println();




//java.util.List
OUTPUT IN REVERSE ORDER



Iterator itr
2

= s.
descendingIterator()
;

http://www.hudatutorials.com

Download link:
http://www.hudatutorials.com/ht/java/
util/
linked
list
.html


7




while(itr
2
.has
Next
())



{




System.out.println(itr
2
.
next
());



}


}

}


/* java.util.LinkedList class Example
9
*/

/* Save with file name LinkedListExample
9
.java */


import java.util.Linked
List;


public class LinkedListExample
9

{


public static void main(String args[])


{



//java.util.LinkedList DECLARATION



LinkedList al;




//java.util.LinkedList OBJECT CREATION



//USING DEFAULT CONSTRUCTOR



al = new LinkedList();




//ADD AN ELEMENT



al.add
First
("Java Tutorials");




//
ADD AN ELEMENT AT LAST



al.add
Last
("C Tutorials");



al.add
Last
("CPP Tutorials");




//ADD AN ELEMENT AT FIRST



al.addFirst("Huda Tutorials
2
");




//ADD AN ELEMENT



al.
push
("Huda Tutorials");




//java.util.LinkedLis
t OUTPUT



System.out.println(al);




//GET THE FIRST ELEMENT



System.out.println("First Element : " +
al.
get
First()
)
;




//GET THE
LAST

ELEMENT



System.out.println("
Last

Element : " + al.get
Last
());




//GET THE
HEAD (FIRST)

ELEMENT



System.out.println
("
Head (First)

Element : " + al.
peek
());



}

}