Created
March 7, 2011 00:32
-
-
Save ts-3156/857907 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* すべてのArrayListに共通するユーザを見つける | |
* @param <T> | |
* @param listOfUsers | |
* @return | |
*/ | |
public static <T> ArrayList<T> getCommonUsers(List<ArrayList<T>> listOfUsers){ | |
if(listOfUsers == null) | |
return null; | |
if(listOfUsers.size() == 0) | |
return new ArrayList<T>(); | |
if(listOfUsers.size() == 1) | |
return new ArrayList<T>(listOfUsers.get(0)); | |
int minUserCount = Integer.MAX_VALUE; | |
int minUserIndex = -1; | |
for(int i = 0; i < listOfUsers.size(); i++) | |
if(listOfUsers.get(i).size() < minUserCount){ | |
minUserCount = listOfUsers.get(i).size(); | |
minUserIndex = i; | |
} | |
if(minUserIndex == -1) | |
minUserIndex = 0; | |
ArrayList<T> commonUsers = new ArrayList<T>(listOfUsers.get(minUserIndex)); | |
for(int i = 0; i < listOfUsers.size(); i++){ | |
if(i == minUserIndex) | |
continue; | |
ArrayList<T> tmpCommonUsers = getCommonUsers(commonUsers, listOfUsers.get(i)); | |
commonUsers = new ArrayList<T>(tmpCommonUsers); | |
} | |
return commonUsers; | |
} | |
/** | |
* users1とusers2に共通するユーザを見つける | |
* @param <T> | |
* @param users1 | |
* @param users2 | |
* @return | |
*/ | |
private static <T> ArrayList<T> getCommonUsers(List<T> users1, List<T> users2){ | |
ArrayList<T> commonUsers = new ArrayList<T>(); | |
for (T u1 : users1) { | |
T u = null; | |
for (T u2 : users2) { | |
if (u1.equals(u2)) { | |
u = u1; | |
break; | |
} | |
} | |
if (u != null) { | |
boolean isOvarlapped = false; | |
for (T t : commonUsers) { | |
if (u.equals(t)) { | |
isOvarlapped = true; | |
break; | |
} | |
} | |
if (!isOvarlapped) | |
commonUsers.add(u); | |
} | |
} | |
return commonUsers; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment