[JAVA] How to connect the strings in the List separated by commas

About this article

There are times when the value returned from DAO is in the List and I want to concatenate the values and retrieve them. This time I will leave a memorandum about how to do it in such a case.

Note) If you use the second method as it is, a warning will appear at compile time, so correct it. Please use it.

For the code itself, refer to the link below. [Java] Make the string array a comma-separated string

To try it as it is ...

Issue the following command at the command prompt

cd ListTest.Directory with java
javac ListTest.java
java ListTest 

<--- Execution result ----------------------------> Type of first list class java.lang.String The contents of the first list. Comma connection: a, b Second list type class java.lang.String The contents of the second list. Comma connection: c, d <--------------------------------------->

code#

ListTest.java


import java.util.*;

public class ListTest{

	 /*
	 *A program that tests how to write a List
	 *reference
	 * http://d.hatena.ne.jp/mtoyoshi/20080717/1216299220
	 *
	 * @Disable compile warnings in SuppressWarnings
	 */
	 @SuppressWarnings("unchecked")
     public static void main(String[] args){

     	/*The first one
     	*
     	*/
     	List<String> lst = new ArrayList<String>();

     	lst.add("a");
     	lst.add("b");
    	
     	StringBuilder builder = new StringBuilder();
     	
     	//Type confirmation
     	System.out.println("Type of first list" + lst.get(0).getClass());
    	
     	for(String str : lst) {
			builder.append(str).append(",");
		}

		String result = builder.substring(0, builder.length() - 1);
        System.out.println("The contents of the first list. Comma connection:" + result);

     	
     	/*Second
     	* List<String> lst2 = new ArrayList<String>();Otherwise you will get a compile-time warning
     	*/
     	List lst2 = new ArrayList();
     	

     	lst2.add("c");
     	lst2.add("d");
    	
     	StringBuilder builder2 = new StringBuilder();
		
     	//Type confirmation
     	System.out.println("Second list type" + lst2.get(0).getClass());
     	
		for (int i = 0; i < lst2.size(); i++) {                        
            String str2  = (String)lst2.get(i);
			builder2.append(str2).append(",");
        }

		String result2 = builder2.substring(0, builder2.length() - 1);
        System.out.println("The contents of the second list. Comma connection:" + result2);
     	
     }
}

Recommended Posts

How to connect the strings in the List separated by commas
How to concatenate strings separated by commas using StringJoiner even in java7 environment
Several ways to concatenate strings separated by commas in Java
[Rails] How to display the list of posts by category
How to concatenate strings in java
How to display products by category on the same list screen
How to output a list of strings in JSF as comma-separated strings
How to sort the List of SelectItem
How to get the date in java
[Java] How to receive numerical data separated by spaces in standard input
[Rails] How to decide the destination by "rails routes"
How to check the logs in the Docker container
How to add sound in the app (swift)
How to separate .scss by controller in Rails
How to build the simplest blockchain in Ruby
How to check Rails commands in the terminal
[Rails6] How to connect the posting function generated by Scaffold with the user function generated by devise
[Java] Various methods to acquire the value stored in List by iterative processing
How to concatenate strings
How to delete / update the list field of OneToMany
How to set the display time to Japan time in Rails
How to return to the previous screen by Swipe operation
How to remove the underline displayed by Rails link_to
How to move to the details screen by clicking the image
Organized how to interact with the JDK in stages
How to specify the resource path in HTML import
How to debug the generated jar file in Eclipse
[Rails] How to display an image in the view
[Java] How to get the key and value stored in Map by iterative processing
How to save a file with the specified extension under the directory specified in Java to the list
[Java] How to display the day of the week acquired by LocalDate, DateTimeformatter in Japanese
How to get the class name / method name running in Java
How to correctly check the local HTML file in the browser
How to use the getter / setter method (in object orientation)
How to dynamically change the column name acquired by MyBatis
How to set chrony when the time shifts in CentOS7
How to create a placeholder part to use in the IN clause
How to retrieve the hash value in an array in Ruby
How to select a specified date by code in FSCalendar
How to add the same Indexes in a nested array
How to derive the last day of the month in Java
Differences in how to handle strings between Java and Perl
How to disable existing selected items in the select box
How to switch Java in the OpenJDK era on Mac
[Rails] How to display information stored in the database in view
[Ruby] How to batch convert strings in an array to numbers
[Java] How to search for a value in an array (or list) with the contains method
[Rails / Routing] How to refer to the controller in the directory you created
How to output the value when there is an array in the array
How to write ruby if in one line Summary by beginner
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
How to get the id of PRIMAY KEY auto_incremented in MyBatis
How to install the language used in Ubuntu and how to build the environment
[Behavior confirmed in December 2020] How to implement the alert display function
Cast an array of Strings to a List of Integers in Java
How to store the information entered in textarea in a variable in the method
Determine if the strings to be compared are the same in Java
How to solve the unknown error when using slf4j in Java
[Rails] How to reset the database in production environment (Capistrano version)
Measure the bottleneck! How to trace only slow methods in AspectJ
How to call multiple names at once in the same category