[java] 두 ArrayList의 차이를 어떻게 계산할 수 있습니까?

두 개의 ArrayList가 있습니다.

ArrayList A에는 다음이 포함됩니다.

['2009-05-18','2009-05-19','2009-05-21']

ArrayList B에는 다음이 포함됩니다.

['2009-05-18','2009-05-18','2009-05-19','2009-05-19','2009-05-20','2009-05-21','2009-05-21','2009-05-22']

ArrayList A와 ArrayList B를 비교해야합니다. 결과 ArrayList에는 ArrayList A에없는 List가 포함되어야합니다.

ArrayList 결과는 다음과 같아야합니다.

['2009-05-20','2009-05-22']

비교하는 방법?



답변

Java에서는 Collection인터페이스의 removeAll메소드를 사용할 수 있습니다 .

// Create a couple ArrayList objects and populate them
// with some delicious fruits.
Collection firstList = new ArrayList() {{
    add("apple");
    add("orange");
}};

Collection secondList = new ArrayList() {{
    add("apple");
    add("orange");
    add("banana");
    add("strawberry");
}};

// Show the "before" lists
System.out.println("First List: " + firstList);
System.out.println("Second List: " + secondList);

// Remove all elements in firstList from secondList
secondList.removeAll(firstList);

// Show the "after" list
System.out.println("Result: " + secondList);

위의 코드는 다음 출력을 생성합니다.

First List: [apple, orange]
Second List: [apple, orange, banana, strawberry]
Result: [banana, strawberry]


답변

이미 정답이 있습니다. 그리고리스트 (컬렉션) 사이에서 더 복잡하고 흥미로운 작업을하고 싶다면 아파치 커먼즈 컬렉션 ( CollectionUtils )을 사용하세요. 그것은 연결 / 분리, 교차점 찾기, 한 컬렉션이 다른 컬렉션인지 다른 좋은 것들의 하위 집합인지 확인할 수 있습니다.


답변

스트림이있는 Java 8에서는 실제로 매우 간단합니다. 편집 : 스트림없이 효율적일 수 있습니다.

List<String> listA = Arrays.asList("2009-05-18","2009-05-19","2009-05-21");
List<String> listB = Arrays.asList("2009-05-18","2009-05-18","2009-05-19","2009-05-19",
                                   "2009-05-20","2009-05-21","2009-05-21","2009-05-22");

List<String> result = listB.stream()
                           .filter(not(new HashSet<>(listA)::contains))
                           .collect(Collectors.toList());

해시 세트는 한 번만 생성됩니다. 메서드 참조는 contains 메서드에 연결됩니다. 람다로 동일한 작업을 수행하려면 변수에 세트가 있어야합니다. 변수를 만드는 것은 나쁜 생각이 아닙니다. 특히보기 흉하거나 이해하기 어렵다면 더욱 그렇습니다.

네 게이트 메서드 참조를 직접 호출 할 수 없기 때문에이 유틸리티 메서드 (또는 명시 적 캐스트)없이 술어 를 쉽게 부정 할 수 없습니다 (먼저 유형 추론이 필요함).

private static <T> Predicate<T> not(Predicate<T> predicate) {
    return predicate.negate();
}

스트림에 filterOut메서드 나 무언가 가 있으면 더 좋아 보일 것입니다.


또한 @Holger가 나에게 아이디어를 주었다. ArrayList그있다 removeAll방법은 여러 제거를 위해 최적화 된, 그것은 단지 요소 번 재 배열. 그러나 contains주어진 컬렉션에서 제공 하는 방법을 사용 하므로 listA작은 부분이 아닌 경우 해당 부분을 최적화해야합니다 .

listAlistB이전에 선언이 솔루션은 자바 8 필요하지 않습니다 그리고 그것은 매우 효율적입니다.

List<String> result = new ArrayList(listB);
result.removeAll(new HashSet<>(listA));


답변

편집 : 원래 질문은 언어를 지정하지 않았습니다. 내 대답은 C #입니다.

대신이 목적으로 HashSet을 사용해야합니다. ArrayList를 사용해야하는 경우 다음 확장 메서드를 사용할 수 있습니다.

var a = arrayListA.Cast<DateTime>();
var b = arrayListB.Cast<DateTime>();
var c = b.Except(a);

var arrayListC = new ArrayList(c.ToArray());

HashSet 사용 …

var a = new HashSet<DateTime>(); // ...and fill it
var b = new HashSet<DateTime>(); // ...and fill it
b.ExceptWith(a); // removes from b items that are in a


답변

나는 Guava Sets.difference를 사용 했습니다 .

매개 변수는 일반 컬렉션이 아니라 집합이지만 모든 컬렉션 (고유 항목 포함)에서 집합을 만드는 편리한 방법은 Guava ImmutableSet.copyOf (Iterable)입니다.

(처음에 관련 / 중복 질문에 게시 했지만 지금까지 누락 된 좋은 옵션이라고 생각하기 때문에 여기에서도 복사하고 있습니다.)


답변

이것은 Java 8에서 매우 오래된 질문이지만 다음과 같이 할 수 있습니다.

 List<String> a1 = Arrays.asList("2009-05-18", "2009-05-19", "2009-05-21");
 List<String> a2 = Arrays.asList("2009-05-18", "2009-05-18", "2009-05-19", "2009-05-19", "2009-05-20", "2009-05-21","2009-05-21", "2009-05-22");

 List<String> result = a2.stream().filter(elem -> !a1.contains(elem)).collect(Collectors.toList());


답변

나는 당신이 C #에 대해 이야기하고 있다고 생각합니다. 그렇다면 이것을 시도 할 수 있습니다.

    ArrayList CompareArrayList(ArrayList a, ArrayList b)
    {
        ArrayList output = new ArrayList();
        for (int i = 0; i < a.Count; i++)
        {
            string str = (string)a[i];
            if (!b.Contains(str))
            {
                if(!output.Contains(str)) // check for dupes
                    output.Add(str);
            }
        }
        return output;
    }