简体   繁体   中英

Converting Set<byte[]> to List<String>

I'm using redis in memory database in my application. We are adding to a set some string values using the sAdd method and fetching the values from set using the smembers method. This method is returning a Set<byte[]>. I need to convert this set to a list of String. I tried the below way but it is not working.

List<String> list = Arrays.asList((servKeys)).toArray();

You can use Base64 encoder with Stream API:

List<String> list = servKeys.stream()
    .map(bytes -> Base64.getEncoder().encodeToString(bytes))
    .collect(Collectors.toList());

Alternatively to Base64 , you can use new String(bytes, StandardCharsets.UTF_8) .

This depends on what kind of data byte[] is. The code above assumes a plain text, however, you can easily adjust the behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM