This post shows you how to convert String to Boolean in Java. Let's write a generic code in this post.
Create Generic Converter interface
/**
* Converts objects of S type to T type.
*/
public interface Converter<S, T> {
/**
* Converts the source object from S type to T type.
*
* @param source the object to convert
*
* @return the converted object
*/
T convert(S source);
}
StringToBooleanConverter
Let's implement above Converter interface to convert String to boolean in Java
/**
* Converts String to Boolean.
*/
public class StringToBooleanConverter implements Converter<String, Boolean> {
@Override
public Boolean convert(String source) {
return Boolean.valueOf(source);
}
}
Free Spring Boot Tutorial - 5 Hours Full Course
Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course