Docsity

This is an assignment for hashmap, Essays (university) of Java Programming

Toggle sidebar
bg1
When writing a data structure what should be our guidelines for choosing the right Java Collection?
The overall approach I'd suggest for choosing is as follows
1. choose the general type of organization that the data needs to have (e.g. map or list); without too
much thought, this is usually fairly clear;
2. then, choose the implementation of that type that has the minimum functionality that you actually
require
package listarraylist;
import java.util.ArrayList;
import java.util.ListIterator;
public class Main {
public static void main(String[] args) {
// Create ArrayList object with capacity of 2 elements
ArrayList al = new ArrayList(2);
System.out.println(al+", size = "+al.size());
// Add items to the ArrayList
al.add("R");
al.add("U");
al.add("O");
al.add(new String("x"));
al.add(2, new Integer(10));
System.out.println(al+", size = " + al.size());
// Remove item
al.remove("U");
System.out.println(al+", size = " + al.size());
// Check if the list contains the specified element
Boolean b = al.contains("x");
System.out.println("The list contains x = " + b);
b = al.contains("p");
System.out.println("The list contains p = " + b);
b = al.contains(new Integer(10));
System.out.println("The list contains Integer of 10 = " + b);
// Create ListIterator and iterate entries in it
ListIterator li = al.listIterator();
while (li.hasNext())
System.out.println("From ListIterator = " + li.next());
// Create Object array from ArrayList
Object a[] = al.toArray();
pf3
pf4
pf5
pf8

Partial preview of the text

Download This is an assignment for hashmap and more Essays (university) Java Programming in PDF only on Docsity!

When writing a data structure what should be our guidelines for choosing the right Java Collection?

The overall approach I'd suggest for choosing is as follows

1. choose the general type of organization that the data needs to have (e.g. map or list); without too

much thought, this is usually fairly clear;

2. then, choose the implementation of that type that has the minimum functionality that you actually

require

package listarraylist;

import java.util.ArrayList;

import java.util.ListIterator;

public class Main {

public static void main(String[] args) {

// Create ArrayList object with capacity of 2 elements

ArrayList al = new ArrayList(2);

System.out.println(al+", size = "+al.size());

// Add items to the ArrayList

al.add("R");

al.add("U");

al.add("O");

al.add(new String("x"));

al.add(2, new Integer(10));

System.out.println(al+", size = " + al.size());

// Remove item

al.remove("U");

System.out.println(al+", size = " + al.size());

// Check if the list contains the specified element

Boolean b = al.contains("x");

System.out.println("The list contains x = " + b);

b = al.contains("p");

System.out.println("The list contains p = " + b);

b = al.contains(new Integer(10));

System.out.println("The list contains Integer of 10 = " + b);

// Create ListIterator and iterate entries in it

ListIterator li = al.listIterator();

while (li.hasNext())

System.out.println("From ListIterator = " + li.next());

// Create Object array from ArrayList

Object a[] = al.toArray();

for (int i=0; i<a.length; i++)

System.out.println("From an Array = " + a[i]);

package settreeset;

import java.util.Set;

import java.util.TreeSet;

public class Main {

public static void main(String[] args) {

Set ts = new TreeSet();

ts.add("one");

ts.add("two");

ts.add("three");

ts.add("four");

ts.add("three");

System.out.println("Members from TreeSet = " + ts);

Set ts2 = new TreeSet();

ts2.add(1);

ts2.add(2);

ts2.add(3);

ts2.add(4);

ts2.add(2);

System.out.println("Members from TreeSet = " + ts2);

A hash table is just an array of linked lists. Each linked list holds all the items in the table that share the

same has code. Initially, all the lists are empty (represented as null in the array). I need to be able to add

and delete items in the list. In a hash table, the order of items in a particular list doesn't matter, so I simply

insert each new item at the beginning of a list. This makes the insert operation fairly simple. Deletion,

however, is more complicated, since I need to be able to delete an item no matter where it occurs in a list.

// Print out the location number and the list of // key/value pairs in this location. System.out.print(i + ":"); ListNode list = table[i]; // For traversing linked list number i. while (list != null) { System.out.print(" (" + list.key + "," + list.value + ")"); list = list.next; } System.out.println(); } } // end dump()

public void put(Object key, Object value) { // Associate the specified value with the specified key. // Precondition: The key is not null. int bucket = hash(key); // Which location should this key be in? ListNode list = table[bucket]; // For traversing the linked list // at the appropriate location. while (list != null) { // Search the nodes in the list, to see if the key already exists. if (list.key.equals(key)) break; list = list.next; } // At this point, either list is null, or list.key.equals(key). if (list != null) { // Since list is not null, we have found the key. // Just change the associated value. list.value = value; } else { // Since list == null, the key is not already in the list. // Add a new node at the head of the list to contain the // new key and its associated value. if (count >= 0.75*table.length) { // The table is becoming too full. Increase its size // before adding the new node. resize(); } ListNode newNode = new ListNode(); newNode.key = key; newNode.value = value; newNode.next = table[bucket]; table[bucket] = newNode; count++; // Count the newly added key. } }

public Object get(Object key) { // Retrieve the value associated with the specified key // in the table, if there is any. If not, the value // null will be returned. int bucket = hash(key); // At what location should the key be? ListNode list = table[bucket]; // For traversing the list. while (list != null) {

// Check if the specified key is in the node that // list points to. If so, return the associated value. if (list.key.equals(key)) return list.value; list = list.next; // Move on to next node in the list. } // If we get to this point, then we have looked at every // node in the list without finding the key. Return // the value null to indicate that the key is not in the table. return null; }

public void remove(Object key) { // Remove the key and its associated value from the table, // if the key occurs in the table. If it does not occur, // then nothing is done. int bucket = hash(key); // At what location should the key be? if (table[bucket] == null) { // There are no keys in that location, so key does not // occur in the table. There is nothing to do, so return. return; } if (table[bucket].key.equals(key)) { // If the key is the first node on the list, then // table[bucket] must be changed to eliminate the // first node from the list. table[bucket] = table[bucket].next; count--; // Remove new number of items in the table. return; } // We have to remove a node from somewhere in the middle // of the list, or at the end. Use a pointer to traverse // the list, looking for a node that contains the // specified key, and remove it if it is found. ListNode prev = table[bucket]; // The node that precedes // curr in the list. Prev.next // is always equal to curr. ListNode curr = prev.next; // For traversing the list, // starting from the second node. while (curr != null &&! curr.key.equals(key)) { curr = curr.next; prev = curr; } // If we get to this point, then either curr is null, // or curr.key is equal to key. In the later case, // we have to remove curr from the list. Do this // by making prev.next point to the node after curr, // instead of to curr. If curr is null, it means that // the key was not found in the table, so there is nothing // to do. if (curr != null) { prev.next = curr.next; count--; // Record new number of items in the table. } }

list = next; // Move on to the next node in the OLD table. } } table = newtable; // Replace the table with the new table. } // end resize()

} // end class HashTable

A Program for Testing HashTable:

A little program to test the HashTable class. Note that I start with a really small table so that I can easily test the resize() method. */

public class TestHashTable {

public static void main(String[] args){ HashTable table = new HashTable(2); String key,value; while (true) { System.out.println("\nMenu:"); System.out.println(" 1. test put(key,value)"); System.out.println(" 2. test get(key)"); System.out.println(" 3. test containsKey(key)"); System.out.println(" 4. test remove(key)"); System.out.println(" 5. show complete contents of hash table."); System.out.println(" 6. EXIT"); System.out.print("Enter your command: "); switch ( TextIO.getlnInt()) { case 1: System.out.print("\n Key = "); key = TextIO.getln(); System.out.print(" Value = "); value = TextIO.getln(); table.put(key,value); break; case 2: System.out.print("\n Key = "); key = TextIO.getln(); System.out.println(" Value is " + table.get(key)); break; case 3: System.out.print("\n Key = "); key = TextIO.getln(); System.out.println(" containsKey(" + key + ") is "

  • table.containsKey(key)); break; case 4: System.out.print("\n Key = "); key = TextIO.getln(); table.remove(key); break;

case 5: table.dump(); break; case 6: return; // End program by returning from main() default: System.out.println(" Illegal command."); break; } System.out.println("\nHash table size is " + table.size()); } }

} // end class TestHashTable