Using Java 8 to convert a list of objects into a string obtained from the toString() method

前端 未结 13 1723
太阳男子
太阳男子 2020-11-28 02:02

There are a lot of useful new things in Java 8. E.g., I can iterate with a stream over a list of objects and then sum the values from a specific field of the Object

相关标签:
13条回答
  • 2020-11-28 02:27

    Testing both approaches suggested in Shail016 and bpedroso answer (https://stackoverflow.com/a/24883180/2832140), the simple StringBuilder + append(String) within a for loop, seems to execute much faster than list.stream().map([...].

    Example: This code walks through a Map<Long, List<Long>> builds a json string, using list.stream().map([...]:

    if (mapSize > 0) {
        StringBuilder sb = new StringBuilder("[");
    
        for (Map.Entry<Long, List<Long>> entry : threadsMap.entrySet()) {
    
            sb.append("{\"" + entry.getKey().toString() + "\":[");
            sb.append(entry.getValue().stream().map(Object::toString).collect(Collectors.joining(",")));
        }
        sb.delete(sb.length()-2, sb.length());
        sb.append("]");
        System.out.println(sb.toString());
    }
    

    On my dev VM, junit usually takes between 0.35 and 1.2 seconds to execute the test. While, using this following code, it takes between 0.15 and 0.33 seconds:

    if (mapSize > 0) {
        StringBuilder sb = new StringBuilder("[");
    
        for (Map.Entry<Long, List<Long>> entry : threadsMap.entrySet()) {
    
            sb.append("{\"" + entry.getKey().toString() + "\":[");
    
            for (Long tid : entry.getValue()) {
                sb.append(tid.toString() + ", ");
            }
            sb.delete(sb.length()-2, sb.length());
            sb.append("]}, ");
        }
        sb.delete(sb.length()-2, sb.length());
        sb.append("]");
        System.out.println(sb.toString());
    }
    
    0 讨论(0)
  • 2020-11-28 02:27

    I'm going to use the streams api to convert a stream of integers into a single string. The problem with some of the provided answers is that they produce a O(n^2) runtime because of String building. A better solution is to use a StringBuilder, and then join the strings together as the final step.

    //              Create a stream of integers 
        String result = Arrays.stream(new int[]{1,2,3,4,5,6 })                
                // collect into a single StringBuilder
                .collect(StringBuilder::new, // supplier function
                        // accumulator - converts cur integer into a string and appends it to the string builder
                        (builder, cur) -> builder.append(Integer.toString(cur)),
                        // combiner - combines two string builders if running in parallel
                        StringBuilder::append) 
                // convert StringBuilder into a single string
                .toString();
    

    You can take this process a step further by converting the collection of object to a single string.

    // Start with a class definition
    public static class AClass {
        private int value;
        public int getValue() { return value; }
        public AClass(int value) { this.value = value; }
    
        @Override
        public String toString() {
            return Integer.toString(value);
        }
    }
    
    // Create a stream of AClass objects
            String resultTwo = Arrays.stream(new AClass[]{
                    new AClass(1),
                    new AClass(2),
                    new AClass(3),
                    new AClass(4)
            })
                    // transform stream of objects into a single string
                    .collect(StringBuilder::new,
                            (builder, curObj) -> builder.append(curObj.toString()),
                            StringBuilder::append
                    )
                // finally transform string builder into a single string
                .toString();
    
    0 讨论(0)
  • 2020-11-28 02:34

    Also, you can do like this.

        List<String> list = Arrays.asList("One", "Two", "Three");
        String result = String.join(", ", list);
        System.out.println(result);
    
    0 讨论(0)
  • 2020-11-28 02:38

    Just in case anyone is trying to do this without java 8, there is a pretty good trick. List.toString() already returns a collection that looks like this:

    [1,2,3]

    Depending on your specific requirements, this can be post-processed to whatever you want as long as your list items don't contain [] or , .

    For instance:

    list.toString().replace("[","").replace("]","") 
    

    or if your data might contain square brackets this:

    String s=list.toString();
    s = s.substring(1,s.length()-1) 
    

    will get you a pretty reasonable output.

    One array item on each line can be created like this:

    list.toString().replace("[","").replace("]","").replaceAll(",","\r\n")
    

    I used this technique to make html tooltips from a list in a small app, with something like:

    list.toString().replace("[","<html>").replace("]","</html>").replaceAll(",","<br>")
    

    If you have an array then start with Arrays.asList(list).toString() instead

    I'll totally own the fact that this is not optimal, but it's not as inefficient as you might think and is pretty straightforward to read and understand. It is, however, quite inflexible--in particular don't try to separate the elements with replaceAll if your data might contain commas and use the substring version if you have square brackets in your data, but for an array of numbers it's pretty much perfect.

    0 讨论(0)
  • 2020-11-28 02:45

    With Java 8+

    String s = Arrays.toString(list.stream().toArray(AClass[]::new));
    

    Not the most efficient, but it is a solution with a small amount of code.

    0 讨论(0)
  • 2020-11-28 02:47

    There is a collector joining in the API. It's a static method in Collectors.

    list.stream().map(Object::toString).collect(Collectors.joining(","))
    

    Not perfect because of the necessary call of toString, but works. Different delimiters are possible.

    0 讨论(0)
提交回复
热议问题