Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Tim trong tu dien
- import java.util.*;
- public class HashTableExample {
- public static void main(String args[]) {
- Hashtable tdav = new Hashtable();
- tdav.put("Hello","Chao");
- tdav.put("Begin","Bat dau");
- tdav.put("End","Ket thuc");
- //Tim tu trong tu dien
- String tu = "Begin";
- if (tdav.containsKey(tu))
- System.out.println(tu+" : "+tdav.get(tu));
- else
- System.out.println("Khong co tu:"+tu);
- }
- }
- 2. Dem so tu trung nhau
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.StringTokenizer;
- public class countWords {
- public static void main(String[] args) {
- Hashtable h = new Hashtable();
- String s = "to be or not to be";
- StringTokenizer st = new StringTokenizer(s," ");
- //System.out.println("So tu:"+st.countTokens());
- while (st.hasMoreTokens()) {
- //System.out.println(st.nextToken());
- String ns = st.nextToken();
- if(h.containsKey(ns))
- {
- int i = (int)h.get(ns);
- h.put(ns, ++i);
- //System.out.println(ns + " " + i);
- }
- else{
- h.put(ns, 1);
- //System.out.println(ns + " =1");
- }
- }
- Enumeration e2 = h.elements();
- Enumeration e1 = h.keys();
- while (e1.hasMoreElements())
- System.out.println(e1.nextElement().toString() + " " + e2.nextElement().toString());
- }
- }
Add Comment
Please, Sign In to add comment