侧边栏壁纸
博主昵称

JAVA基础-HashMap学习01 🍂

JAVA基础-HashMap学习01
  • 文章发布于2024-03-14 22:18:08,共374字,阅读内容所需的预计时间2分钟。 本文总阅读量

自学JAVA-HashMap初级,练习代码

HashMap使用,统计单词出现的次数,基础版,写的不好

 1package collenction;
 2
 3import java.util.HashMap;
 4import java.util.Iterator;
 5import java.util.Map;
 6import java.util.Set;
 7
 8/**
 9 *  this is a cat and that is a mice and where is the foot?
10 * 统计每个单词出现的次数
11 *
12 *存储到Map中
13 * key:String
14 * value:自定义类型
15 *  
16 *  @author Administrator
17 */
18public class HashMapTest01 {
19
20	public static void main(String[] args) {
21		//定义字符串
22		String str = "this is a cat and that is a mice and where is the foot?";
23		//分割字符串存储到Map中,根据空格分割
24		String[] strArray = str.split(" ");
25		
26		Map<String,Letter> letter = new HashMap<String,Letter>(); 
27		
28		/*
29		 * 实现方式一
30		 */
31	
32		for(String temp:strArray) {
33			//	为所有的key创建容器
34			if(!letter.containsKey(temp)) {
35				letter.put(temp,new Letter());				
36			}
37			//之后容器中存放对应value
38			Letter col = letter.get(temp);
39			col.setCount(col.getCount()+1);
40		}
41		
42		//输出Map的值
43		Set<String> keys = letter.keySet();
44		Iterator i = keys.iterator();
45		while (i.hasNext()) {
46			Letter col = letter.get(i.next()	);
47			System.out.println("字母"+i.next()	+"出现了"+col.getCount()+"次");
48		}
49		 
50		for (String key : keys) {
51			Letter col = letter.get(key);
52			System.out.println("字母"+ key	+"出现了"+col.getCount()+"次");
53
54		}
55		System.out.println("************");
56		/*
57		 * 实现方式二
58		 */
59		Letter col = null;
60		for(String temp:strArray) {
61			//	判断容器是否存在
62			if((col = letter.get(temp))==null) {
63				//不存在就创建一个
64				letter.put(temp,new Letter());				
65			}
66			//之后容器中存放对应value
67			col.setCount(col.getCount()+1);
68		}
69		
70		
71		//输出Map的值
72				Set<String> keyss = letter.keySet();
73				for (String key : keyss) {
74					col = letter.get(key);
75					System.out.println("字母"+ key	+"出现了"+col.getCount()+"次");
76				}
77		
78    /*
79    * 当前Letter的name没用上,也可以存储key
80    */
81	}
82}

创建实体类Letter用于存储出现次数

 1package collenction;
 2
 3public class Letter {
 4
 5	private String name;
 6	private int count;
 7	public String getName() {
 8		return name;
 9	}
10	public void setName(String name) {
11		this.name = name;
12	}
13	public int getCount() {
14		return count;
15	}
16	public void setCount(int count) {
17		this.count = count;
18	}
19	public Letter(String name, int count) {
20		super();
21		this.name = name;
22		this.count = count;
23	}
24	public Letter() {
25	
26	}
27	
28}

这里展示输出结果,后面数字出现double是因为变量没变,所以重复统计了

字母foot?出现了1次
字母that出现了2次
字母cat出现了2次
字母is出现了1次
字母mice出现了1次
字母the出现了1次
字母foot?出现了1次
字母a出现了2次
字母that出现了1次
字母and出现了2次
字母cat出现了1次
字母this出现了1次
字母is出现了3次
字母where出现了1次
字母mice出现了1次
************
字母the出现了2次
字母foot?出现了2次
字母a出现了4次
字母that出现了2次
字母and出现了4次
字母cat出现了2次
字母this出现了2次
字母is出现了6次
字母where出现了2次
字母mice出现了2次
博主栏壁纸
博主头像 爱喝酸奶的我

懒人博客,随缘记录生活点滴

15 文章数
12 评论量
最新评论
标签云
    一言
    今天早上我告诉你我想你了,你没理我。今天中午我给你打电话,你不接,打第二个你就关机。晚上我在你公司楼下等你,你对我说的第一句话就是滚“滚,别烦我,别浪费时间了”,我真的好感动,你居然为我考虑了,怕我浪费时间。呜呜呜,这是我爱你的第74天。