1. ArrayList.toString()
ArrayList<String> testArrayList = new ArrayList<>();
testArrayList.add("Json");
testArrayList.add("Test");
testArrayList.add("ArrayListToString");
String strToString = testArrayList.toString();
System.out.println("convertArrayListToString: strToString = " + strToString);
결과 : 변환 실패
결과
convertArrayListToString: strToString = [Json, Test, ArrayListToString]
2. StringBuilder append()
ArrayList<String> testArrayList = new ArrayList<>();
testArrayList.add("Json");
testArrayList.add("Test");
testArrayList.add("ArrayListToString");
StringBuilder stringBuilder = new StringBuilder();
for (String item : testArrayList) {
stringBuilder.append(item);
}
String strAppend = stringBuilder.toString();
System.out.println("convertArrayListToString: strAppend = " + strAppend);
결과 : 성공
결과
convertArrayListToString: strAppend = JsonTestArrayListToString
3. String.join()
ArrayList<String> testArrayList = new ArrayList<>();
testArrayList.add("Json");
testArrayList.add("Test");
testArrayList.add("ArrayListToString");
String strJoin = String.join("", testArrayList);
System.out.println("convertArrayListToString: strJoin = " + strJoin);
결과 : 성공
결과
convertArrayListToString: strJoin = JsonTestArrayListToString
전체 소스
import java.text.SimpleDateFormat;
import java.util.ArrayList;
class ArrayListToString {
public static void main(String[] args) {
ArrayList<String> testArrayList = new ArrayList<>();
testArrayList.add("Json");
testArrayList.add("Test");
testArrayList.add("ArrayListToString");
convertArrayListToString(testArrayList);
convertArrayListToStringAppend(testArrayList);
convertArrayListToStringJoin(testArrayList);
}
static void convertArrayListToString(ArrayList<String> strings) {
String strToString = strings.toString();
System.out.println("convertArrayListToString: strToString = " + strToString);
}
static void convertArrayListToStringAppend(ArrayList<String> strings) {
StringBuilder stringBuilder = new StringBuilder();
for (String item : strings) {
stringBuilder.append(item);
}
String strAppend = stringBuilder.toString();
System.out.println("convertArrayListToString: strAppend = " + strAppend);
}
static void convertArrayListToStringJoin(ArrayList<String> strings) {
String strJoin = String.join("", strings);
System.out.println("convertArrayListToString: strJoin = " + strJoin);
}
}
결과
convertArrayListToString: strToString = [Json, Test, ArrayListToString]
convertArrayListToString: strAppend = JsonTestArrayListToString
convertArrayListToString: strJoin = JsonTestArrayListToString
성능 테스트
1. StringBuilder append()
- 100000 반복 : 45ms
- 10000000 반복 : 1423ms
2. String.join()
- 100000 반복 : 47ms
- 10000000 반복 : 1415ms
* H/W 성능에 따라 결과를 다를 수 있지만 속도 = append() >= join() 결과는 변하지 않을 것으로 예상.
성능 결론
String.join() 코드는 아래와 같음.
결국 append() 코드와 동일한 코드임.
public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) {
Objects.requireNonNull(delimiter);
Objects.requireNonNull(elements);
StringJoiner joiner = new StringJoiner(delimiter);
for (CharSequence cs: elements) {
joiner.add(cs);
}
return joiner.toString();
}
반응형