CodeTable应用

package com;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class CodeTableTest {

public static void main(String args[]) {
/*
*parentMap:键作为类型,值保存此类型的所有汉化对应关系
*形如:<B:<001:海淀>>
*/
Map<String, Map<String, String>> parentMap = new HashMap<String, Map<String, String>>();
Map<String, String> childMap = null ;
Set<String> setType = new HashSet<String>();//保存类型
String tempType = null ;//保存临时类型
List<CodeTable> allCode = null;//保存数据库表所有数据
allCode = getAllCode();
for(CodeTable tempCode:allCode){
setType.add(tempCode.getCodeType());
}

Iterator<String> iteratorType = setType.iterator();

while(iteratorType.hasNext()){//此循环需要优化

tempType =iteratorType.next();
childMap = new HashMap<String, String>();
for(CodeTable tempCode:allCode){
if(tempType.equals(tempCode.getCodeType())){
childMap.put(tempCode.getCodeKey(), tempCode.getCodeName());
}
}
parentMap.put(tempType, childMap);
}

//以上过程在应用启动时需要一次对parentMap组装完数据,以后只需对parentMap操作即可
//测试

System.out.println(getAllName("S",parentMap));
System.out.println(getName("B","001",parentMap));


}
/**
* 通过类型和键值,返回名称,提供给页面进行汉化
* return String
*/
private static String getName(String codeType,String codeKey,Map<String, Map<String, String>> map){
String tempName = getAllName(codeType,map).get(codeKey) ;
if(tempName==null){
tempName = "没有对应值" ;
}
return tempName ;
}

/**
* 通过类型得到Map,提供给页面组装成下拉列表
* return Map
*/

private static Map<String, String> getAllName(String codeType,Map<String, Map<String, String>> map){
Map<String, String> tempMap = map.get(codeType);
if(tempMap==null){
tempMap = new HashMap<String, String>();
tempMap.put("", "--请选择--");
return tempMap ;
}
return tempMap;

}


private static List<CodeTable> getAllCode() {
CodeTable ct = null;
List<CodeTable> allCode = new ArrayList<CodeTable>();

ct = new CodeTable();
ct.setCodeType("B");
ct.setCodeName("海淀");
ct.setCodeKey("001");
allCode.add(ct);

ct = new CodeTable();
ct.setCodeType("B");
ct.setCodeName("丰台");
ct.setCodeKey("002");
allCode.add(ct);

ct = new CodeTable();
ct.setCodeType("B");
ct.setCodeName("朝阳");
ct.setCodeKey("003");
allCode.add(ct);

//

ct = new CodeTable();
ct.setCodeType("S");
ct.setCodeName("浦东东区");
ct.setCodeKey("004");
allCode.add(ct);

ct = new CodeTable();
ct.setCodeType("S");
ct.setCodeName("浦东西区");
ct.setCodeKey("005");
allCode.add(ct);

ct = new CodeTable();
ct.setCodeType("S");
ct.setCodeName("浦东南区");
ct.setCodeKey("006");
allCode.add(ct);

return allCode;
}

}

class CodeTable {

private String codeType;
private String codeName;
private String codeKey;

public String getCodeType() {
return codeType;
}

public void setCodeType(String codeType) {
this.codeType = codeType;
}

public String getCodeName() {
return codeName;
}

public void setCodeName(String codeName) {
this.codeName = codeName;
}

public String getCodeKey() {
return codeKey;
}

public void setCodeKey(String codeKey) {
this.codeKey = codeKey;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值