programing

목록 변환

nasanasas 2020. 8. 24. 18:51
반응형

목록 변환 목록에


정수 목록이 있고 List<Integer>모든 정수 개체를 문자열로 변환하여 새로운 List<String>.

당연히 새를 만들고 각 정수를 List<String>호출하는 목록을 반복 할 수 String.valueOf()있지만 더 나은 (read : more automatic ) 방법 이 있는지 궁금 합니다.


내가 아는 한, 반복하고 인스턴스화하는 것이 이것을 수행하는 유일한 방법입니다. 다음과 같은 것 (다른 사람에게 도움이 될 수있는 방법을 알고 계실 것입니다) :

List<Integer> oldList = ...
/* Specify the size of the list up front to prevent resizing. */
List<String> newList = new ArrayList<String>(oldList.size()) 
for (Integer myInt : oldList) { 
  newList.add(String.valueOf(myInt)); 
}

사용 구아바 - 프로젝트에서 구글 컬렉션 , 당신은 사용할 수 transform의 방법을 나열 클래스

import com.google.common.collect.Lists;
import com.google.common.base.Functions

List<Integer> integers = Arrays.asList(1, 2, 3, 4);

List<String> strings = Lists.transform(integers, Functions.toStringFunction());

에서 List반환 된 transform것은 백업 목록 보기 입니다. 변환은 변환 된 목록에 대한 각 액세스에 적용됩니다.

그주의 Functions.toStringFunction()발생합니다 NullPointerException확실 목록이 null이 포함되지 않습니다 경우 그렇게 밖에 이용, 널 (null)에 적용 할 때.


Java 8을위한 솔루션입니다. Guava보다 약간 길지만 최소한 라이브러리를 설치할 필요는 없습니다.

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

//...

List<Integer> integers = Arrays.asList(1, 2, 3, 4);
List<String> strings = integers.stream().map(Object::toString)
                                        .collect(Collectors.toList());

당신이하고있는 일은 괜찮지 만 'Java-it-up'이 필요하다고 느끼면 TransformerApache Commonscollect 메소드사용할 수 있습니다. 예 :

public class IntegerToStringTransformer implements Transformer<Integer, String> {
   public String transform(final Integer i) {
      return (i == null ? null : i.toString());
   }
}

..그리고..

CollectionUtils.collect(
   collectionOfIntegers, 
   new IntegerToStringTransformer(), 
   newCollectionOfStrings);

String.valueOf의 소스는 다음을 보여줍니다.

public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}

그다지 중요하지는 않지만 toString을 사용합니다.


Instead of using String.valueOf I'd use .toString(); it avoids some of the auto boxing described by @johnathan.holland

The javadoc says that valueOf returns the same thing as Integer.toString().

List<Integer> oldList = ...
List<String> newList = new ArrayList<String>(oldList.size());

for (Integer myInt : oldList) { 
  newList.add(myInt.toString()); 
}

Here's a one-liner solution without cheating with a non-JDK library.

List<String> strings = Arrays.asList(list.toString().replaceAll("\\[(.*)\\]", "$1").split(", "));

Another Solution using Guava and Java 8

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<String> strings = Lists.transform(numbers, number -> String.valueOf(number));

Not core Java, and not generic-ified, but the popular Jakarta commons collections library has some useful abstractions for this sort of task. Specifically, have a look at the collect methods on

CollectionUtils

Something to consider if you are already using commons collections in your project.


To the people concerned about "boxing" in jsight's answer: there is none. String.valueOf(Object) is used here, and no unboxing to int is ever performed.

Whether you use Integer.toString() or String.valueOf(Object) depends on how you want to handle possible nulls. Do you want to throw an exception (probably), or have "null" Strings in your list (maybe). If the former, do you want to throw a NullPointerException or some other type?

Also, one small flaw in jsight's response: List is an interface, you can't use the new operator on it. I would probably use a java.util.ArrayList in this case, especially since we know up front how long the list is likely to be.


@Jonathan: I could be mistaken, but I believe that String.valueOf() in this case will call the String.valueOf(Object) function rather than getting boxed to String.valueOf(int). String.valueOf(Object) just returns "null" if it is null or calls Object.toString() if non-null, which shouldn't involve boxing (although obviously instantiating new string objects is involved).


I think using Object.toString() for any purpose other than debugging is probably a really bad idea, even though in this case the two are functionally equivalent (assuming the list has no nulls). Developers are free to change the behavior of any toString() method without any warning, including the toString() methods of any classes in the standard library.

Don't even worry about the performance problems caused by the boxing/unboxing process. If performance is critical, just use an array. If it's really critical, don't use Java. Trying to outsmart the JVM will only lead to heartache.


An answer for experts only:

    List<Integer> ints = ...;
    String all = new ArrayList<Integer>(ints).toString();
    String[] split = all.substring(1, all.length()-1).split(", ");
    List<String> strs = Arrays.asList(split);

Lambdaj allows to do that in a very simple and readable way. For example, supposing you have a list of Integer and you want to convert them in the corresponding String representation you could write something like that;

List<Integer> ints = asList(1, 2, 3, 4);
Iterator<String> stringIterator = convertIterator(ints, new Converter<Integer, String> {
    public String convert(Integer i) { return Integer.toString(i); }
}

Lambdaj applies the conversion function only while you're iterating on the result.


List<String> stringList = integerList.stream().map((Object s)->String.valueOf(s)).collect(Collectors.toList())

You can't avoid the "boxing overhead"; Java's faux generic containers can only store Objects, so your ints must be boxed into Integers. In principle it could avoid the downcast from Object to Integer (since it's pointless, because Object is good enough for both String.valueOf and Object.toString) but I don't know if the compiler is smart enough to do that. The conversion from String to Object should be more or less a no-op, so I would be disinclined to worry about that one.


Just for fun, a solution using the jsr166y fork-join framework that should in JDK7.

import java.util.concurrent.forkjoin.*;

private final ForkJoinExecutor executor = new ForkJoinPool();
...
List<Integer> ints = ...;
List<String> strs =
    ParallelArray.create(ints.size(), Integer.class, executor)
    .withMapping(new Ops.Op<Integer,String>() { public String op(Integer i) {
        return String.valueOf(i);
    }})
    .all()
    .asList();

(Disclaimer: Not compiled. Spec is not finalised. Etc.)

Unlikely to be in JDK7 is a bit of type inference and syntactical sugar to make that withMapping call less verbose:

    .withMapping(#(Integer i) String.valueOf(i))

This is such a basic thing to do I wouldn't use an external library (it will cause a dependency in your project that you probably don't need).

We have a class of static methods specifically crafted to do these sort of jobs. Because the code for this is so simple we let Hotspot do the optimization for us. This seems to be a theme in my code recently: write very simple (straightforward) code and let Hotspot do its magic. We rarely have performance issues around code like this - when a new VM version comes along you get all the extra speed benefits etc.

As much as I love Jakarta collections, they don't support Generics and use 1.4 as the LCD. I am wary of Google Collections because they are listed as Alpha support level!


I didn't see any solution which is following the principal of space complexity. If list of integers has large number of elements then it's big problem.

It will be really good to remove the integer from the List<Integer> and free
the space, once it's added to List<String>.

We can use iterator to achieve the same.

    List<Integer> oldList = new ArrayList<>();
    oldList.add(12);
    oldList.add(14);
    .......
    .......

    List<String> newList = new ArrayList<String>(oldList.size());
    Iterator<Integer> itr = oldList.iterator();
    while(itr.hasNext()){
        newList.add(itr.next().toString());
        itr.remove();
    }

Using Streams : If let say result is list of integers (List<Integer> result) then :

List<String> ids = (List<String>) result.stream().map(intNumber -> Integer.toString(intNumber)).collect(Collectors.toList());

One of the ways to solve it. Hope this helps.


I just wanted to chime in with an object oriented solution to the problem.

If you model domain objects, then the solution is in the domain objects. The domain here is a List of integers for which we want string values.

The easiest way would be to not convert the list at all.

That being said, in order to convert without converting, change the original list of Integer to List of Value, where Value looks something like this...

class Value {
    Integer value;
    public Integer getInt()
    {
       return value;
    }
    public String getString()
    {
       return String.valueOf(value);
    }
}

This will be faster and take up less memory than copying the List.

Good Luck!

참고URL : https://stackoverflow.com/questions/18524/converting-listinteger-to-liststring

반응형