简体   繁体   中英

java HashMap contains Set as key and arraylist as value

Hi I got problem in my code.

String[] vArr = new String[4];
vArr[0] = "329~abc-2~PACKET_DATA_GATEWAY_IP_ADDRESS~1.2.3.4";
vArr[1] = "328~abc-1~PACKET_DATA_GATEWAY_IP_ADDRESS~1.1.1.1";
vArr[2] = "329~abc-2~PACKET_DATA_GATEWAY_PORT_NUMBER~123";
vArr[3] = "328~abc-1~PACKET_DATA_GATEWAY_PORT_NUMBER~123";

if(vArr != null && vArr.length > 0) {
    System.out.println("vArr.length ::" + vArr.length);                 
    ArrayList<VehicleTO> aprogList = new ArrayList<VehicleTO>();            

    for(int i = 0;i < vArr.length; i++) {
        System.out.println("Inside for loop ::" + vArr.length);
        String[] iData = vArr[i].split("~");
        System.out.println("value retrieved :" + Arrays.toString(iData));
        HashMap<Set, ArrayList<VehicleTO>> programValues = 
                             new HashMap<Set, ArrayList<VehicleTO>>();

        String programName = null;
        programName = iData[1];

        HashSet<String> listnerOutputArgList = new HashSet<String>();
        listnerOutputArgList.add(programName);
        Collections.unmodifiableSet(listnerOutputArgList);

        String progId = null;
        String paramName = null;
        String paramvalue = null;
        progId = iData[0];
        paramName = iData[2];
        paramvalue = iData[3];                  

        VehicleTO vehTO = new VehicleTO();                      
        vehTO.setProgramName(programName);

        if(paramName.equals("PACKET_DATA_GATEWAY_IP_ADDRESS")) {    
            vehTO.setIpAddress(paramvalue);         
        }
        System.out.println("Setting Values ip ::" + vehTO.getIpAddress());

        if(paramName.equals("PACKET_DATA_GATEWAY_PORT_NUMBER")) {   
            vehTO.setPortNumber(paramvalue);                
        }
        System.out.println("Setting Values port ::" + vehTO.getPortNumber());

        if(listnerOutputArgList.contains(vehTO.getProgramName())) {
            aprogList.add(vehTO);
        }
        int progDet = aprogList.size();

        System.out.println("The list value:"+ progDet);         

        programValues.put(listnerOutputArgList,aprogList);

        for (Iterator<Set> it = programValues.keySet().iterator(); it.hasNext()) {
            Set key = it.next();
            if(programValues.containsKey(key)) {
                programValues.get(key).add(vehTO);
                ArrayList<VehicleTO> value = programValues.get(key);

                for (int nCount = 0; nCount < value.size(); nCount++) {
                         System.out.println((String)value.get(nCount).getIpAddress());
                         System.out.println((String)value.get(nCount).getPortNumber());
                }
        } 
    }       
}

THe out put need to display as:

abc-1   1.2.3.4  123
abc-2   1.1.1.1  123

But my problem is The list value is showing as: 4 instead of 2 because of that out put is wrong.

How to solve this:

I did not try to find an answer; better first simplify, so that it is more readable. I saw that you are still not quite confident with java 7, hence some rewritten code:

Use Set<String> .

        for (Set<String> key : programValues.keySet()) {
             ArrayList<VehicleTO> value = programValues.get(key);
             value.add(vehTO);
             for (VehicleTO vTO : value) {
                 System.out.println((String)vTO.getIpAddress());
                 System.out.println((String)vTO.getPortNumber());
             }
         }

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