The java for loop has two sets. All the child elements of the first set are compared with the child elements of the second set. Change it into a sentence and ask for advice on how to write it.

The

java for loop has two collections. All the child elements of the first set are compared with the child elements of the second set. How to write it in grammar? Ask for the advice of the great gods,

problem model:
list1=,
list2=,
list1,list2 are variables. Here is just a few random numbers to show.
Loop diagram: and so on

clipboard.png

when the object of each List1 is compared with the list2 object in a loop, if judges what happens if it is the same, and continues to loop if it is not the same.

Apr.11,2021

Thank you for the invitation

List<Integer> list1 = new ArrayList<Integer>();
        list1.addAll(Arrays.asList(new Integer[]{1,2,3,4,5,6}));
        List<Integer> list2 = new ArrayList<Integer>();
        list2.addAll(Arrays.asList(new Integer[]{3,5,6}));
        for(Integer var2 : list2){
            for(Integer var1 : list1){
                if(var2.equals(var1)){
                    System.out.println("do something");
                }
            }
        }


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.UUID;


public class Test {
    
    public static void main(String[] args) throws Exception {

        //
        List<Model> list1 = new ArrayList<Model>();
        list1.add(new Model(1,"11"));
        list1.add(new Model(2,"33"));
        list1.add(new Model(3,"33"));
        List<Model> list2 = new ArrayList<Model>();
        list2.add(new Model(1,"11"));
        list2.add(new Model(2,"33"));
        list2.add(new Model(3,"33"));
        for(Model m2 : list2){
            for(Model m1 : list1){
                if(m1.getAttr2().equals(m2.getAttr2())){
                    System.out.println("do something");
                }
            }
        }
        /**Comparator**/
        // Object
        compareSomething(list1,list2,new Comparator<Model>() {
            @Override
            public int compare(Model o1,Model o2) {
                return o1.getAttr2().compareTo(o2.getAttr2());
            }
        });
        
        
        // Integer
        List<Integer> list3 = new ArrayList<Integer>();
        list3.addAll(Arrays.asList(new Integer[]{1,2,3,4,5,6}));
        List<Integer> list4 = new ArrayList<Integer>();
        list4.addAll(Arrays.asList(new Integer[]{3,5,6}));
        compareSomething(list3,list4,new Comparator<Integer>() {
            @Override
            public int compare(Integer o1,Integer o2) {
                return o1.compareTo(o2);
            }
        });
    }
    
    
    public static <T> void compareSomething(List<T> list1,List<T> list2,Comparator<T> compare){
        for(T m2 : list2){
            for(T m1 : list1){
                 if(0 == compare.compare(m1, m2)){
                       System.out.println("do something");
                 }
            }
        }
    }
    
  
}
class Model{
    
    private Integer attr1 = 1;
    
    private String attr2 = UUID.randomUUID().toString();

    public Model(){
        
    }
    
    public Model(Integer attr1,String attr2){
        this.attr1 = attr1;
        this.attr2 = attr2;
    }
    
    public Integer getAttr1() {
        return attr1;
    }

    public void setAttr1(Integer attr1) {
        this.attr1 = attr1;
    }

    public String getAttr2() {
        return attr2;
    }

    public void setAttr2(String attr2) {
        this.attr2 = attr2;
    }
}

Menu