Skip to content

Instantly share code, notes, and snippets.

@pocari
Last active December 23, 2015 02:29
Show Gist options
  • Save pocari/6567572 to your computer and use it in GitHub Desktop.
Save pocari/6567572 to your computer and use it in GitHub Desktop.
/**
* tmの中で、keyが最初に重複した行数を返す
* @param tm
* @return
*/
private static String checkDuplicate3(TableMessage tm) {
//日付をキーに重複した行番号のリストを保持するMap
Map<String, List<Integer>> dup = new HashMap<String, List<Integer>>();
//5行分について、同じ日付を持つ行数をまとめる
for (int i = 0; i < 5; i++) {
List<Integer> list = dup.get(tm.getRecord(i, "key"));
if (list == null) {
list = new ArrayList<Integer>();
dup.put(tm.getRecord(i, "key"), list);
}
list.add(Integer.parseInt(tm.getRecord(i, "num")));
}
//重複した行番号のリスト中でもっとも小さい行番号が含まれる重複日付の行番号リストを返す
List<Integer> result = Collections.min(dup.values(), new Comparator<List<Integer>>() {
@Override
public int compare(List<Integer> arg0, List<Integer> arg1) {
return Collections.min(arg0) - Collections.min(arg1);
}
});
//重複しているものがあれば、それを連結して返す
if (result != null) {
return joinList(result);
}
//重複が無ければnull
return null;
}
/**
* 回数のリストから「x回目, y回目・・・」のメッセージを組み立てて返す
* @param lst
* @return
*/
private static String joinList(List<Integer> lst) {
StringBuilder sb = new StringBuilder();
for (int i : lst) {
String msg = i + "回目";
if (sb.length() == 0) {
sb.append(msg);
} else {
sb.append(", " + msg);
}
}
return sb.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment