java hashMap 查询效率非常高,看一下根据key找value,根据value找key
2015-10-16 16:53
671 查看
@Test
public void testMap(){
Map<Character, String> map=new HashMap<Character, String>();
//step1 加密字符键 字母
char c='a';
for(int i=2;i<=9;i++){
int k=(i==7|| i==9)?4:3;
for(int j=1;j<=k;j++){
String value=""+i+j;
map.put(c, value);
c++;
}
}
//step2 加密字符 空格 逗号 句号
map.put(' ', "11");
map.put(',', "12");
map.put('.', "13");
//step3 从0开始循环放入数字
for(c='0';c<='9';c++){
String value=c+"0";
map.put(c, value);
}
System.out.println(map);
String msg="no zuo no die.";
StringBuilder sb=new StringBuilder(msg.length()*2);
for(int i=0;i<msg.length();i++){
String value=map.get(msg.charAt(i));
if(value!=null){
sb.append(value);
}else{
System.out.println("包含不能识别的字符");
break;
}
}
if(sb.length()==msg.length()*2){
System.out.println("原文"+msg);
System.out.println("密文"+sb.toString());
}
//解密:
String cypt=sb.toString();
StringBuilder sb2=new StringBuilder();
Set<Entry<Character, String>> entrySet=map.entrySet();
for(int i=0;i<cypt.length();i+=2){
String sub=cypt.substring(i,i+2);
System.out.println(sub);
for(Entry<Character, String> entry:entrySet){
//当前子字符串和entry中的value比较
if(sub.equals(entry.getValue())){
sb2.append(entry.getKey());
break;//一旦找到匹配的key就不在寻找。
}
}
}
if(sb2.length()==cypt.length()/2){
System.out.println("解密成功"+sb2.toString());
}else{
System.out.println("解密失败");
}
}
public void testMap(){
Map<Character, String> map=new HashMap<Character, String>();
//step1 加密字符键 字母
char c='a';
for(int i=2;i<=9;i++){
int k=(i==7|| i==9)?4:3;
for(int j=1;j<=k;j++){
String value=""+i+j;
map.put(c, value);
c++;
}
}
//step2 加密字符 空格 逗号 句号
map.put(' ', "11");
map.put(',', "12");
map.put('.', "13");
//step3 从0开始循环放入数字
for(c='0';c<='9';c++){
String value=c+"0";
map.put(c, value);
}
System.out.println(map);
String msg="no zuo no die.";
StringBuilder sb=new StringBuilder(msg.length()*2);
for(int i=0;i<msg.length();i++){
String value=map.get(msg.charAt(i));
if(value!=null){
sb.append(value);
}else{
System.out.println("包含不能识别的字符");
break;
}
}
if(sb.length()==msg.length()*2){
System.out.println("原文"+msg);
System.out.println("密文"+sb.toString());
}
//解密:
String cypt=sb.toString();
StringBuilder sb2=new StringBuilder();
Set<Entry<Character, String>> entrySet=map.entrySet();
for(int i=0;i<cypt.length();i+=2){
String sub=cypt.substring(i,i+2);
System.out.println(sub);
for(Entry<Character, String> entry:entrySet){
//当前子字符串和entry中的value比较
if(sub.equals(entry.getValue())){
sb2.append(entry.getKey());
break;//一旦找到匹配的key就不在寻找。
}
}
}
if(sb2.length()==cypt.length()/2){
System.out.println("解密成功"+sb2.toString());
}else{
System.out.println("解密失败");
}
}
相关文章推荐
- Top 10 Java Serialization Interview Questions and Answers
- iOS中 FMDB第三方SQLite数据库 UI_20
- iOS中 FMDB第三方SQLite数据库 UI_20
- phpCAS::handleLogoutRequests()关于java端项目登出而php端项目检测不到的测试
- iOS UITableView 相关知识
- iOS开发之UIlabel多行文字自动换行 (自动折行)
- UIView hitTest:withEvent:方法流程
- iOS 中UUID唯一标记的使用
- ios UIView animateWithDuration 学习
- AndroidUI--SimpleAdapter CursorAdapter
- Leetcode:unique_binary_search_trees
- 记录使用Buildbot遇到的坑
- Mechanize Guide
- Retrofit(2.0)入门小错误 -- Could not locate ResponseBody xxx Tried: * retrofit.BuiltInConverters
- java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Timestamp
- FDStackView —— Downward Compatible UIStackView (Part 1)
- 【UIAlertView警报和UIActionSheet操作表】
- Android UI组件的动态更新
- EasyUI加载内嵌json数据方法
- Handler post()等在子线程中更新主线程的UI的方法使用小汇