Show usage of Lists interfaces in a Jupyter Notebook example. Try using List interface and making a ArrayList object. Consider using an example that will be part of interest or final projects. This will probably become your favorite Data Structure in Java.

List Interface

  • way to store ordered collection of obj
  • inherits the Collection interface
  • Implementation Classes

    • ArrayList
    • LinkedList
    • Stack
    • Vector
  • List is an interface so can't create objects with type list but instead use a class that implements List to create an object

//declaring List interface
//public interface List<E> extends Collection<E>;
//ArrayList (object of List interface)
//creates an integer arraylist 
List<Integer> l1 = new ArrayList<Integer>();

ArrayLists

  • Elements can be added or removed, resizable
import java.util.ArrayList;

ArrayList<String> dnMarketItems = new ArrayList<String>();

add(int index, element) This method is used to add an element at a particular index in the list. When a single parameter is passed, it simply adds the element at the end of the list.

dnMarketItems.add("AP Physics C Baron's Prep Book");
dnMarketItems.add("TI Inspire Calculator");
dnMarketItems.add("PE Lock");
true

addAll(int index, Collection collection) This method is used to add all the elements in the given collection to the list. When a single parameter is passed, it adds all the elements of the given collection at the end of the list.

ArrayList<String> dnMeenaListings = new ArrayList<String>(); 

dnMeenaListings.add("AP Calc BC Baron's Prep Book");
dnMeenaListings.add("Calc Tutoring 1 hr sessions");


dnMarketItems.addAll(dnMeenaListings);
System.out.println("Del Norte Market Listings: " + dnMarketItems + "\n");
System.out.println("Meena's Listings: " + dnMeenaListings);
Del Norte Market Listings: [AP Physics C Baron's Prep Book, TI Inspire Calculator, PE Lock, AP Calc BC Baron's Prep Book, Calc Tutoring 1 hr sessions]

Meena's Listings: [AP Calc BC Baron's Prep Book, Calc Tutoring 1 hr sessions]

size() This method is used to return the size of the list.

System.out.println("The number of listings in DN Marketplace is currently " + dnMarketItems.size());
The number of listings in DN Marketplace is currently 5

clear() This method is used to remove all the elements in the list. However, the reference of the list created is still stored.

dnMeenaListings.clear();
System.out.println("Meena's Listings: " + dnMeenaListings);
Meena's Listings: []

remove(int index) This method removes an element from the specified index. It shifts subsequent elements(if any) to left and decreases their indexes by 1.

// deletes the first item on the list (AP Physics C Prep Book)
dnMarketItems.remove(0);
System.out.println("Del Norte Market Listings: " + dnMarketItems);
Del Norte Market Listings: [TI Inspire Calculator, PE Lock, AP Calc BC Baron's Prep Book, Calc Tutoring 1 hr sessions]

remove(element) This method is used to remove the first occurrence of the given element in the list.

dnMarketItems.add("AP Physics C Baron's Prep Book");

// removes PE Lock
dnMarketItems.remove("PE Lock");
System.out.println("Del Norte Market Listings: " + dnMarketItems);
Del Norte Market Listings: [TI Inspire Calculator, AP Calc BC Baron's Prep Book, Calc Tutoring 1 hr sessions, AP Physics C Baron's Prep Book]

get(int index) This method returns elements at the specified index.

System.out.println("The first item listed is " + dnMarketItems.get(0));
The first item listed is TI Inspire Calculator

set(int index, element) This method replaces elements at a given index with the new element. This function returns the element which was just replaced by a new element.

// replace the first term (index of 0)
dnMarketItems.set(0, "Used TI Inspire Calculator");
System.out.println("Del Norte Market Listings: " + dnMarketItems);
Del Norte Market Listings: [Used TI Inspire Calculator, AP Calc BC Baron's Prep Book, Calc Tutoring 1 hr sessions, AP Physics C Baron's Prep Book]

indexOf(element) This method returns the first occurrence of the given element or -1 if the element is not present in the list.

System.out.println("The index for calc tutoring sessions is " + dnMarketItems.indexOf("Calc Tutoring 1 hr sessions"));
The index for calc tutoring sessions is 2

lastIndexOf(element) This method returns the last occurrence of the given element or -1 if the element is not present in the list.

// prints negative 1 since the item is not in the list
System.out.println("The index for physics tutoring sessions is " + dnMarketItems.indexOf("Physics Tutoring 1 hr sessions"));
The index for physics tutoring sessions is -1

equals(element) This method is used to compare the equality of the given element with the elements of the list.

ArrayList<String> dnMarketInventory = new ArrayList<String>(); 
dnMarketInventory.add("Used TI Inspire Calculator");
dnMarketInventory.add("AP Calc BC Baron's Prep Book");
dnMarketInventory.add("Calc Tutoring 1 hr sessions");

if (dnMarketItems.equals(dnMarketInventory) == true) {
    System.out.println("The inventory matches the listings!");
}
else {
    System.out.println("The inventory does not match the listings!");
}
The inventory does not match the listings!

hashCode() This method is used to return the hashcode value of the given list.

System.out.println("The hashcode for this list is " + dnMarketItems.hashCode());
The hashcode for this list is -720605698

isEmpty() This method is used to check if the list is empty or not. It returns true if the list is empty, else false.

System.out.println("Meena's listings is empty is " + dnMeenaListings.isEmpty());
Meena's listings is empty is true

contains(element) This method is used to check if the list contains the given element or not. It returns true if the list contains the element.

System.out.println("DN Market Items contains PE Lock is " + dnMarketItems.contains("PE Lock"));
DN Market Items contains PE Lock is false

containsAll(Collection collection) This method is used to check if the list contains all the collection of elements.

if (dnMarketItems.containsAll(dnMarketInventory) == true) {
    System.out.println("The inventory has at least everything in the listings!");
}
else {
    System.out.println("The inventory does not have everything the listings!");
}
The inventory has at least everything in the listings!

sort(Comparator comp) This method is used to sort the elements of the list on the basis of the given comparator.

Collections.sort(dnMarketItems);
System.out.println("Sorted DN Market items " + dnMarketItems);
Sorted DN Market items [AP Calc BC Baron's Prep Book, AP Physics C Baron's Prep Book, Calc Tutoring 1 hr sessions, Used TI Inspire Calculator]
public class Song
{

  /** Verse - prints out a verse of the song
   * @param number - a String like "one", "two", etc.
   * @param rhyme - a String like "thumb", "shoe", etc.
   */
   public void verse(String number, String rhyme)
   {
     System.out.println("This old man, he played " + number);
     System.out.println("He played knick knack on my " + rhyme);
   }

  // The chorus method
  public void chorus()
  {
     System.out.println("With a knick knack paddy whack, give a dog a bone.");
     System.out.println("This old man came rolling home.");
  }

  public static void main(String args[])
  {
      Song mySong = new Song();
      mySong.verse("one", "thumb");
      mySong.chorus();
      mySong.verse("two", "shoe");
      mySong.chorus();
      mySong.verse("three", "knee");
      mySong.chorus();
      mySong.verse("four", "door");
      mySong.chorus();
  }
}

Song.main(null);
This old man, he played one
He played knick knack on my thumb
With a knick knack paddy whack, give a dog a bone.
This old man came rolling home.
This old man, he played two
He played knick knack on my shoe
With a knick knack paddy whack, give a dog a bone.
This old man came rolling home.
This old man, he played three
He played knick knack on my knee
With a knick knack paddy whack, give a dog a bone.
This old man came rolling home.
This old man, he played four
He played knick knack on my door
With a knick knack paddy whack, give a dog a bone.
This old man came rolling home.