简体   繁体   中英

Removing multiple objects from a HashMap

I've created a HashMap facts, which holds key(className) and value(pojo).

I want to remove more than one class objects from that hashMap while sending it to a method, the code I've below only removes one Class object (response), what if I wanted to remove more then one, (say response & HULC). Also I don't want to remove the objects from facts hashMap , hence created another copy of it.

I would appreciate your inputs thanks in advance :)

public class TempAvengersTest {

    public static void main(String[] args) {

        TempAvengersTest test = new TempAvengersTest();

        //setting avengersVo
        AvengersVo avengersVo = new AvengersVo();
        avengersVo.setNumberOfSuperHeros("6");
        avengersVo.setReleaseDate("May6,2016");

        HulcVO hulcVo = new HulcVo();
        hulcVo.setweight("2000");
        hulcVo.setAggression("Max");

        IronManVo imVo = new IronManVo();
        imVo.setAttitude("Max");
        imVo.setNewGear(true);

        HashMap<String, Integer> positiveFeedback = new HashMap<String, Integer>();
        positiveFeedback.put("IMDB", 8);
        positiveFeedback.put("rottenTomatoes", 78);

        AudienceResponseVo responseVo = new AudienceResponseVo();
        responseVo.setNegativeFeedbackInd(true);
        responseVo.setPositiveFeedback(positiveFeedback);

        HashMap<String, Object> facts = new HashMap<String, Object>();
        facts.put("Movie", avengersVo);
        facts.put("HULC", hulcVo);
        facts.put("IronMan", imVo);
        facts.put("response", responseVo);

        sendToPostScreeningHandler(test.sendToPreScreeningHandler(facts));


        System.out.println("\n\n********************************************  END  ********************************************");
    }

    private HashMap<String, Object> sendToPreScreeningHandler (HashMap<String, Object> facts){

        //" facts needs to be unchanged, Hence creating tmpFacts " !!!
        HashMap<String, Object> tmpFacts = new HashMap<String, Object>();
        tmpFacts.putAll(facts);

        //Deleting response object map from tmpFacts.
           for (Object key : facts.keySet()) {
                if("response".equals(key)){
                    tmpFacts.remove(key);
                }
           }

        System.out.println("PreScreeningEngineVo: "+AvengersJSonConverter.convertToJSON(tmpFacts));
        System.out.println("****************************************************************************************************");
        System.out.println("PostScreeningEngineVo: "+AvengersJSonConverter.convertToJSON(facts));

        return facts;
    }
}

You could use a list of elements to remove and the contains method.

So this:

   if("response".equals(key))

Would change to:

if(removables.contains(key))

But, you'd have to declare removables somewhere, like this:

private List<String> removables = new ArrayList("response", "HULC");

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