我有一个类似下面的hashmap:
someMap= new HashMap<Integer, String>();
someMap.put(1, "variable1");
someMap.put(2, "variable2");
someMap.put(3, "variable3");
someMap.put(4, "variable4");
someMap.put(5, "variable5");
我有一个类似下面的java类:
public class SomeVO {
Long someNumber;
String shortDesc;
public Long getSomeNumber() {
return someNumber;
}
public void setSomeNumber(Long someNumber) {
this.someNumber = someNumber;
}
public String getShortDesc() {
return shortDesc;
}
public void setShortDesc(String shortDesc) {
this.shortDesc = shortDesc;
}
}
在数据库中我有像
这样的值someNumber and short-description
当我查询数据库时,我返回一个列表,其中包含上述信息:
List<SomeVO > existingSomeNumberAndShortDescriptionList
现在我必须将List
与someMap
进行比较,然后返回两个地图,这些地图将变量作为该变量的键和简短描述。
就像我必须从existingSomeNumberAndShortDescriptionList
进行比较,我需要得到像
variable1,shortDescription(来自existingSomeNumberAndShortDescriptionList中可用的数据库),
和变量1,Y或N,如果某个号码在列表中可用,则为Y
其他N
答案 0 :(得分:3)
您的代码将是这样的:
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// Loaded Hashmap-------------------------------------------------------------------
HashMap<Long, String> someMap= new HashMap<Long, String>();
someMap.put(1L, "variable1");
someMap.put(2L, "variable2");
someMap.put(3L, "variable3");
someMap.put(4L, "variable4");
someMap.put(5L, "variable5");
// List getting from db-------------------------------------------------------------------
List<SomeVO> existingSomeNumberAndShortDescriptionList = new ArrayList<SomeVO>();
SomeVO someVO1=new SomeVO();
someVO1.setSomeNumber(1L);
someVO1.setShortDesc("Description 1");
SomeVO someVO2=new SomeVO();
someVO2.setSomeNumber(2L);
someVO2.setShortDesc("Description 2");
existingSomeNumberAndShortDescriptionList.add(someVO1);
existingSomeNumberAndShortDescriptionList.add(someVO2);
//--------------------------------------------------------------------------------------------
HashMap<String, String> hashmap1 =new HashMap<String, String>();
HashMap<Long, String> hashmap2 =new HashMap<Long, String>();
//Iterate through list of bean
for (Iterator<SomeVO> iterator = existingSomeNumberAndShortDescriptionList .iterator(); iterator.hasNext();) {
SomeVO someVO = (SomeVO) iterator.next();
// Compare key with main hashmap and Put in hashmap 1
hashmap1.put(someMap.get(someVO.getSomeNumber()),someVO.getShortDesc());
// Compare key with main hashmap and check if number exists and Put in hashmap 2
if(someMap.containsKey(someVO.getSomeNumber()))
hashmap2.put(someVO.getSomeNumber(),"Y");
else
hashmap2.put(someVO.getSomeNumber(),"N");
}
// print hashmaps
System.out.println(hashmap1);
System.out.println(hashmap2);
}
输出将是......
{variable1=Description 1, variable2=Description 2}
{1=Y, 2=Y}