The LinkedList class is a collection based on a linked list instead of an array like the ArrayList. They are good when you plan to add and delete items. They aren’t particularly efficient at providing access based off of index searches though.

Nearly every Java Linked List method is shown below. Make sure you look for the code that follows the video, so you completely understand Linked Lists.

If you like videos like this, share it

Code from the Video

// The LinkedList class is a collection based on a Linked List
// instead of an array. They are particularly good when you
// expect to perform many additions and deletions with a
// collection. When using a linked list you don't have to
// move items around when you add or delete an item. They
// aren't particularly suited to providing access based off
// of index searches like an array though. Each object in a
// linked list contains a pointer to the objects that proceed
// and follow it.
// When you change an ArrayList a new array is created by it.
import java.util.Arrays;
import java.util.LinkedList; // LinkedList Library methods
public class LessonTwelve {
public static void main(String[] args){
// Creates a LinkedList object
LinkedList linkedListOne = new LinkedList();
// Creates a LinkedList that contains Strings
LinkedList<String> names = new LinkedList<String>();
// You use add to add items to the Linked List
names.add("Ahmed Bennani");
names.add("Ali Syed");
// addLast places the object last in the list
names.addLast("Nathan Martin");
// addLast places the object first in the list
names.addFirst("Joshua Smith");
// You can define what position to place the object in
names.add(0, "Noah Peeters");
// You replace a value in an index with set()
names.set(2, "Paul Newman");
// You remove items either by providing the index, or
// the value
names.remove(4);
names.remove("Joshua Smith");
// removeFirst() removes the first element
// removeLast() removes the last element
// removeFirstOccurrence(Object) removes the
// first Object that matches what you passed
// You can use the enhanced for to print them out
for(String name : names)
{
System.out.println(name);
}
/* OUTPUT
* Noah Peeters
* Paul Newman
* Ali Syed
*/
// You can retrieve values with get()
System.out.println("\nFirst Index: " + names.get(0));
/* OUTPUT
* First Index: Noah Peeters
*/
// Retrieve the first value with getFirst()
System.out.println("\nFirst Index: " + names.getFirst());
/* OUTPUT
* First Index: Noah Peeters
*/
// Retrieve the first value with getFirst()
System.out.println("\nLast Index: " + names.getLast());
/* OUTPUT
* Last Index: Ali Syed
*/
LinkedList<String> nameCopy = new LinkedList<String>(names);
// When you print out the LinkedList itself the toString
// method is called
System.out.println("\nnameCopy: " + nameCopy);
/* OUTPUT
* nameCopy: [Noah Peeters, Paul Newman, Ali Syed]
*/
// You can check if an object is in the list with contains()
if(names.contains("Noah Peeters"))
{
System.out.println("\nNoahs Here");
}
/* OUTPUT
* Noahs Here
*/
// You can check if everything in one list is in another
if(names.containsAll(nameCopy))
{
System.out.println("\nCollections are the same");
}
/* OUTPUT
* Collections are the same
*/
// Return the index for an object with indexOf
System.out.println("\nIndex of Ali is: " + names.indexOf("Ali Syed"));
/* OUTPUT
* Index of Ali is: 2
*/
// Check if a list is empty with isEmpty()
System.out.println("List Empty: " + names.isEmpty());
/* OUTPUT
* List Empty: false
*/
// Get the number of items in the list with size
System.out.println("How many values: " + names.size());
/* OUTPUT
* How many values: 3
*/
// peek() retrieves the first element, but doesn't throw an error
// if there is no element to retrieve
System.out.println("Look without error: " + names.peek());
/* OUTPUT
* Look without error: Noah Peeters
*/
// poll() returns the first value and deletes it from the list
System.out.println("Remove first element: " + nameCopy.poll());
/* OUTPUT
* Remove first element: Noah Peeters
*/
// poll() returns the last value and deletes it from the list
System.out.println("Remove last element: " + nameCopy.pollLast());
/* OUTPUT
* Remove last element: Ali Syed
*/
// push puts a new element on the front of the list
nameCopy.push("Noah Peeters");
// pop removes an element on the front of the list
nameCopy.pop();
System.out.println("\nnameCopy: " + nameCopy);
/* OUTPUT
* nameCopy: [Paul Newman]
*/
// Create a new array to hold values from the Linked List
Object[] nameArray = new Object[4];
// toArray converts the LinkedList into an array of objects
nameArray = names.toArray();
// toString converts items in the array into a String
System.out.println(Arrays.toString(nameArray));
/* OUTPUT
* [Noah Peeters, Paul Newman, Ali Syed]
*/
// clear() deletes everything in the Linked List
names.clear();
}
}

I personally learned everything from books, but I didn’t have access to the internet and video tutorials when I was first learning to program. I’d say it is best to try a bunch of different options to see what works best for you. If you start reading a book and it doesn’t make sense, or isn’t interesting then move to a different book. The most popular Java book is the head first java book. It is a little dated, but that won’t matter at all if you are just starting to program. I hope that helps. I can’t believe I’m popular in India! Thank you 🙂

Thanks Derek i just downloaded book “head first java”,i am starting to read it.Just tell me how much time will it take to become a professional programmer,i mean what programming languages should i learn in addition to java as i know little bit of C and C++.

That depends on how quickly you can make sense of OOP programming. Everybody is different. I think the most important thing is that you enjoy learning about programming. It also depends on what you mean by professional. The are many people that get hired as programmers that don’t know what they are doing. There are very few people that could write great software. It is more of an art form after you get past the introductory level

Is there a difference between Push/Pop and AddFirst/RemoveFirst? They appear to do the exact same thing. I assume this is just convention, so you should use push/pop if your linked list is acting as a stack, just so it’s not confusing to anyone reading your code.

1. I really enjoy everything I do.
2. If I ever start to get bored I switch to another activity.
3. I eat a very healthy diet of only fruit, vegetables and grains
4. I exercise for 20 minutes at a time or less 5 times per day normally between different jobs
5. I read constantly and do my best to avoid news, pop culture, etc.
6. I try to make every moment of the day special. I play with my kids for a bit numerous times per day. I enjoy a little music here and there. I take time to eat great tasting meals. Every day is just another adventure.