简体   繁体   中英

Add elements to a list and separate each element by a colon “:” instead of a comma

I have the below three elements :

play_full_NAME=556677

pause_full_NAME=9922

stop_full_NAME=112233

A string "abc" returns all the above three elements one by one from a particular piece of code.

I am trying to add all three elements in a list separated by a colon ":"

Sample output :

play_full_NAME=556677:pause_full_NAME=9922:stop_full_NAME=112233

My attempt:

List<String> list = new ArrayList<String>();

list.join(":",abc)

Please help with a better way to handle this.

Your understanding about List is little flawed. Comma is only printed for representation purposes.

To join strings with colon, you can do the following

List<String> list = Arrays.asList("play_full_NAME=556677", 
                                  "pause_full_NAME=9922", 
                                  "stop_full_NAME=112233");

String joinedString = String.join(":", list);

Did you really understood the List well?

In fact, there is no separator, each item / value is stored as different "object".

So you have some amount of independent values- Strings in this case, what can you see on screenshot bellow, or if you will do System.out.println(someList); it will call override of method toString() which is inherited from Object class , which is root parent class of all classes in Java.

在此处输入图片说明

So its absolutely nonsense to add some split character between each items in List , they are split already, you can access each item by get(int position) method.

So if you want to print each item of list "by yourself", can be done like follows:

for (int i = 0; i < someList.size(); i++) {
    System.out.println(i + " = " + someList.get(i));    
}
/* output will be 
   1 = 1 item
   2 = 2 item
   3 = 3 item
   4 = 4 item
*/

If you want to implement custom method for printing "your list" then you can extend eg. ArrayList class and override toString method, but better and more trivial approach will be prepare some method in some utils to get formatted String output with context of List- eg. (notice there will be ; after last element)

public static String getFormatStringFromList(ArrayList<String> data) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < data.size(); i++) {
        sb.append(data.get(i) + ";");   
    }

    return sb.toString();
    //eg. 0 item;1 item;2 item;3 item;4 item;
}

To avoid last separator you can do eg. simple check

public static String getFormatStringFromListWitoutLastSeparator(List<String> someList) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < someList.size(); i++) {
            sb.append(someList.get(i));
            if(i < someList.size() -1) {
                 sb.append(";");
            }
        }

        return sb.toString();
        //0 item;1 item;2 item;3 item;4 item
        /*
           someList[0] = 0 item
           someList[1] = ;
           someList[2] = 1 item
           someList[3] = ;
           {etc..}
        */
    }

The best approach to get String from list will be like @krisnik advised:

String joinedString = String.join(":", list);

You would need to add the colon elements separately if you want them to be within your list.

For example:

List<String> list = new ArrayList<String>();

list.add(abc);
list.add(":");
list.add(def);
list.add(":");

and so on.

I would recommend against this, however, as you can simply format the output string using String.format or a StringBuilder when you need it.

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