I have these two methods. Can someone help me to refactor and make them common?
public void method1(Map<String,String> map, String key, String value){
map.put(key, value);
}
public void method2(GenericRecord recordMap, String key, String value){
recordMap.put(key, value);
}
Unable to refactor it.
GenericRecordimplementMap. Otherwise, you can't. Java has no duck typing.KeyValueConsumer<K, V>with a methodvoid consume(K key, V value)and then require aKeyValueConsumer<String, String>as parameter, that can be passed along as a lambda.put, and you just shifted that responsibility to the caller.