I have a simple HashMap, and it seems like there's no way to pass it to any standard ItemReader from Spring Batch which I find quite frustrating. Will I have to write my own Item Reader in order to process a HashMap or is there something that I'm missing ?
Well it depends on what your items are: keys, values or entries of your map.
You can use the ListItemReader and pass keys, values or entries as a list in its constructor. For example:
Map<Integer, String> items = new HashMap<>();
items.put(1, "foo");
items.put(2, "bar");
ListItemReader<Integer> keysItemReader = new ListItemReader<>(new ArrayList<>(items.keySet()));
ListItemReader<String> valuesItemReader = new ListItemReader<>(new ArrayList<>(items.values()));
ListItemReader<Map.Entry<Integer, String>> entriesItemReader = new ListItemReader<>(new ArrayList<>(items.entrySet()));
You can also use the IteratorItemReader and pass the iterator on your items to it in the constructor:
IteratorItemReader<Map.Entry<Integer, String>> iteratorItemReader = new IteratorItemReader<>(items.entrySet().iterator());
// same for keys or values
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With