Brief introduction: The Lists. Transform provided by Guava is very powerful, but problems have been encountered in the recent use of the project. There are still pits, so this paper first briefly introduces Lists. Later, we will take a look at the implementation principle of Lists. Transform based on possible problems in the actual use process. If there are any mistakes, welcome to comment and discuss.
## 1: The use of Lists. Transform
Dao query database returns a Result set list, in which the Result object contains Id, nameStr, MSG and other fields, but some interface parameters of the upper layer business may only need the Result set list of Id. Of course, we can use the for each loop list and then take out the IDS and add them to the list one by one. Lists. Transform is a convenient way to solve this process, and it can easily transform a list of one type into a list of another type. The usage is as follows:
public void listToList(){// source list list <Result> listResults = Lists. NewArrayList (new Result(1,"test1"),new Result(2,"test2"),new Result(3,"test3")); List list <String> strLists = Lists. Transform (listResults,new Function<Result,String>(){@override public String apply(Result result){returnresult.getNameStr(); }}); }Copy the code
## 2 :Lists. Transform See the following code, only in the above example added a line to change the properties of the object in the listResults operation and print:
@Test
public void listToList(){// source list list <Result> listResults = Lists. NewArrayList (new Result(1,"test1"),new Result(2,"test2"),new Result(3,"test3")); List list <String> strLists = Lists. Transform (listResults,new Function<Result,String>(){@override public String apply(Result result){returnresult.getNameStr(); }}); // After conversion, the target list prints system.out.println ("strLists 1 values:");
for(String str:strLists){
System.out.println(str+";"); } // Modify the source listfor(Result result:listResults){
result.setNameStr("reset"); } // Print the target list system.out.println ("strLists 2 values:");
for(String str:strLists){
System.out.println(str+";"); }}Copy the code
What is the output? If we first used Lists. Transform, we would think that the output should be the same, but this is not the case. The converted list will be changed by changes to the source list.
strLists 1 values:
test1;
test2;
test3;
strLists 2 values:
reset;
reset;
reset;
Copy the code
This is a big problem if you don’t understand Lists. Transform, which will cause serious problems. Let’s look at how Lists.
public static <F, T> List<T> transform(List<F> fromList, Function<? super F, ? extends T> function) {
return (List)(fromList instanceof RandomAccess ? new Lists.TransformingRandomAccessList(fromList, function) : new Lists.TransformingSequentialList(fromList, function));
}
Copy the code
You can see the code above write very clear, and Lists. The transform the returned is a newly created TransformingSequentialList instance, then we could then look down
public ListIterator<T> listIterator(int index) {
return new TransformedListIterator<F, T>(this.fromList.listIterator(index)) {
T transform(F from) {
returnTransformingSequentialList.this.function.apply(from); }}; }Copy the code
TransformingSequentialList each traversal traversal in again from the original list to calculate the function # # using the new open source libraries are summarized up finally to do more to understand, look at the source before using, so it can reasonable use scenarios at the right moment, If you don’t know it’s best to code it in a safer way.