Common elements in two lists

后端 未结 14 1080
无人共我
无人共我 2020-11-22 12:22

I have two ArrayList objects with three integers each. I want to find a way to return the common elements of the two lists. Has anybody an idea how I can achiev

14条回答
  •  爱一瞬间的悲伤
    2020-11-22 12:55

        // Create two collections:
        LinkedList listA =  new LinkedList();
        ArrayList listB =  new ArrayList();
    
        // Add some elements to listA:
        listA.add("A");
        listA.add("B");
        listA.add("C");
        listA.add("D");
    
        // Add some elements to listB:
        listB.add("A");
        listB.add("B");
        listB.add("C");
    
        // use 
    
        List common = new ArrayList(listA);
        // use common.retainAll
    
        common.retainAll(listB);
    
        System.out.println("The common collection is : " + common);
    

提交回复
热议问题