How to easily copy one type of list into another type?
Say I want to copy all elements of list A into list 开发者_运维知识库B. But the elements in list A are of type ExampleClass and elements in list B are String.
ExampleClass {
String a;
}
Since ExampleClass has an attribute a which is a String, when converting from ExampleClass to String I want the value of a to be returned as the String that represents ExampleClass. Would toString() do this? What's the most efficient way of doing this without having to loop through all of list A, convert each ExampleClass into String, and adding them to list B?
You have to loop through the list, convert each element and add it to the other list. There is no other way. Even if you use some library to do this, it would still need to do exactly that.
As for toString()
being meaningful in this situation, that totally depends on your class. At the very least, ExampleClass needs to implement toString()
, because the default implementation looks like "ExampleClass@abc564", which is probably not useful to you at all.
It seems that you want to use the value of its field a
.
So, to summarize
for (ExampleClass e: listA){
listB.add(e.toString());
}
// and in ExampleClass
public String toString(){
return a;
}
Note: if you override toString()
, this will be used everywhere the class is printed, which may or may not be a problem. For the purpose of the conversion loop, you might just as well have a getA()
method and use that one instead of toString()
.
Your question is about efficiency...so I'll answer that.
In order to copy one list to another list, fundamentally, there is no way to do that than to loop through all elements at some level.
Now, you would be able to reduce how much memory is allocated and moves around if you only want to copy the String as a reference. But someone at some level is going to loop through N elements, so it can just as easily be you.
I've used the TransformedList class from Apache Commons in the past. It's doesn't make a copy of the list, but rather decorates a list with a transforming function. In your case, your Transformer would simply return the value of a
from an object.
Give it a shot if you find that you don't need an actual copy of the list.
Note that to answer one of your latter questions - toString()
will not print anything about the fields within your class unless you override that method and tell it to do so. If you don't override it, the output is defined by Object's toString() method (the exact output of which is not specified by the API other than being "a textual representation", but in Sun JREs is the fully qualified classname combined with the identity hashcode of the object, itself derived from the memory address used by that object).
In general I don't like relying on a specific value of toString() for a custom class, as it's not really well defined what the value ought to be, but I certainly feel it should be human-readable. I'd much prefer an explicit getA()
method to use in this case.
精彩评论