简体   繁体   中英

Compare and return HashMap for given value

I have a hashmap which is like the below :

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");

and i have a java class like the below :

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;
    }

}

in database i have values like

someNumber and short-description 

when i query database i return a list which will have the above information :

List<SomeVO > existingSomeNumberAndShortDescriptionList

now i have to compare this List withe someMap and return two maps which will have variable as key and short Description for that variable.

like i have to compare from the existingSomeNumberAndShortDescriptionList and i need to have final results like

variable1, shortDescription(this from database which is available in existingSomeNumberAndShortDescriptionList),

and variable1, Y or N if someNumber is available in List it than Y else N

Your code will be like this :

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);

}

And output will be..

{variable1=Description 1, variable2=Description 2}
{1=Y, 2=Y}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM