简体   繁体   English

我可以根据特定条件对每个循环进行迭代吗?

[英]Can I iterate for each loop on the basis of specific condition?

Suppose there are two lists of two classes say Class A and Class B respectively listOfClassesA and listOfClassesB . 假设有两个类别的两个列表,分别是Class AClass B listOfClassesAlistOfClassesB

Now I need to iterate these lists in a single loop on specific condition that suppose boolean variable test is true than iterate list A otherwise B, So I want something like below 现在,我需要在特定条件下(假设布尔变量测试为真)在单个循环中迭代这些列表,而不是迭代列表A否则B,所以我想要以下内容

Boolean test = true;
For( test ? A a:B b   :   test ? listOfClassesA : listOfClassesB){
     String id = test?a.getId():b.getId();
}

I have this requirement because in almost both cases almost all code are same so I dont want to repeat loop two times, but I didn't find any way to do this. 我有这个要求,因为在几乎两种情况下,几乎所有代码都是相同的,因此我不想重复两次循环,但是我没有找到任何方法来执行此操作。 Is there any way to achieve this? 有什么办法可以做到这一点?

No, Java does not let you write a loop like that. 不,Java不允许您编写这样的循环。 What you can do is defining a generic method * , and put a call to that method in a body of your loop. 您可以做的是定义一个通用方法* ,然后在循环体中调用该方法。 This way you would need to repeat only the loop header, and not the code inside the loop body. 这样,您将只需要重复循环头,而不需要重复循环体内的代码。

if (test) {
    for (A a : listOfA) loopBody(a);
} else {
    for (B b : listOfB) loopBody(b);
}
...
private static <T> void loopBody(T item) {
    ...
}


* If the two classes are related, a method that takes their common superclass would work as well. *如果两个类相关联,则采用它们的公共超类的方法也将起作用。

You can implement a new class (can be anonymous) implementing the Iterable interface that will return your iterator responsible for iterating over listOfClassesA or listOfClassesB . 您可以实现一个新类(可以是匿名的),该类实现Iterable接口,该接口将返回负责迭代listOfClassesAlistOfClassesB迭代器。 However, in the definition of your iterator you can can only use the class A and B common super type. 但是,在迭代器的定义中,您只能使用类AB公共超级类型。

That is what I had on mind: 那就是我的想法:

interface I { }
class A implements I { }
class B implements I { }

class MyIterator implements Iterable<I> {
    private List<I> list;

    public MyIterator(boolean test, List<A> listOfClassesA, List<B> listOfClassesB) {
        super();
        this.list = new ArrayList<I>(test ? listOfClassesA : listOfClassesB);
    }

    @Override
    public Iterator<I> iterator() {
        return list.iterator();
    }   
}

public class Test {
    public static void main(String[] args) throws Exception {
        List<A> listOfClassesA = new ArrayList<A>();
        List<B> listOfClassesB = new ArrayList<B>();

        for(I elm : new MyIterator(true, listOfClassesA, listOfClassesB)) {
            // elm is of I type 
        }
    }
}

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

相关问题 如何根据条件设置Textview? - How can I set the Textview on the basis of a condition? 我可以在struts 1的逻辑迭代内部循环吗? - can i loop inside logic iterate of struts 1? 每次迭代循环时,对象都会被覆盖 - Objects are being overwritten each time i iterate over the loop 如何使用每种类型的循环迭代通用列表 - How Do I Iterate Generic List Using For Each Type For Loop How can I use Java Stream in order to iterate on a collection on object and use a specific string field of each object to create a new array? - How can I use Java Stream in order to iterate on a collection on object and use a specific string field of each object to create a new array? 是否可以在以下条件中找到哪个表达式评估为true,在此基础上我可以找到a的值 - Is it possible to find which expression evaluated true in the following if condition and on that basis I can find the value of a 循环特定时间的For循环 - Iterate For loop for specific time line 我可以在 Java 中使用 for-each 遍历 NodeList 吗? - Can I iterate through a NodeList using for-each in Java? 我可以遍历以Java中特定字符串开头的HDFS文件吗? - Can I iterate through HDFS files that begin with a specific string in java? 我可以随机迭代for循环而不是顺序吗? - Can I iterate through a for loop randomly instead of sequentially?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM