Browse Source

Hash examples

george 6 months ago
parent
commit
88c9291ed5
2 changed files with 55 additions and 0 deletions
  1. 32 0
      unidad6/java/AddElementsToHashtable.java
  2. 23 0
      unidad6/java/HashSample.java

+ 32 - 0
unidad6/java/AddElementsToHashtable.java

@@ -0,0 +1,32 @@
+// Using Hashtable() Constructor
+import java.io.*;
+import java.util.*;
+
+class AddElementsToHashtable 
+{
+    public static void main(String args[])
+    {
+        // No need to mention the
+        // Generic type twice
+        Hashtable<String, String> ht1 = new Hashtable<>();
+
+        // Initialization of a Hashtable
+        // using Generics
+        Hashtable<Integer, String> ht2
+            = new Hashtable<Integer, String>();
+
+        // Inserting the Elements
+        // using put() method
+        ht1.put("192.168.1.20", "one");
+        ht1.put("192.16.1.1", "two");
+        ht1.put("10.1.2.1", "three");
+
+        ht2.put(4, "four");
+        ht2.put(5, "five");
+        ht2.put(6, "six");
+
+        // Print mappings to the console
+        System.out.println("Mappings of ht1 : " + ht1);
+        System.out.println("Mappings of ht2 : " + ht2);
+    }
+}

+ 23 - 0
unidad6/java/HashSample.java

@@ -0,0 +1,23 @@
+// Java program to demonstrates hashcode()
+// value for different objects
+class HashSample {
+    int id;
+    String name;
+
+    public HashSample(int id, String name)
+    {
+        this.id = id;
+        this.name = name;
+    }
+
+    public static void main(String[] args)
+    {
+        HashSample obj1 = new HashSample(1, "John");
+        HashSample obj2 = new HashSample(1, "John");
+
+        System.out.println("hashCode of obj1: "
+                           + obj1.hashCode());
+        System.out.println("hashCode of obj2: "
+                           + obj2.hashCode());
+    }
+}