如何在Java映射中进行for循环检查,并在if语句后使值为false?
我有这个功能。当我按下html页面中的复选框时,它应该会更新我的Java映射。所以问题是,它可以完美地为特定值生成贴图值true,但当我想要生成反转函数,使false时,它不起作用如何在Java映射中进行for循环检查,并在if语句后使值为false?,java,Java,我有这个功能。当我按下html页面中的复选框时,它应该会更新我的Java映射。所以问题是,它可以完美地为特定值生成贴图值true,但当我想要生成反转函数,使false时,它不起作用 @PostMapping("/updateUser") public ModelAndView updateTechs(@ModelAttribute User user, @RequestParam("techCheckbox") List<String> techs) { User user
@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);
}
@PostMapping(/updateUser)
公共ModelAndView UpdateTech(@ModelAttribute用户,@RequestParam(“techCheckbox”)列表技术)
{
User userFromDB=userInterface.findByUsername(User.getUsername());
Map allTechs=userFromDB.getLanguagesKnows();
对于(int i=0;i
谢谢大家! 一种可能的解决方案是在应用
techs
列表中的输入之前“归零”(假出)allTechs
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);
}
for(Map.Entry:allTechs.entrySet())
{
System.out.println(entry.getKey()+“/”+entry.getValue());
allTechs.put(entry.getKey(),false);
}
for(迭代器i=techs.Iterator();i.hasNext();){
String item=i.next();
//可能不安全;我们还没有检查确认
//“技术”在我们的名单上是有效的
所有技术放置(项目,真实);
}
一种可能的解决方案是在应用技术
列表中的输入之前“归零”(假出)所有技术
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);
}
for(Map.Entry:allTechs.entrySet())
{
System.out.println(entry.getKey()+“/”+entry.getValue());
allTechs.put(entry.getKey(),false);
}
for(迭代器i=techs.Iterator();i.hasNext();){
String item=i.next();
//可能不安全;我们还没有检查确认
//“技术”在我们的名单上是有效的
所有技术放置(项目,真实);
}
@vikiii当我删除if时,它会使所有值都为false,除了一个,但它应该首先检查一个键的所有值,如果不等于,则使其为false。不是hapenning@vikiiii当我删除if时,它会使所有值都为false,除了一个,但它应该首先检查一个键的所有值,如果不等于,则使其为false。这不可能