Skip to content

Instantly share code, notes, and snippets.

@need4spd
Last active August 29, 2015 14:07
Show Gist options
  • Save need4spd/d1f84f5deaafb9481bed to your computer and use it in GitHub Desktop.
Save need4spd/d1f84f5deaafb9481bed to your computer and use it in GitHub Desktop.
List Test
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import lombok.Data;
import org.junit.Before;
import org.junit.Test;
import javax.annotation.Nullable;
import java.util.List;
public class ListTest {
@Data
private class TestDTO {
private String a;
public TestDTO(String a) {
this.a = a;
}
}
@Data
private class TestToDTO {
private String a;
public TestToDTO(String a) {
this.a = a;
}
}
private List<TestDTO> testList = Lists.newArrayList();
@Before
public void init() {
TestDTO t1 = new TestDTO("1");
TestDTO t2 = new TestDTO("2");
TestDTO t3 = new TestDTO("3");
testList.add(t1);
testList.add(t2);
testList.add(t3);
}
@Test
public void transform() {
List<TestToDTO> transformed = Lists.transform(testList, new Function<TestDTO, TestToDTO>() {
@Nullable
@Override
public TestToDTO apply(@Nullable TestDTO input) {
TestToDTO testToDTO = new TestToDTO(input.getA());
System.out.print("DDDDD");
return testToDTO;
}
});
for (TestToDTO t : transformed) {
t.setA(t.getA() + "11");
}
for (TestToDTO t : transformed) {
System.out.println(t.getA());
}
//1,2,3
}
@Test
public void iterable() {
List<TestToDTO> transformed = Lists.newArrayList(Iterables.transform(testList, new Function<TestDTO, TestToDTO>() {
@Nullable
@Override
public TestToDTO apply(@Nullable TestDTO input) {
TestToDTO testToDTO = new TestToDTO(input.getA());
return testToDTO;
}
}));
for (TestToDTO t : transformed) {
t.setA(t.getA() + "11");
}
for (TestToDTO t : transformed) {
System.out.println(t.getA());
}
//111,211,311
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment