Java Hashtable example.

/*

Java Hashtable example.

This Java Hashtable example describes the basic operation performed on the hashtable.

*/

import java.util.Hashtable;

import java.util.Enumeration;

public class HashtableExample{

public static void main(String args[]){

// constructs a new empty hashtable with default initial capacity

Hashtable hashtable = new Hashtable();

/*

To specify initial capacity, use following constructor.

燞ashtable hashtable = new Hashtable(100);

燭o create hashtable from map use following constructor

燞ashtable hashtable = new Hashtable(Map myMap);

?/

hashtable.put( "One", new Integer(1) ); // adding value into hashtable

hashtable.put( "Two", new Integer(2) );

hashtable.put( "Three", new Integer(3) );

/*

IMPORTANT : We CAN NOT add primitives to the hashtable. We have to wrap it into one of the wrapper before adding.

*/

/*

To copy all key - value pairs from Map to Hashtable use putAll method.

Signature of putAll method is,

void putAll(Map m)

*/

//get number of keys present in the hashtable

System.out.println("Hashtable contains " + hashtable.size() + " key value pair.");

/*

To check whether Hashtable is empty or not, use isEmpty() method.

isEmpty() returns true is Hashtable is empty, otherwise false.

*/

/*

Finding particular value from the Hashtable :

Hashtable's contains method returns boolean depending upon the presense of the value in given hashtable

Signature of the contains method is,

boolean contains(Object value)

*/

if( hashtable.contains( new Integer(1) ) ){

System.out.println("Hashtable contains 1 as value");

}else{

System.out.println("Hashtable does not contain 1 as value");

}

/*

Finding particular Key from the Hashtable :

Hashtable's containsKey method returns boolean depending upon the presense of the key in given hashtable

Signature of the method is,

boolean containsKey(Object key)

*/

if( hashtable.containsKey("One") ){

System.out.println("Hashtable contains One as key");

}else{

System.out.println("Hashtable does not contain One as value");

}

/*

Use get method of Hashtable to get value mapped to particular key.

Signature of the get method is,

Object get(Object key)

*/

Integer one = (Integer) hashtable.get("One");

System.out.println("Value mapped with key \"One\" is " + one);

/*

IMPORTANT:?get method returns Object, so we need to downcast it.

*/

/*

To get all keys stored in Hashtable use keys method.

Signature of the keys method is,

Enumeration keys()

To get Set of the keys use keySet() method instead.

Set keySet()

*/

System.out.println("Retriving all keys from the Hashtable");

Enumeration e = hashtable.keys();

while( e. hasMoreElements() ){

System.out.println( e.nextElement() );

}

/*

To get all values stored in Hashtable牋牋 use elements method.

Signature of the elements method is,

Enumeration elements()

To get Set of the values use entrySet() method instead.

Set entrySet()

*/

System.out.println("Retriving all values from the Hashtable");

e = hashtable.elements();

while( e. hasMoreElements() ){

System.out.println( e.nextElement() );

}

/*

To remove particular key - value pair from the Hashtable use remove method.

Signature of remove methid is,

Object remove(Object key)

This method returns value that had mapped to the given key, otherwise null if mapping not found.

*/

System.out.println( hashtable.remove("One") + " is removed from the Hashtable.");

}

}

/*

OUTPUT of the above given Java Hashtable Example would be :

Hashtable contains 3 key value pair.

Hashtable contains 1 as value

Hashtable contains One as key

Value mapped with key "One" is 1

Retriving all keys from the Hashtable

One

Three

Two

Retriving all values from the Hashtable

1

3

2

1 is removed from the Hashtable.

*/

转载于:https://www.cnblogs.com/licheng/archive/2008/12/06/1349363.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package 作业5; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import java.io.File; import java.util.HashMap; import java.util.Scanner; import java.io.File; import java.util.HashMap; import java.util.Scanner; public class Example13_7{ public static void main( String args []){ WindowWord win = new WindowWord(); win.setTitle("英﹣汉小字典"); } } class WindowWord extends JFrame{ JTextField inputText,showText; WordPolice police; public WindowWord(){ setLayout(new FlowLayout()); inputText = new JTextField(6); showText = new JTextField(6); showText.setColumns(15); add(inputText); add(showText); police = new WordPolice(); police.setView(this); inputText.addActionListener(police); setBounds(100,100,400,280); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class WordPolice implements ActionListener { WindowWord view; HashMap<String,String>hashtable; File file = new File ("E:\\JAVA\\上机\\作业5\\cet4.txt"); Scanner sc = null ; WordPolice () { hashtable = new HashMap< String,String >(); //System.out.println(hashtable.size()); try { sc = new Scanner ( file ); //System.out.println(sc); while (sc.hasNext ()) { String englishWord = sc . next (); String chineseWord = sc . next (); hashtable . put ( englishWord , chineseWord ); } HashMap<String,String> reversedHashtable = new HashMap<String,String>(); for (String key : hashtable.keySet()) { String value = hashtable.get(key); reversedHashtable.put(value, key); } hashtable.putAll(reversedHashtable); } catch(Exception e ) {} } public void setView(WindowWord view) { this.view = view; } public void actionPerformed(ActionEvent e) { String inputWord = view.inputText.getText (); if(hashtable.containsKey(inputWord)) { String outputWord = hashtable.get ( inputWord ); view.showText.setText (outputWord ); } else { String chineseWord = inputWord; for (String key : hashtable.keySet()) { String value = hashtable.get(key); if (value.equals(chineseWord)) { view.showText.setText(key); return; } } view.showText.setText ("没有此单词"); } } }把这段代码完善一下,让它能实现汉语翻译为英语,英语也能翻译为汉语,GUI界面不变
05-24

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值