简体   繁体   English

LinkedHashMap-包含方法

[英]LinkedHashMap - contains method

I need to read a flag to validate if I'll send or not an email, this flag is on a LinkedHashMap , so I'm trying to go over it looking for an ID, then I'll have to ask if is true or false, and I'm trying this: 我需要阅读一个标志以验证是否发送电子邮件,该标志位于LinkedHashMap ,因此我试图遍历该标志以查找ID,然后必须询问是否为true或错误,我正在尝试:

    while(iterator.hasNext()){

         LinkedHashMap<String, Object> fileMap = (LinkedHashMap<String, Object>) iterator.next();
         userid = (Long)fileMap.get("USERID");
         file.findAllFiles(userid);

         if(file.findAllFiles(userid).contains(haveFiles = true)){
             //send email
         }else{
             //do something
         }

     }

Is it correct? 这是正确的吗?

Assuming it works up to the point of: 假设它可以起到以下作用:

if(file.findAllFiles(userid).contains(haveFiles = true))

Then I think the issue is in that condition itself. 然后我认为问题在于这种情况本身。 What you have written is roughly equivalent of writing 你写的东西大致相当于写作

??? haveFiles = true;    
if(file.findAllFiles(userid).contains(haveFiles))

(where ??? can be either Object , Boolean or boolean , don't know from your code snapshot) (其中???可以是ObjectBooleanboolean ,从代码快照中不知道)

I bet what you might want to do is something like 我敢打赌,您可能想做的是

Boolean haveFiles = file.findAllFiles(userid).get("haveFiles");
if (Boolean.TRUE.equals(haveFiles) {

What you have done calls the contains method which receives any Object and returns true if that object is among the values of the map. 完成的操作称为contains方法,该方法接收任何Object,如果该对象在映射的值中,则返回true。 You probably don't want that, I guess what you want to do is to retrieve the value associated to the "haveFiles" key into the map, for that you have to use the get method. 您可能不希望这样做,我想您要做的是将与“ haveFiles”键关联的值检索到映射中,因为您必须使用get方法。

PS: Also, if file.findAllFiles(userid) method is truly calling some DB stuff you might want to invoke it only once (the first time looks totally unnecesary to me). PS:另外,如果file.findAllFiles(userid)方法确实在调用某些数据库内容,则您可能只想调用一次(对我而言,第一次看起来完全不必要)。

I think your if expression has a logical error. 我认为您的if表达式有逻辑错误。

haveFiles = true

will always return true, reducing your expression to : 将始终返回true,将您的表达式简化为:

file.findAllFiles(userid).contains(true)

since (haveFiles = true) assigns the value true to the variable haveFiles. 因为(haveFiles = true)将值true分配给变量haveFiles。

You should rather be doing something on the following lines : 您应该在以下几行上做一些事情:

Map<Flag, Boolean> keyValueMap = file.findAllFiles(userid)
if(keyValueMap.contains(Flag.HAVE_FILES) == true){
     //send email
 }else{
     //do something
 }

..asuming you the file.findAllFiles() returns a map object with flags and their boolean status and Flag is an enum containing all the supported flags. ..假设您使用file.findAllFiles()会返回一个带有标志及其布尔状态的映射对象,并且Flag是一个包含所有受支持标志的枚举。

The expression 表达方式

(keyValueMap.contains(Flag.HAVE_FILES) == true)

will evaluate to true only when keyValueMap contains an entry for Flag.HAVE_FILES and the value is true. 仅当keyValueMap包含Flag.HAVE_FILES的条目且值为true时,才会评估为true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM