Linked Lists in Java Part 2

April 26th, 2008
By: Cory Mathews

In Part One we learned the basics of using the Linked list class in java. I showed how to create, add, edit, and remove items from the list.

Now I am going to show you how to use the following functions:

.toString(), .equals(), .hashCode(), .isEmpty(), .subList(), .contains(),

For all my examples I am using the list MyList with the contents [1, 2, 3].

.toString()

This function is an excellent debugging statement, or just a simple very quick way to see everything in your list. Its simple to use:

myList.toString();
//or to print out the list
System.out.println(myList.toString());

This would print out:

[1, 2, 3]

.equals()

This method does exactly what you would think it would. It compares two lists to see if they are equal. For two lists to be equal they must have all the same items, and they must be in the same order. So in other words one must be a deep copy of the other. Here is a little example using .equals() and .toString().

myList.add("1");
myList.add("2");
myList.add("3");
myList2.add("1");
myList2.add("2");
myList2.add("3");
System.out.println(myList.toString());
System.out.println(myList2.toString());
System.out.println(myList.equals(myList2));

myList2.remove(1);
myList2.add("2");
System.out.println(myList.toString());
System.out.println(myList2.toString());
System.out.println(myList.equals(myList2));

This would print out

[1, 2, 3]
[1, 2, 3]
true
[1, 2, 3]
[1, 3, 2]
false

.hashCode()

.hashCode() uses an algorithm to generate a number based on the list. This can be used for encryption or anything that you need a simple reliable number to represent a list. Sun provides the algorithm for generating the number and here it is.

  hashCode = 1;
  Iterator i = list.iterator();
  while (i.hasNext()) {
      Object obj = i.next();
      hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
  }

You don’t actually need to know its algorithm but its always nice to see what exactly it is doing. so Now here is a short example using the hasCode(). I am using the resulting values from the .equals() section so as a reminder myList contains [1,2,3] and myList2 contains [1,3,2] and they are all strings.

System.out.println(myList.hashCode());
System.out.println(myList.hashCode());

This would output

78481
78511

As you can see these two lists produce different hash codes. So even though the lists contain the same strings they still produce different hash codes.

.isEmpty()

This is a boolean method that does exactly what it says. It return true or false depending if the list contains anything.

System.out.println(myList.isEmpty());

would output

false

.subList()

subList allows you to return a shallow copy of part of the list. So changing the values of the subList will change the values of the original list. So for example MyList contains [1,2,3] the example code:

System.out.println(myList.subList(0,3));
System.out.println(myList.subList(1,2));
System.out.println(myList.subList(2,3));
System.out.println(myList.subList(2,6));

displays

[1, 2, 3]
[2]
[3]
Exception in thread "main" java.lang.IndexOutOfBoundsException: toIndex = 6
at java.util.SubList.(AbstractList.java:705)
at java.util.AbstractList.subList(AbstractList.java:570)
at LinkedListTest.main(LinkedListTest.java:37)

Since my list is only 3 items long you can see that trying to go to 6 gives and IndexOutOfBoundsException and crashes. But looking at the 3 previous calls, I can get the entire list as a sublist, kind of pointless but it can be done. I can also take just specific elements as you see in the middle 2 examples.

We can also combine subList and clear to imitate the method removeRange which for whatever reason I could never get to work. The code:
myList.subList(0, 2).clear();
System.out.println(myList.size());
System.out.println(myList.toString());

would change our list and output

1
[3]

.contains()

This is a very useful method. It return a true or false depending if the item you are looking for is in the list or not. So if i was to run the code

System.out.println(myList.contains("1"));
System.out.println(myList.contains("0"));

It would print out

true
false

Thats all for now, as always here is an example source with all the explained methods being used.

import java.util.*;
public class LinkedListTest
{
  public static void main(String[] args)
  {
	  LinkedList myList = new LinkedList();
	  LinkedList myList2 = new LinkedList();

	  myList.add("1");
	  myList.add("2");
	  myList.add("3");
	  myList2.add("1");
	  myList2.add("2");
	  myList2.add("3");
	  System.out.println(myList.toString());
	  System.out.println(myList2.toString());
	  System.out.println(myList.equals(myList2));

	  myList2.remove(1);
	  myList2.add("2");
	  System.out.println(myList.toString());
	  System.out.println(myList2.toString());
	  System.out.println(myList.equals(myList2));

	  System.out.println(myList.hashCode());
	  System.out.println(myList2.hashCode());

	  System.out.println(myList.isEmpty());
	  System.out.println(myList.subList(0,3));
	  System.out.println(myList.subList(1,2));
	  System.out.println(myList.subList(2,3));
	  myList.subList(0, 2).clear();
	  System.out.println(myList.size());
	  System.out.println(myList.toString());
	  myList.add("2");
	  myList.add("1");

	  System.out.println(myList.contains("1"));
	  System.out.println(myList.contains("0"));
  }
}