Java Dictionary Example
Dictionary class is the abstract class which is parent of any class which uses the key and value pair relationship. The classes like HashTable extends this class for their functionality. Every key and every value is an object. In any one Dictionary object, every key is associated with at most one value. As a rule, the equals method should be used by implementations of this class to decide if two keys are the same. Also note that this class has become obsolete, the new implementation has to use the Map interface. Lets look at an example.
In the below example, I have created a “java.txt” file which the example code and reading the text file and printing it.
package javabeat.net.core; import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map; public class JavaDictionaryExample {
public static void main(String args[]) throws IOException,FileNotFoundException{
BufferedReader bufReader = new BufferedReader(new FileReader(new File(
"java.txt")));
String inputLine = null;
Map dictionaryMap = new HashMap();
while ((inputLine = bufReader.readLine()) != null) { // Here split the input line
String[] words = inputLine.split("\\s+");
if (inputLine.equals(""))
continue;
for (String wordStr : words) {
wordStr = wordStr.replace(".", "");
wordStr = wordStr.replace(",", "");
if (dictionaryMap.containsKey(wordStr)) {
Integer val = (Integer)dictionaryMap.get(wordStr);
dictionaryMap.put(wordStr, val + 1);
} else
dictionaryMap.put(wordStr, 1);
}
}
for (Object key : dictionaryMap.keySet())
System.out.println(key + ": " + dictionaryMap.get(key));
}
}
Output
1);: 2
: 28
Printing: 1
for: 2
dictionaryget(key));: 1
package: 1
javautilHashMap;: 1
readerclose();: 1
"inputtxt")));: 1
commas: 1
":: 1
main(String: 1
any: 1
empty: 1
import: 5
dots: 1
Hashtable();: 1
key: 1
": 1
else: 1
static: 1
wordreplace("": 2
Map: 2
+: 3
class: 1
inputLine: 1
javabeatnetcore;: 1
dictionarykeySet()): 1
and: 1
input: 1
reader: 1
javaioFileReader;: 1
FileReader(new: 1
args[]): 1
//: 5
String[]: 1
Systemoutprintln(key: 1
String: 1
:: 2
word: 3
lines: 1
Ignore: 1
javaioFile;: 1
=: 9
val: 2
javautilMap;: 1
javaioBufferedReader;: 1
inputLinesplit("\\s+");: 1
line: 1
dictionaryput(word: 2
File(: 1
HashMap();: 1
null): 1
words): 1
while: 1
JavaDictionaryExample: 1
words: 2
if: 2
map: 1
dictionaryget(word);: 1
Remove: 1
null;: 1
BufferedReader(new: 1
all: 1
readerreadLine()): 1
"");: 2
(dictionarycontainsKey(word)): 1
void: 1
continue;: 1
dictionary: 2
the: 2
stored: 1
in: 1
(String: 2
((inputLine: 1
new: 3
BufferedReader: 1
Split: 1
!=: 1
}: 5
(inputLineequals("")): 1
Integer: 1
public: 2
{: 5
Comments
Java Dictionary Example的更多相关文章
- Java Dictionary 类存储键值
字典(Dictionary) 字典(Dictionary) 类是一个抽象类,它定义了键映射到值的数据结构. 当你想要通过特定的键而不是整数索引来访问数据的时候,这时候应该使用Dictionary. 当 ...
- Java Dictionary 类
Dictionary 类是一个抽象类,用来存储键/值对,作用和Map类相似. 给出键和值,你就可以将值存储在Dictionary对象中.一旦该值被存储,就可以通过它的键来获取它.所以和Map一样, D ...
- (私人收藏)[开发必备]最全Java离线快速查找手册(可查询可学习,带实例)
(私人收藏)[开发必备]最全Java离线快速查找手册(可查询可学习,带实例) https://pan.baidu.com/s/1L54VuFwCdKVnQGVc8vD1TQnwmj java手册 Ja ...
- Java和Scala容器转换
参考:https://blog.csdn.net/dymkkj/article/details/77921573 Java和Scala互操作的一个重要的内容就是容器的转换,容器是一个语言的数据结构,表 ...
- Bean Validation规范
以下内容转载自:https://www.ibm.com/developerworks/cn/java/j-lo-beanvalid/ Bean Validation规范介绍 JSR303 规范(Bea ...
- scala成长之路(3)隐式转换
不废话,先上例子:定义一个参数类型为String的函数: scala> def sayHello(name:String) = println("hello " + name ...
- 20180824-Java Enumeration 接口
Java Enumeration接口 Enumeration接口中定义了一些方法,通过这些方法可以枚举(一次获得一个)对象集合中的元素. 这种传统接口已被迭代器取代,虽然Enumeration 还未被 ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- Java自定义一个字典类(Dictionary)
标准Java库只包含Dictionary的一个变种,名为:Hashtable.(散列表) Java的散列表具有与AssocArray相同的接口(因为两者都是从Dictionary继承来的).但有一个方 ...
随机推荐
- NET对象的跨应用程序域
NET对象的跨应用程序域 转眼就到了元宵节,匆匆忙忙的脚步是我们在为生活奋斗的写照,新的一年,我们应该努力让自己有不一样的生活和追求.生命不息,奋斗不止.在上篇博文中主要介绍了.NET的AppDoma ...
- C#配置IIS搭建网站的工具类
public class IISWorker { public static string HostName = "localhost"; /// <summary> ...
- mvc:view-controller 标签
一.SpringMVC 会把ModelAndView 的model中的数据放到request 域中 二.如果不想让请求经过任何handler,而直接响应页面,可以使用 mvc:view-control ...
- PowerDNS简单教程(1):安装篇
这一篇开始直接是PowerDNS教程,连续四篇.DNS的相关背景知识我就不介绍了,有需要的话看看 http://baike.baidu.com/link?url=QcthFpAb2QydMqcMJr9 ...
- Ubuntu14.04.3,apt-get出现dpkg: error processing package xxx (--configure)和cups-daemon错误的解决方案
Ubuntu14.04.3,使用apt-get安装软件的时候,报个莫名其妙的错误: dpkg: error processing package xxx (--configure): balabala ...
- 检测Android和IOS
var u=navigator.userAgent; var isAndroid=u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; / ...
- 小BAT解决大麻烦_某卡教室控制软件
@echo off mode con cols= lines= if "%1" == "h" goto begin mshta vbscript:)(windo ...
- 使用Spring实现AOP(XML+注解)
一.Spring对AOP的支持 AOP并不是Spring框架特有的,Spring只是支持AOP编程的框架之一,每一个框架对AOP的支持各有特点,有些AOP能够对方法的参数进行拦截,有些AOP对方法进行 ...
- HDU 1019 (多个数的最小公倍数)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1019 Least Common Multiple Time Limit: 2000/1000 MS (J ...
- performPeriodicTask
/********************************************************************* * @fn performPeriodicTask 执行 ...