HashMaps

Nicholas Duchon: May 9, 2019.

Outline



The Wiki has a nice article:

Notes - all classes type Tnd

Code - Tnd

// File: HashMapND.java
// Date: May 9, 2019
// Author: Nicholas Duchon
// Purpose: create and read data files
//  into ArrayList's using local HashMaps

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class HashMapND {
   // number of children for each level of 5 levels
   int [] max = {4, 2, 3, 4, 5};
  
   // root is target data structure
   Tnd root = new Tnd (0, 0, "root", 0);
  
   public static void main (String [] args) {
      new HashMapND ();
  
} // end main
  
   public HashMapND () {
      // create the data structures
      createStructure ();

      // return the structures in level order
      // in format of data file: flag, int (index), int (parent index)
      String data = toLevelOrder ();

      // print the format appropriate for data file
      System.out.println ("Data:\n" + data);
     
      // clear the create data data structures:
      root = new Tnd (0, 0, "New Root", 0);
     
      // read the data file using local hash maps to connect item to its parent list:
      readString ("Read Data:\n" + data);
     
      // print the resulting internal data structure
      System.out.println ("This: " + this);
  
} // end no parameter constructor
  
   void readString (String d) {
      HashMap <Integer, Tnd> hmt = new HashMap <> ();
      hmt.put (root.index, root);
     
      Scanner sd = new Scanner (d);
      String flag = "";
      int a, b;
     
      while (sd.hasNextLine()) {
         String line = sd.nextLine();
         Scanner sc = new Scanner (line);
         flag = sc.next();
         if (!sc.hasNextInt ()) continue;
         a = sc.nextInt();
         b = sc.nextInt();
         switch (flag) {
            case "And": addTnd (new Tnd(a, b, "And",  2), hmt);
               break;
            case "Bnd": addTnd (new Tnd(a, b, "Bnd",  4), hmt);
               break;
            case "Cnd": addTnd (new Tnd(a, b, "Cnd",  6), hmt);
               break;
            case "Dnd": addTnd (new Tnd(a, b, "Dnd",  8), hmt);
               break;
            case "End": addTnd (new Tnd(a, b, "End", 10), hmt);
               break;
         } // end switch
      } // end for each line
  
} // end method readString
  
   void addTnd (Tnd t, HashMap <Integer, Tnd> hmt) {
      hmt.get (t.parent).links.add(t);
      hmt.put (t.index, t);
  
} // end method addAnd
  
   void createStructure () {
      for (int i = 1; i <= max[0]; i++) { // A's
         Tnd a = new Tnd (i, 0, "And",  2);
         root.links.add (a);
         for (int j = 1; j <= max[1]; j++) { // B's
            Tnd b = new Tnd (j + a.index*10, a.index, "Bnd",  4);
            a.links.add (b);
            for (int k = 1; k <= max[2]; k++) { // C's
               Tnd c = new Tnd (k + b.index*10, b.index, "Cnd",  6);
               b.links.add (c);
               for (int m = 1; m <= max[3]; m++) { // D's
                  Tnd d = new Tnd (m + c.index*10, c.index, "Dnd",  8);
                  c.links.add (d);
                  for (int n = 1; n <= max[4]; n++) { // E's
                     Tnd e = new Tnd (n + d.index*10, d.index, "End",  10);
                     d.links.add (e);
                  } // end for E's
               } // end for D's
            } // end for C's
         } // end for B's
      } // end for A's
  
} // end method create Structure
  
   public String toString () {
      String st = root.name;
      for (Tnd a: root.links) st += "\n" + a.toString();
      return st;
  
} // end method toString
  
   // See the following for the algorithm:
   // http://sandsduchon.org/duchon/Musings/a/traversals.html
   public String toLevelOrder () {
      String st = "";
      ArrayList <Tnd> list = new ArrayList <> ();
      for (Tnd t: root.links) list.add (t);
      while (!list.isEmpty()) {
         Tnd t = list.remove(0);
         st += t.name + " " + t.index + " " + t.parent + "\n";
         for (Tnd x: t.links) list.add (x);
      } // end for elements in list
      return st;
   } // end method toString
} // end class HashMapND

class Tnd {
   int index, parent, depth=2;
   String name = "";
   ArrayList <Tnd> links = new ArrayList <> ();
  
   public Tnd (int a, int b, String c, int d) {
      index  = a;
      parent = b;
      name   = c;
      depth  = d;
  
} // end constructor

   public String toString () {
      String st = String.format (
                 "%" + depth + "s %s %d %d",
                  "", name, index, parent);
      for (Tnd t: links)
         st += "\n" + t;
      return st;
  
} // end toString method
  
} // end class Tnd


Notes - including child classes of Tnd

Code - Tnd + Xnd

// File: HashMapND.java
// Date: Sep 14, 2018
// Author: Nicholas Duchon
// Purpose: create and read data files
//  into ArrayList's using local HashMaps

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class HashMapND {
   // number of children for each level of 5 levels
   int [] max = {4, 2, 3, 0, 0};
  
   // aA is target data structure
   ArrayList <And> aA = new ArrayList <> ();
  
   public static void main (String [] args) {
      new HashMapND ();
   } // end main
  
   public HashMapND () {
      // create the data structures
      createStructure ();

      // return the structures in level order
      // in format of data file: flag, int (index), int (parent index)
      // format: <String> <int> <int>

      String data = toLevelOrder ();

      // print the format appropriate for data file

      System.out.println ("Data:\n" + data);
     
      // clear the create data data structures:
      aA = new ArrayList <> ();
     
      // read the data file using local hash maps to connect item to its parent list:
      readString ("Read Data:\n" + data);
     
      // print the resulting internal data structure
      System.out.println ("This:" + this);
   } // end no parameter constructor
  
   void readString (String d) {
      HashMap <Integer, And> hma = new HashMap <> ();
      HashMap <Integer, Bnd> hmb = new HashMap <> ();
      HashMap <Integer, Cnd> hmc = new HashMap <> ();
      HashMap <Integer, Dnd> hmd = new HashMap <> ();
     
      Scanner sd = new Scanner (d);
      String flag = "";
     
      while (sd.hasNextLine()) {
         String line = sd.nextLine();
         Scanner sc = new Scanner (line);
         flag = sc.next();
         switch (flag) {
            case "And": addAnd (sc, hma);      break;
            case "Bnd": addBnd (sc, hma, hmb); break;
            case "Cnd": addCnd (sc, hmb, hmc); break;
            case "Dnd": addDnd (sc, hmc, hmd); break;
            case "End": addEnd (sc, hmd);      break;
         } // end switch
      } // end for each line
   } // end method readString
  
   void addAnd (Scanner sc, HashMap <Integer, And> hma) {
        // Instantiate the class
      And a = new And (sc.nextInt(), sc.nextInt());

        // Add instance to main data structure
      aA.add (a);

        // Add this instance to the hash map
      hma.put (a.index, a);

   } // end method addAnd
  
   void addBnd (Scanner sc, HashMap <Integer, And> hma, HashMap <Integer, Bnd> hmb) {
        // Instantiate the class
      Bnd b = new Bnd (sc.nextInt(), sc.nextInt());

        // Link parent list of children (links) to this new instance
      hma.get(b.parent).links.add(b);

        // Add instance to main data structure
      hmb.put (b.index, b);

   } // end method addAnd
  
   void addCnd (Scanner sc, HashMap <Integer, Bnd> hmb, HashMap <Integer, Cnd> hmc) {
      Cnd c = new Cnd (sc.nextInt(), sc.nextInt());
      hmb.get(c.parent).links.add(c);
      hmc.put (c.index, c);
   } // end method addAnd
  
   void addDnd (Scanner sc, HashMap <Integer, Cnd> hmc, HashMap <Integer, Dnd> hmd) {
      Dnd d = new Dnd (sc.nextInt(), sc.nextInt());
      hmc.get(d.parent).links.add(d);
      hmd.put (d.index, d);
   } // end method addAnd
  
   void addEnd (Scanner sc, HashMap <Integer, Dnd> hmd) {
      End e = new End (sc.nextInt(), sc.nextInt());
      hmd.get(e.parent).links.add(e);
   } // end method addAnd
  
   void createStructure () {
      for (int i = 1; i <= max[0]; i++) { // A's
         And a = new And (i, 0);
         aA.add (a);
         for (int j = 1; j <= max[1]; j++) { // B's
            Bnd b = new Bnd (j + a.index*10, a.index);
            a.links.add (b);
            for (int k = 1; k <= max[2]; k++) { // C's
               Cnd c = new Cnd (k + b.index*10, b.index);
               b.links.add (c);
               for (int m = 1; m <= max[3]; m++) { // D's
                  Dnd d = new Dnd (m + c.index*10, c.index);
                  c.links.add (d);
                  for (int n = 1; n <= max[4]; n++) { // E's
                     End e = new End (n + d.index*10, d.index);
                     d.links.add (e);
                  } // end for E's
               } // end for D's
            } // end for C's
         } // end for B's
      } // end for A's
   } // end method create Structure
  
   public String toString () {
      String st = "";
      for (And a: aA) st += "\n" + a.toString();
      return st;
   } // end method toString
  
   // See the following for the algorithm:
   // http://sandsduchon.org/duchon/Musings/a/traversals.html
   public String toLevelOrder () {
      String st = "";
      ArrayList <Tnd> list = new ArrayList <> ();
      for (Tnd t: aA) list.add (t);
      while (!list.isEmpty()) {
         Tnd t = list.remove(0);
         st += t.name + " " + t.index + " " + t.parent + "\n";
         for (Tnd x: t.links) list.add (x);
      } // end for elements in list
      return st;
   } // end method toString
} // end class HashMapND

class Tnd {
   int index, parent, depth=2;
   String name = "";
   ArrayList <Tnd> links = new ArrayList <> ();
  
   public Tnd (int a, int b, String c, int d) {
      index = a;
      parent = b;
      name = c;
      depth = d;
   } // end constructor

   public String toString () {
      String st = String.format (
                 "%" + depth + "s %s %d %d",
                  "", name, index, parent);
      for (Tnd t: links)
         st += "\n" + t;
      return st;
   } // end toString method
  
} // end class Tnd

class And extends Tnd {
   public And (int v, int p) {
      super (v, p, "And", 2);
   }
} // end class And

class Bnd extends Tnd {
   public Bnd (int v, int p) {
      super (v, p, "Bnd", 4);
   }
} // end class Bnd

class Cnd extends Tnd {
   public Cnd (int v, int p) {
      super (v, p, "Cnd", 6);
   }
} // end class Cnd

class Dnd  extends Tnd {
   public Dnd (int v, int p) {
      super (v, p, "Dnd", 8);
   }
} // end class Dnd

class End  extends Tnd {
   public End (int v, int p) {
      super (v, p, "End", 10);
   }
} // end class End

Output


Data Resulting data structure
Data:
And 1 0
And 2 0
And 3 0
And 4 0
Bnd 11 1
Bnd 12 1
Bnd 21 2
Bnd 22 2
Bnd 31 3
Bnd 32 3
Bnd 41 4
Bnd 42 4
Cnd 111 11
Cnd 112 11
Cnd 113 11
Cnd 121 12
Cnd 122 12
Cnd 123 12
Cnd 211 21
Cnd 212 21
Cnd 213 21
Cnd 221 22
Cnd 222 22
Cnd 223 22
Cnd 311 31
Cnd 312 31
Cnd 313 31
Cnd 321 32
Cnd 322 32
Cnd 323 32
Cnd 411 41
Cnd 412 41
Cnd 413 41
Cnd 421 42
Cnd 422 42
Cnd 423 42

This:
   And 1 0
     Bnd 11 1
       Cnd 111 11
       Cnd 112 11
       Cnd 113 11
     Bnd 12 1
       Cnd 121 12
       Cnd 122 12
       Cnd 123 12
   And 2 0
     Bnd 21 2
       Cnd 211 21
       Cnd 212 21
       Cnd 213 21
     Bnd 22 2
       Cnd 221 22
       Cnd 222 22
       Cnd 223 22
   And 3 0
     Bnd 31 3
       Cnd 311 31
       Cnd 312 31
       Cnd 313 31
     Bnd 32 3
       Cnd 321 32
       Cnd 322 32
       Cnd 323 32
   And 4 0
     Bnd 41 4
       Cnd 411 41
       Cnd 412 41
       Cnd 413 41
     Bnd 42 4
       Cnd 421 42
       Cnd 422 42
       Cnd 423 42



Using Hashmap Constructors

// File: HashMapNDv04.java
// Date: Sep 14, 2018
// Author: Nicholas Duchon
// Purpose: create and read data files
//  into ArrayList's using local HashMaps
// History
//    May 22, 2019 - modified to use hashmaps in constructors
//                 - and instance initializers

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class HashMapNDv04 {
   // number of children for each level of 5 levels
   int [] max = {4, 2, 3, 5, 4};
 
   // aA is target data structure
   ArrayList <And> aA = new ArrayList <> ();
 
   public static void main (String [] args) {
      new HashMapNDv04 ();
   } // end main
 
   public HashMapNDv04 () {
      // create the data structures
      createStructure ();
  
      // return the structures in level order
      // in format of data file: flag, int (index), int (parent index)
      // format: <String> <int> <int>
      String data = toLevelOrder ();
  
      // print the format appropriate for data file
      System.out.println ("Data:\n" + data);
    
      // clear the create data data structures:
      aA = new ArrayList <> ();
    
      // read the data file using local hash maps to connect item to its parent list:
      readString ("Read Data:\n" + data);
    
      // print the resulting internal data structure
      System.out.println ("This:" + this);
   } // end no parameter constructor
 
   void readString (String d) {
      HashMap <Integer, Tnd> hmt = new HashMap <> ();
    
      Scanner sd = new Scanner (d);
      String flag = "";
    
      while (sd.hasNextLine()) {
         String line = sd.nextLine();
         Scanner sc = new Scanner (line);
         flag = sc.next();
         switch (flag) {
            case "And": aA.add (new And (sc, hmt)); break;
            case "Bnd": new Bnd (sc, hmt); break;
            case "Cnd": new Cnd (sc, hmt); break;
            case "Dnd": new Dnd (sc, hmt); break;
            case "End": new End (sc, hmt); break;
         } // end switch
      } // end for each line
   } // end method readString
 
   void createStructure () {
      for (int i = 1; i <= max[0]; i++) { // A's
         And a = new And (i, 0);
         aA.add (a);
         for (int j = 1; j <= max[1]; j++) { // B's
            Bnd b = new Bnd (j + a.index*10, a.index);
            a.links.add (b);
            for (int k = 1; k <= max[2]; k++) { // C's
               Cnd c = new Cnd (k + b.index*10, b.index);
               b.links.add (c);
               for (int m = 1; m <= max[3]; m++) { // D's
                  Dnd d = new Dnd (m + c.index*10, c.index);
                  c.links.add (d);
                  for (int n = 1; n <= max[4]; n++) { // E's
                     End e = new End (n + d.index*10, d.index);
                     d.links.add (e);
                  } // end for E's
               } // end for D's
            } // end for C's
         } // end for B's
      } // end for A's
   } // end method create Structure
 
   public String toString () {
      String st = "";
      for (And a: aA) st += "\n" + a.toString();
      return st;
   } // end method toString
 
   // See the following for the algorithm:
   // http://sandsduchon.org/duchon/Musings/a/traversals.html
   public String toLevelOrder () {
      String st = "";
      // NOTE: ArrayDeque might be a better choice here

      ArrayList <Tnd> list = new ArrayList <> ();
      for (Tnd t: aA) list.add (t);
      while (!list.isEmpty()) {
         Tnd t = list.remove(0);
         st += t.name + " " + t.index + " " + t.parent + "\n";
         for (Tnd x: t.links) list.add (x);
      } // end for elements in list
      return st;
   } // end method toString
} // end class HashMapNDv04

class Tnd {
   int index, parent, depth=2;
   String name = "Tnd";
   ArrayList <Tnd> links = new ArrayList <> ();
  
   public Tnd (Scanner sc, HashMap <Integer, Tnd> hmt) {
      index = sc.nextInt ();
      parent = sc.nextInt ();
      hmt.put (index, this);
      Tnd p = hmt.get (parent);
      if (p != null) p.links.add (this);
   } // end constructor
  
   public Tnd (int n, int m) {
      index = n;
      parent = m;
   } // end int int constructor
 
   public String toString () {
      String st = String.format (
                 "%" + depth + "s %s %d %d",
                  "", name, index, parent);
      for (Tnd t: links)
         st += "\n" + t;
      return st;
   } // end toString method
 
} // end class Tnd

class And extends Tnd {
   {name = "And"; depth = 2;}
 
   public And (Scanner sc, HashMap <Integer, Tnd> hmt) {
      super (sc, hmt);
   } // end constructor
  
   public And (int n, int m) {
      super (n, m);
   } // end int int constructor
} // end class And

class Bnd extends Tnd {
   {name = "Bnd"; depth = 4;}
 
   public Bnd (Scanner sc, HashMap <Integer, Tnd> hmt) {
      super (sc, hmt);
   } // end constructor
  
   public Bnd (int n, int m) {
      super (n, m);
   } // end int int constructor
} // end class Bnd

class Cnd extends Tnd {
   {name = "Cnd"; depth = 6;}
 
   public Cnd (Scanner sc, HashMap <Integer, Tnd> hmt) {
      super (sc, hmt);
   } // end constructor
  
   public Cnd (int n, int m) {
      super (n, m);
   } // end int int constructor
} // end class Cnd

class Dnd  extends Tnd {
   {name = "Dnd"; depth = 8;}
 
   public Dnd (Scanner sc, HashMap <Integer, Tnd> hmt) {
      super (sc, hmt);
   } // end constructor
  
   public Dnd (int n, int m) {
      super (n, m);
   } // end int int constructor
} // end class Dnd

class End  extends Tnd {
   {name = "End"; depth = 10;}
 
   public End (Scanner sc, HashMap <Integer, Tnd> hmt) {
      super (sc, hmt);
   } // end constructor
  
   public End (int n, int m) {
      super (n, m);
   } // end int int constructor
} // end class End




ND.