If you don't want to check each value, I would recommend java object converting into JSON, like into Map<String,Object>
and then use Collectors.joining
String value = map.values()
.stream().filter(Objects::nonNull)
.map(Object::toString)
.collect(Collectors.joining("|"));
You can do it using the reflection package, but make sure your fields are public
Person per = new Person(1, "Kevin", "20", "Male");
String value = Arrays.stream(Person.getClass().getFields())
.map(f-> {
try {
return f.get(per).toString();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}).filter(Objects::nonNull).collect(Collectors.joining("|"));
System.out.println(value); //1|Kevin|20|Male
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…