Java Video Tutorial 12

Java Video Tutorial 12In this part of the Java Video Tutorial I cover Java Linked Lists.

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("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)
		 * Noah Peeters
		 * Paul Newman
		 * Ali Syed
		// You can retrieve values with get()
		System.out.println("\nFirst Index: " + names.get(0));
		 * First Index: Noah Peeters
		// Retrieve the first value with getFirst()
		System.out.println("\nFirst Index: " + names.getFirst());
		 * First Index: Noah Peeters
		// Retrieve the first value with getFirst()
		System.out.println("\nLast Index: " + names.getLast());
		 * 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);
		 * 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");
		 * Noahs Here
		// You can check if everything in one list is in another
			System.out.println("\nCollections are the same");
		 * Collections are the same
		// Return the index for an object with indexOf
		System.out.println("\nIndex of Ali is: " + names.indexOf("Ali Syed"));
		 * Index of Ali is: 2
		// Check if a list is empty with isEmpty()
		System.out.println("List Empty: " + names.isEmpty());
		 * List Empty: false
		// Get the number of items in the list with size
		System.out.println("How many values: " + names.size());
		 * 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());
		 * Look without error: Noah Peeters
		// poll() returns the first value and deletes it from the list
		System.out.println("Remove first element: " + nameCopy.poll());
		 * Remove first element: Noah Peeters
		// poll() returns the last value and deletes it from the list
		System.out.println("Remove last element: " + nameCopy.pollLast());
		 * 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
		System.out.println("\nnameCopy: " + nameCopy);
		 * 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
		 * [Noah Peeters, Paul Newman, Ali Syed]
		// clear() deletes everything in the Linked List

27 Responses to “Java Video Tutorial 12”

  1. Ravi says:

    Thanks a lot!!
    You are making me more job ready than my college!

    Hats off!

    • admin says:

      That was the idea behind creating this website. I always wanted to contribute to providing a free education to the world. Thank you for taking the time to show your appreciation 🙂

  2. patel says:

    Nothing to say your way of doing tutorials is great . innovative. Thanks for all of your free knowledge. Really Appreciated

    • admin says:

      Thank you 🙂 It is very risky to make video tutorials without a book to guide me nor any outside influence. I’m glad you enjoy them. I love making them

  3. Aamir says:

    Once again thanks for the video,i just wanna say that i am from India, is there is any book u recommend to study for java or should i only watch videos on internet to learn java in Depth.

    • Derek Banas says:

      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 🙂

      • Anonymous says:

        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++.

        • Derek Banas says:

          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

  4. Tom P says:

    Hi Jeff, really appreciate your tutorials. Thank you for your time.

    My question is when would linked lists be used? What kind of software would need them?

  5. Karan says:

    Hey Derek, In this tutorial don’t the methods poll() and pop() mean the same thing?

  6. XD_ says:

    And yes you are popular in INDIA.
    Thank you for such great video series.

    PS : It would be great if we can see data structures using java videos ( covering linked list,stack,queue,tree,graph implementations ).

  7. Nikolay says:

    Hi Derek, I really appreciate what you do! Thank you a lot!
    Greetings from Bulgaria (a small country in South-Eastern Europe).

  8. saquib says:

    Derek, Derek, Derek, you are now my favorite!!

  9. Kellan says:

    Can you provide the code as a .txt or .java file? I like to save offline notes and I need to manually remove the line numbers when I copy/paste. Thanks!

    Amazing tutorials by the way – I have a computer science degree and these videos are a great (and fast!) refresher.

    • Derek Banas says:

      If you put your cursor at the top of the code 3 icons appear at the top right. one is called view source. If you click that it will eliminate the line numbers. Sorry about that.

  10. Kellan says:

    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.

  11. Suganya says:

    Hi Derek,

    U are really wonderful. Im astonished by the way u teach. Even I wanted learn the concepts very clearly like u. Give me some tips…

    • Derek Banas says:

      Thank you 🙂 I think I’m able to do what I do for a couple of reasons.

      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.

      I hope that helps 🙂

  12. carlos says:

    you videos are excellent, they help me a lot.

Leave a Reply

Your email address will not be published.