0

I have this function. It should update my Java Map when I press on checkbox in my html page. So the problem that it works perfect for making Map value true for specific value, but when I want to make reverse function, to make false, it doesn't work.

@PostMapping("/updateUser")
public ModelAndView updateTechs(@ModelAttribute User user, @RequestParam("techCheckbox") List<String> techs)
{

    User userFromDB = userInterface.findByUsername(user.getUsername());

    Map<String, Boolean> allTechs = userFromDB.getLanguagesKnows();

    for(int i = 0; i < allTechs.size(); i++)
    {
        for(int q = 0; q < techs.size(); q++)
        {
            if(allTechs.keySet().toArray()[i].equals(techs.get(q)))
                allTechs.put(String.valueOf(allTechs.keySet().toArray()[i]), true); // Everything is ok, map is updated

            else // If no matches between keys
            {
                /* If i don't have this IF statement, then each element becomes false except for the last one 
                    But at the same time this IF doesn't work
                */
                if(q == techs.size())
                {
                    allTechs.put(String.valueOf(allTechs.keySet().toArray()[i]), false); // Isn't executed
                }
            }
        }
    }

    userInterface.save(userFromDB);
}

Thank you!

1
  • @vikiiii when I delete that if, it makes all values false, except for the one, but it should first of all check all values for one key, and if not equals, make it false. It's not hapenning Commented Mar 9, 2018 at 20:25

1 Answer 1

2

One possible solution is to "zero-out" (false-out) allTechs before applying the input from the techs list.

for (Map.Entry<String, Boolean> entry : allTechs.entrySet())
{
    System.out.println(entry.getKey() + "/" + entry.getValue());
    allTechs.put(entry.getKey(), false);
}

for (Iterator<String> i = techs.iterator(); i.hasNext();) {
    String item = i.next();
    // possibly unsafe; we haven't checked to make sure 
    // that the "tech" is a valid one for our list
    allTechs.put(item, true); 
}
Sign up to request clarification or add additional context in comments.

Comments

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.