Search code examples
javacollectionsjava-8consumer

Write Map in File with Consumer in Java 8


I want to write a class MapWriter<T> that Implements a Consumer of Map<T,Integer> that writes the input map into a text file. I have a final attribute outputFilePathStr.

@AllArgsConstructor
public class MapWriter<T> implements Consumer<Map<T,Integer>>{

    private final String outputFilePathStr;

    @Override
    public void accept(Map<T, Integer> tIntegerMap) {
        Files.write(outputFilePathStr, tIntegerMap);
    }
}

I have an error in Files.write(outputFilePathStr, tIntegerMap), i can't compile it.

enter image description here P.S: The annotation @AllArgsConstructor is from Lombok plugin, it generates automatically all the constructors.


Solution

  • I resolved it. i changed this:

     Files.write(outputFilePathStr, tIntegerMap.toString().getBytes())