HashMap使用

/*
* 测试HashMap的应用,判断
*/
import java.util.HashMap; public class HuaWeiTest { private static final Integer ONE = new Integer(1); public static void main(String[] args) {
HashMap<Character, Integer> m=new HashMap<Character, Integer>();
char c[]={'张','张','王','王','王','赵','刘'};
for(int i=0;i<c.length;i++){
Integer freq=(Integer)m.get(c[i]); //get方法是获取这个position的value;c[i]是position;取值;
m.put(c[i], freq==null?ONE:new Integer(freq.intValue()+1)); }
System.out.println("不同姓氏有"+m.size()+"个");
System.out.println(m);
}
}

测试结果:

不同姓氏有4个
{张=2, 赵=1, 刘=1, 王=3}

华为机试题目:

package huawei7;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner; public class Password {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<String> list = new ArrayList<String>();
while(sc.hasNext()) {
list.add(sc.nextLine());
}
for(int i=0;i<list.size();i++) {
String input = list.get(i).trim();
if(isLegal(input)) {
System.out.println("OK");
}
else {
System.out.println("NG");
}
} sc.close();
} static boolean isLegal(String input) {
if(input.length()<=8) {
return false;
} HashMap<String, Integer> hash = new HashMap<String, Integer>(); for(int i=0;i<input.length();i++) {
char ch = input.charAt(i);
if(ch>='0' && ch<='9') {
hash.put("num", 1);
}
else if(ch>='a' && ch<='z') {
hash.put("upCase", 1);
}
else if(ch>='A' && ch<='Z') {
hash.put("lowCase", 1);
}
else if((ch>=0x21 && ch<=0x2F) || (ch>=0x3A && ch<=0x40)
||(ch>=0x5B && ch<=0x60) ||(ch>=0x7B && ch<=0x7E)) {
hash.put("symbol", 1);
}
}
Iterator<Integer> ite = hash.values().iterator();
int count =0;
while(ite.hasNext()) {
count+=ite.next();
}
if(count<3) {
return false;
} for(int i=0;i<input.length();i++) {
for(int j=3;j<= input.length();j++)
if(j+i<input.length()) {
String str =input.substring(i,i+j);
String left = input.substring(i+j);
if(left.contains(str)) {
return false;
}
}
}
return true;
}
}

华为漂亮度

package huawei4;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner; public class Beautiful {
private static final Integer ONE = new Integer(1); public static void main(String[] args){
System.out.println("请输入样例:");
Scanner scStr=new Scanner(System.in);
String in=scStr.nextLine();
String str[]=in.split(" "); //存起来,空格分割;
HashMap<String, Integer> hash=new HashMap<String, Integer>();
int count=str.length-1; //因为空格数为2的时候,时间字符串长度为3,而那个字符串也是两个
//ArrayList<String> list=new ArrayList<String>();
StringBuffer string1=new StringBuffer();
for(int i=0;i<count;i++){ //d对两个字符串进行遍历
string1.append(str[i+1]);
}
System.out.println(string1);
char ch[]=new char[20]; //开辟一个空间的字符数组
for(int j=0;j<string1.length();j++){
ch[j]=string1.charAt(j);
}
for(int k=0;k<ch.length;k++){
Integer freq=(Integer)hash.get(ch[k]);
hash.put(ch[k],freq==null?ONE:new Integer(freq.intValue()+1) ); } } }
上一篇:Android框架之AndroidAnnotations详细讲解


下一篇:TypeMismatchException: Provided id of the wrong type for class zhongfucheng.user.entity.User.