Java tutorial
//package com.java2s; //License from project: Apache License import java.util.List; import java.util.Locale; public class Main { /** * Compares two lists. Caution: Lists can hold same values multiple times. * * @param list1 * @param list2 * @return -1 if no common elements or any of lists are empty/null. * 0 if two lists have the same contents. * x (x>0) if there are x elements in common. */ public static int compareLists(List<? extends Object> list1, List<? extends Object> list2) { if (null == list1 || null == list2 || list1.isEmpty() || list2.isEmpty()) { return -1; } int count = 0; for (Object o : list1) { if (list2.contains(o)) count++; } return count; } /** * Check if the list contains the member. For objects, it uses standard .equals() method. * For strings, it compares but ignores the case. * * @param list * @param member * @return */ public static boolean contains(List<? extends Object> list, Object member) { if (list.isEmpty() || member == null) return false; for (Object obj : list) { if (obj instanceof String && member instanceof String) { if (compareIgnoreCase((String) obj, (String) member)) return true; } else { if (member.equals(obj)) return true; } } return false; } public static boolean compareIgnoreCase(String str1, String str2) { return str1.toLowerCase(Locale.ENGLISH).equals(str2.toLowerCase(Locale.ENGLISH)); } }