如何正确书写if条件

发布于 2024-11-03 20:00:45 字数 584 浏览 1 评论 0原文

我有一个关于在循环中编写 if 条件的问题。非正式地,我希望条件按如下方式工作。我将有一个字符串,我想将它与字符串列表进行比较。如果它与列表中的任何值都不匹配,则继续执行 if 语句的主体。如果它确实匹配其中一个值,则跳过 if 语句的主体。这是我的代码(我写的注释只是为了清楚):

Iterator it=row.entrySet().iterator();  
Iterator iter=getPrimaryKey.entrySet().iterator();

Map.Entry pairs=(Map.Entry)it.next();
            if(!(pairs.getKey().equals(iter.next()))){  //this is the condition. I'm talking about. I want the key to run through all the membres of iter. If it is distinct from all of them, go ahead and execute the code in the if statement. 

无论如何,我不知道我做错了什么,所以我有兴趣听到建议。

I have a question about writing the if condition in a loop. Informally, I want the condition to work as follows. I will have a string, and I want to compare it against a list of strings. If it does not match any of the values in the list, then go ahead and execute the body of the if statement. If it does match one of the values, skip the body of the if statement. Here is my code (I wrote comments just be clear):

Iterator it=row.entrySet().iterator();  
Iterator iter=getPrimaryKey.entrySet().iterator();

Map.Entry pairs=(Map.Entry)it.next();
            if(!(pairs.getKey().equals(iter.next()))){  //this is the condition. I'm talking about. I want the key to run through all the membres of iter. If it is distinct from all of them, go ahead and execute the code in the if statement. 

Anyways, I do not know what I'm doing wrong so I'd be interested in hearing suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

请远离我 2024-11-10 20:00:45

如果我理解正确的话,您将需要设置一个标志(如果找到),然后在遍历整个列表后,使用该标志来确定是否在 while< 之外执行 if 语句的主体/代码> 循环:

boolean found = false;  // start off as not found

// while items available and match not found, keep looking
while(!found && it.hasNext()) {
    Map.Entry pairs = (Map.Entry)it.next();
    if (/* condition */)
        found = true;  // item has been found
}

// if we made it through whole list without finding match, then do stuff
if (!found)
    // do fun things here

If I understand you correctly, you will want to set a flag if it is found, then after the whole list has been traversed, use that flag to determine whether to do the body of the if statement, outside of the while loop:

boolean found = false;  // start off as not found

// while items available and match not found, keep looking
while(!found && it.hasNext()) {
    Map.Entry pairs = (Map.Entry)it.next();
    if (/* condition */)
        found = true;  // item has been found
}

// if we made it through whole list without finding match, then do stuff
if (!found)
    // do fun things here
戒ㄋ 2024-11-10 20:00:45

假设您希望它执行集合中的任何唯一值:

if(!row.entrySet().containsValue(pairs.getValue())
   //execute

Assuming you want it to execute any unique value in the set:

if(!row.entrySet().containsValue(pairs.getValue())
   //execute
灰色世界里的红玫瑰 2024-11-10 20:00:45

在我看来,您想检查“it”迭代器中的字符串是否作为 getPrimaryKey.entrySet() 中的字符串之一存在,因此您可能想直接检查它是否包含在那里。

Iterator it=row.entrySet().iterator();  
Iterator iter=getPrimaryKey.entrySet().iterator();

 while(it.hasNext()){
    Map.Entry pairs=(Map.Entry)it.next();
    if(! getPrimaryKey.keySet().contains(pairs.getKey()){ 
    }

It sounds to me like you want to check if a string in the "it" iterator is present as one of hte strings in the getPrimaryKey.entrySet(), so you might want to check directly if its contained there.

Iterator it=row.entrySet().iterator();  
Iterator iter=getPrimaryKey.entrySet().iterator();

 while(it.hasNext()){
    Map.Entry pairs=(Map.Entry)it.next();
    if(! getPrimaryKey.keySet().contains(pairs.getKey()){ 
    }
酸甜透明夹心 2024-11-10 20:00:45

我假设您引用的key是一个String。我会把你的代码写如下:

    Map<String, Object> row = getTypedRow();
    Map<String, Object> primaryKey = getTypedPrimaryKey();

    Iterator<Map.Entry<String, Object>> it = row.entrySet().iterator();
    Iterator<Map.Entry<String, Object>> iter = primaryKey.entrySet().iterator();


    for (String key : row.keySet()) {
        if ( ! primaryKey.keySet().contains(key) ) {
            // it will be here only for key from the row that aren't present in the primaryKey map.
        }
    }

I assume the key you are referring is a String. I would write your code as following:

    Map<String, Object> row = getTypedRow();
    Map<String, Object> primaryKey = getTypedPrimaryKey();

    Iterator<Map.Entry<String, Object>> it = row.entrySet().iterator();
    Iterator<Map.Entry<String, Object>> iter = primaryKey.entrySet().iterator();


    for (String key : row.keySet()) {
        if ( ! primaryKey.keySet().contains(key) ) {
            // it will be here only for key from the row that aren't present in the primaryKey map.
        }
    }
冷夜 2024-11-10 20:00:45

像这样的事情怎么样......你的迭代器非常令人困惑......

/*ASSUMPTIONS*/
Map<String, Object> row = something;
Map<String, Object> primaryKeyMap = something;
/*END ASSUMPTIONS*/

Set<Map.Entry<String, Object>> entryset = row.entrySet();
for (Map.Entry<String, Object> entry : entryset) {
    if (primaryKeyMap.containsKey(entry.getKey())) {
        //do stuff...
        break;
    }
}

How about something like this... your iterators are very confusing...

/*ASSUMPTIONS*/
Map<String, Object> row = something;
Map<String, Object> primaryKeyMap = something;
/*END ASSUMPTIONS*/

Set<Map.Entry<String, Object>> entryset = row.entrySet();
for (Map.Entry<String, Object> entry : entryset) {
    if (primaryKeyMap.containsKey(entry.getKey())) {
        //do stuff...
        break;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文