Here you can find the source of equal(Collection> col1, Collection> col2)
Parameter | Description |
---|---|
col1 | collection 1 |
col2 | collection 2 |
public static boolean equal(Collection<?> col1, Collection<?> col2)
//package com.java2s; /******************************************************************************* * Copyright (c) 2006-2012//w w w . j a v a 2 s .co m * Software Technology Group, Dresden University of Technology * DevBoost GmbH, Berlin, Amtsgericht Charlottenburg, HRB 140026 * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Software Technology Group - TU Dresden, Germany; * DevBoost GmbH - Berlin, Germany * - initial API and implementation ******************************************************************************/ import java.util.Collection; public class Main { /** * Compares the given collections. * * @param col1 collection 1 * @param col2 collection 2 * @return comparison result */ public static boolean equal(Collection<?> col1, Collection<?> col2) { int check = trivialCollectionEqual(col1, col2); switch (check) { case -1: return false; case 1: return true; default: //nothing } //TODO #1469: does not work if same element is contained twice boolean contains; for (Object eObj : col1) { contains = false; for (Object aObj : col2) { if (eObj.equals(aObj)) { contains = true; break; } } if (!contains) { return false; } } return true; } private static int trivialCollectionEqual(Collection<?> expected, Collection<?> actual) { if (expected == null && actual == null) { return 1; } if (expected == null && actual != null) { return -1; } if (actual == null && expected != null) { return -1; } if (expected == null || actual == null) { return -1; } if (expected.size() != actual.size()) { return -1; } if (expected.size() == 0 && actual.size() == 0) { return 1; } return 0; } }