HashMap
HashMap继承了AbstractMap, 实现了接口Map
Map map<String, Integer> = new HashMap<String,Integer>();
map.size();//map的长度
//判断一个key值是否存在map中,
map.put("a",1);
boolean isExist = map.containsKey("a");
//isExist = true;
map.put("a", "apple");
map.put("b", "banana");
map.put("c","peach");
System.out.println(map.containsKey("a"));
System.out.println(map.containsValue("apple"));
//都会是true
//往里面加入key与value
map.put("e",2);//如果已经有了,会覆盖掉旧值,并返回旧值
map.put("e",3);
题目:彩色的砖块
想清楚了是水题,如果字符超过两种,就不可能相邻之间只有一种不同
import java.util.HashMap;
import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args){
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String s = "";
try{
s =bf.readLine();}
catch(IOException ex){
ex.printStackTrace();
}
Map<Character, Integer> map = new HashMap<Character, Integer>();
for(int i = 0; i < s.length(); i++){
if(map.containsKey(s.charAt(i))){
int tmp = map.get(s.charAt(i));
map.put(s.charAt(i), tmp+1);//记录有多少个相同的值,其实此题没必要记录
}else{
map.put(s.charAt(i),1);
}
}
if(map.size() > 2){
System.out.println(0);
return ;}
else
System.out.println(map.size());
}
}