1

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;
    }
}
1
  • You neither need a loop or that if for that, just call tmpFacts.remove("response") and to delete more, create a separate method with a varargs private void removeFromMap(Map<....> map, String... keysToRemove) and call it like removeFromMap("response", "blub", "test");. Commented Oct 26, 2015 at 1:04

1 Answer 1

1

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");
Sign up to request clarification or add additional context in comments.

2 Comments

So you prefer to iterate the list for each key (which is O(n)) instead if just calling tmpFacts.remove(..) for each item of that list, which would be O(1)? ... ok.
It depends. Given the loop I assumed that you'd want to add or remove things from the removable list dynamically. If it is a static list, and my code here creates a static list, then yeah you'd remove the loop and just have the remove statements, as you said in your comment to the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.