1
import java.util.Enumeration;
import java.util.Hashtable;

//@author fsociety 

public class HashFoo {

public static void main(String[] args) {
    HashFoo <String,String> student = new HashFoo <String,String>();

    student.put("1", "A");//Left side: Key , Right side: Value
    student.put("2", "B");//Left side: Key , Right side: Value
    student.put("3", "C");//Left side: Key , Right side: Value
    student.put("4", "D");//Left side: Key , Right side: Value
    student.put("5", "E");//Left side: Key , Right side: Value
    student.put("6", "F");//Left side: Key , Right side: Value
    student.put("7", "G");//Left side: Key , Right side: Value
    student.put("8", "H");//Left side: Key , Right side: Value

        System.out.println("Search this person 1 information: " + student.get("1"));//Get someone
        System.out.println("Is the person I wrote type of KEY: " + student.containsKey("2"));
        System.out.println("Is the person I wrote type of VALUE: " + student.containsValue("Z") + "\n");

    Enumeration enumerationValue = student.elements();
    Enumeration enumerationKeys = student.keys();
        System.out.println("Hash Table Values: "+"\n");

    while(enumerationValue.hasMoreElements() && enumerationKeys.hasMoreElements()){
        System.out.println(enumerationKeys.nextElement()+ " --> " + enumerationValue.nextElement() + "\n");
    }

        System.out.println("Is student hashtable empty: " + student.isEmpty());
        System.out.println("Hashtable size: " + student.size());

} 

}

I am new so I will learn hash with time. Now, I want to learn how can I do static search,insert,delete method in main. Also how can I store key-value in the array? Thank you in advance. Output Search this person 1 information: A Is the person I wrote type of KEY: true Is the person I wrote type of VALUE: false

Hash Table Values:

6 --> F

5 --> E

4 --> D

3 --> C

2 --> B

1 --> A

8 --> H

7 --> G

Is student hashtable empty: false Hashtable size: 8

I want search in this shape;

Output

1-Search: ..someone.. 2-Insert: ..someone.. 3-Delete: ..someone..

fsociety
  • 13
  • 4
  • It's not clear. You have an Map, and you already use insert (put), search (contains), retrieve (get). You can also delete (remove). What do you want to do ? – guillaume girod-vitouchkina Dec 04 '15 at 23:37
  • Yes it is not clear. I began new so I dont know how can I write molds. I dont know how to write method about search, delete,insert so I wanted all of them but It is enough only search method. @guillaume girod-vitouchkina – fsociety Dec 05 '15 at 00:55
  • .contains and .get == search – NickL Dec 05 '15 at 02:13

1 Answers1

0

With HashMap, you only do this:

At the beginin of your code:

Map< String,String> student = new HashMap < String,String>();

and you get : put, get, contains, remove

You have several options:

to learn more, see this: difference between linkedhashmap, hashmap, map, hashtable

and this: Differences between HashMap and Hashtable?

If you want to keep your order, use LinkedHashMap instead

Community
  • 1
  • 1