Here you can find the source of content_equality(Collection
Parameter | Description |
---|---|
N | the shared type of the collections, supporting a working hashCode method |
A | a parameter |
B | a parameter |
public static <N> boolean content_equality(Collection<N> A, Collection<N> B)
//package com.java2s; //License from project: Open Source License import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.Set; public class Main { /**/*from www. ja v a 2 s .c om*/ * this can be an expensive operation, intended for testing not operating code * @param <N> the shared type of the collections, supporting a working hashCode method * @param A * @param B * @return whether A and B have the same elements and element frequencies. */ public static <N> boolean content_equality(Collection<N> A, Collection<N> B) { if (A == B) return true; if (A == null || B == null) return false; if (A.size() != B.size()) return false; //find the elements from both collections Set<N> elements = new HashSet<N>(); elements.addAll(A); elements.addAll(B); //check the element frequencies are the same in both collections, any disparity = fail for (N element : elements) { int a_count = Collections.frequency(A, element); int b_count = Collections.frequency(B, element); if (a_count != b_count) { return false; } } return true; } }