回答編集履歴
4
getOtEmpty メソッド追加
test
CHANGED
@@ -55,20 +55,19 @@
|
|
55
55
|
ですのでジェネリクスを用いることで型の縛りを無くすことが出来、またタイピング量を減らすことにもなります。(main は上のままで使えます。)
|
56
56
|
list の比較処理内で map の比較処理を呼ぶ形にすることで、いつ何に対して処理を行うのかが明白になります。
|
57
57
|
```java
|
58
|
-
//List<Map> の diff
|
59
58
|
private static <K,V> List<Map<K,List<V>>> diff(List<Map<K,V>> list1, List<Map<K,V>> list2) {
|
60
59
|
List<Map<K,List<V>>> result = new ArrayList<>();
|
61
60
|
|
62
61
|
for(int i=0; i<Math.max(list1.size(), list2.size()); i++) {
|
63
|
-
Map<K,V> map1 = i < list1.size() ? list1.get(i) : Collections.emptyMap();
|
64
|
-
Map<K,V> map2 = i < list2.size() ? list2.get(i) : Collections.emptyMap();
|
65
|
-
|
66
|
-
Map<K,List<V>> map = diff(m
|
62
|
+
Map<K,List<V>> map = diff(getOrEmpty(list1,i), getOrEmpty(list2,i));
|
67
|
-
|
68
63
|
if(!map.isEmpty()) result.add(map);
|
69
64
|
}
|
70
65
|
|
71
66
|
return result;
|
67
|
+
}
|
68
|
+
//list の i 番目の map を返す. i 番目が無い場合は空の map を返す.
|
69
|
+
private static <K,V> Map<K,V> getOrEmpty(List<Map<K,V>> list, int i) {
|
70
|
+
return i < list.size() ? list.get(i) : Collections.emptyMap();
|
72
71
|
}
|
73
72
|
//Map の diff
|
74
73
|
private static <K,V> Map<K,List<V>> diff(Map<K,V> map1, Map<K,V> map2) {
|
3
コード修正
test
CHANGED
@@ -50,3 +50,41 @@
|
|
50
50
|
```
|
51
51
|
[{キー②=[あああ, かかか]}, {キー②=[ううう, くくく]}]
|
52
52
|
```
|
53
|
+
---
|
54
|
+
よく見ると、差分を求める処理ではマップ内のキーや値は String であるかどうかは関係ありません。
|
55
|
+
ですのでジェネリクスを用いることで型の縛りを無くすことが出来、またタイピング量を減らすことにもなります。(main は上のままで使えます。)
|
56
|
+
list の比較処理内で map の比較処理を呼ぶ形にすることで、いつ何に対して処理を行うのかが明白になります。
|
57
|
+
```java
|
58
|
+
//List<Map> の diff
|
59
|
+
private static <K,V> List<Map<K,List<V>>> diff(List<Map<K,V>> list1, List<Map<K,V>> list2) {
|
60
|
+
List<Map<K,List<V>>> result = new ArrayList<>();
|
61
|
+
|
62
|
+
for(int i=0; i<Math.max(list1.size(), list2.size()); i++) {
|
63
|
+
Map<K,V> map1 = i < list1.size() ? list1.get(i) : Collections.emptyMap();
|
64
|
+
Map<K,V> map2 = i < list2.size() ? list2.get(i) : Collections.emptyMap();
|
65
|
+
|
66
|
+
Map<K,List<V>> map = diff(map1, map2);
|
67
|
+
|
68
|
+
if(!map.isEmpty()) result.add(map);
|
69
|
+
}
|
70
|
+
|
71
|
+
return result;
|
72
|
+
}
|
73
|
+
//Map の diff
|
74
|
+
private static <K,V> Map<K,List<V>> diff(Map<K,V> map1, Map<K,V> map2) {
|
75
|
+
Map<K,List<V>> result = new HashMap<>();
|
76
|
+
|
77
|
+
Set<K> keys = new HashSet<>(map1.keySet());
|
78
|
+
keys.addAll(map2.keySet()); //両方の全てのキー
|
79
|
+
|
80
|
+
for(K key : keys) {
|
81
|
+
if(Objects.equals(map1.get(key), map2.get(key))) continue;
|
82
|
+
|
83
|
+
List<V> list = new ArrayList<>();
|
84
|
+
if(map1.containsKey(key)) list.add(map1.get(key));
|
85
|
+
if(map2.containsKey(key)) list.add(map2.get(key));
|
86
|
+
result.put(key, list);
|
87
|
+
}
|
88
|
+
return result;
|
89
|
+
}
|
90
|
+
```
|
2
list1 と list2 のサイズが異なる場合に対応
test
CHANGED
@@ -23,9 +23,9 @@
|
|
23
23
|
private static List<Map<String,List<String>>> diff(List<Map<String,String>> list1, List<Map<String,String>> list2){
|
24
24
|
List<Map<String,List<String>>> result = new ArrayList<>();
|
25
25
|
|
26
|
-
for(int i=0; i<list1.size(); i++) {
|
26
|
+
for(int i=0; i<Math.max(list1.size(), list2.size()); i++) {
|
27
|
-
Map<String,String> map1 = list1.get(i);
|
27
|
+
Map<String,String> map1 = i < list1.size() ? list1.get(i) : Collections.emptyMap();
|
28
|
-
Map<String,String> map2 = list2.get(i);
|
28
|
+
Map<String,String> map2 = i < list2.size() ? list2.get(i) : Collections.emptyMap();
|
29
29
|
|
30
30
|
Set<String> keys = new HashSet<>(map1.keySet());
|
31
31
|
keys.addAll(map2.keySet()); //両方の全てのキー
|
1
値比較を簡略化
test
CHANGED
@@ -32,7 +32,7 @@
|
|
32
32
|
|
33
33
|
Map<String,List<String>> map = new HashMap<>();
|
34
34
|
for(String key : keys) {
|
35
|
-
if(!
|
35
|
+
if(!Objects.equals(map1.get(key), map2.get(key))) {
|
36
36
|
List<String> list = new ArrayList<>();
|
37
37
|
if(map1.containsKey(key)) list.add(map1.get(key));
|
38
38
|
if(map2.containsKey(key)) list.add(map2.get(key));
|