Java LinkedList: add() and size() methods
KW.java
import java.util.LinkedList ;
class KW
{
public static void main ( String [] args )
{
//LinkedList class implements the List interface and Deque -> Queue interface
LinkedList < String > ll = new LinkedList <>();
ll . add ( "Apple" );
ll . add ( "Orange" );
ll . add ( "Mango" );
ll . add ( "Grapes" );
ll . add ( "Cherry" );
ll . add ( "Apple" );
System . out . println ( ll );
System . out . println ( "Size: " + ll . size ());
}
}
Output
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
[Apple, Orange, Mango, Grapes, Cherry, Apple]
Size: 6
kodingwindow@kw:~$
Java LinkedList: get() and contains() methods
KW.java
import java.util.LinkedList ;
class KW
{
public static void main ( String [] args )
{
//LinkedList class implements the List interface and Deque -> Queue interface
LinkedList < String > ll = new LinkedList <>();
ll . add ( "Apple" );
ll . add ( "Orange" );
ll . add ( "Mango" );
ll . add ( "Grapes" );
ll . add ( "Cherry" );
ll . add ( "Apple" );
for ( String s: ll )
{
System . out . println ( s );
}
System . out . println ( "\n" + ll . get ( 3 ));
System . out . println ( ll . contains ( "Apple" ));
}
}
Output
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
Apple
Orange
Mango
Grapes
Cherry
Apple
Grapes
true
kodingwindow@kw:~$
Java LinkedList: addFirst() and addLast() methods
KW.java
import java.util.LinkedList ;
class KW
{
public static void main ( String [] args )
{
//LinkedList class implements the List interface and Deque -> Queue interface
LinkedList < String > ll = new LinkedList <>();
ll . add ( "Apple" );
ll . add ( "Orange" );
ll . add ( "Mango" );
ll . add ( "Grapes" );
ll . add ( "Cherry" );
for ( String s: ll )
{
System . out . println ( s );
}
ll . addFirst ( "Strawberry" );
ll . addLast ( "Apple" );
System . out . println ( "\n" + ll );
}
}
Output
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
Apple
Orange
Mango
Grapes
Cherry
[Strawberry, Apple, Orange, Mango, Grapes, Cherry, Apple]
kodingwindow@kw:~$
Java LinkedList: getFirst() and getLast() methods
KW.java
import java.util.LinkedList ;
class KW
{
public static void main ( String [] args )
{
//LinkedList class implements the List interface and Deque -> Queue interface
LinkedList < String > ll = new LinkedList <>();
ll . add ( "Apple" );
ll . add ( "Orange" );
ll . add ( "Grapes" );
ll . add ( "Cherry" );
ll . add ( "Apple" );
ll . add ( "Mango" );
System . out . println ( ll );
System . out . println ( "First element: " + ll . getFirst ());
System . out . println ( "Last element: " + ll . getLast ());
}
}
Output
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
[Apple, Orange, Grapes, Cherry, Apple, Mango]
First element: Apple
Last element: Mango
kodingwindow@kw:~$
Java LinkedList: indexOf() and lastIndexOf() methods
KW.java
import java.util.LinkedList ;
class KW
{
public static void main ( String [] args )
{
//LinkedList class implements the List interface and Deque -> Queue interface
LinkedList < String > ll = new LinkedList <>();
ll . add ( "Apple" );
ll . add ( "Orange" );
ll . add ( "Mango" );
ll . add ( "Grapes" );
ll . add ( "Cherry" );
ll . add ( "Apple" );
System . out . println ( "indexOf Apple: " + ll . indexOf ( "Apple" ));
System . out . println ( "lastIndexOf Apple: " + ll . lastIndexOf ( "Apple" ));
}
}
Output
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
indexOf Apple: 0
lastIndexOf Apple: 5
kodingwindow@kw:~$
Java LinkedList: addAll() and containsAll() methods
KW.java
import java.util.LinkedList ;
class KW
{
public static void main ( String [] args )
{
//LinkedList class implements the List interface and Deque -> Queue interface
LinkedList < String > ll1 = new LinkedList <>();
ll1 . add ( "Apple" );
ll1 . add ( "Orange" );
ll1 . add ( "Mango" );
System . out . println ( ll1 );
LinkedList < String > ll2 = new LinkedList <>();
ll2 . add ( "Grapes" );
ll2 . add ( "Cherry" );
ll2 . add ( "Apple" );
System . out . println ( ll2 );
System . out . println ( ll1 . containsAll ( ll2 ));
ll1 . addAll ( ll2 );
System . out . println ( ll1 );
}
}
Output
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
[Apple, Orange, Mango]
[Grapes, Cherry, Apple]
false
[Apple, Orange, Mango, Grapes, Cherry, Apple]
kodingwindow@kw:~$
Java LinkedList: remove() and removeAll() methods
KW.java
import java.util.LinkedList ;
class KW
{
public static void main ( String [] args )
{
//LinkedList class implements the List interface and Deque -> Queue interface
LinkedList < String > ll = new LinkedList <>();
ll . add ( "Apple" );
ll . add ( "Orange" );
ll . add ( "Mango" );
ll . add ( "Grapes" );
ll . add ( "Cherry" );
ll . add ( "Apple" );
System . out . println ( ll );
ll . remove ();
System . out . println ( ll );
ll . remove ( 2 );
System . out . println ( ll );
ll . remove ( "Cherry" );
System . out . println ( ll );
ll . removeAll ( ll ); //ll.clear();
if ( ll . isEmpty ())
{
System . out . println ( "LinkedList is empty now" );
}
}
}
Output
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
[Apple, Orange, Mango, Grapes, Cherry, Apple]
[Orange, Mango, Grapes, Cherry, Apple]
[Orange, Mango, Cherry, Apple]
[Orange, Mango, Apple]
LinkedList is empty now
kodingwindow@kw:~$