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..

3
  • 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 ? Commented Dec 4, 2015 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 Commented Dec 5, 2015 at 0:55
  • .contains and .get == search Commented Dec 5, 2015 at 2:13

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.