这有点复杂,但绝对有可能:
public class Main {
public static void main(String[] args) {
MyObject<String, Integer> myObject1 = new MyObject<>("A", 1);
MyObject<String, Integer> myObject2 = new MyObject<>("B", 2);
MyObject<String, Integer> myObject3 = new MyObject<>("C", 3);
List<MyObject<String, Integer>> myObjects = Arrays.asList(myObject1, myObject2, myObject3);
assertThat(myObjects).extracting("myMap", Map.class).is(containingKey("A"), atIndex(0))
.is(containingKey("B"), atIndex(1))
.is(containingKey("C"), atIndex(2));
}
private static class MapContainsKeyCondition<K> extends Condition<Map> {
private final K keyToContain;
public MapContainsKeyCondition(K key) {
this.keyToContain = key;
}
@Override
public boolean matches(Map map) {
return map.containsKey(keyToContain);
}
}
private static <K> Condition<Map> containingKey(K key) {
return new MapContainsKeyCondition<>(key);
}
public static class MyObject<K, V> {
final Map<K, V> myMap;
public MyObject(K key, V value) {
this.myMap = Collections.singletonMap(key, value);
}
}
}