Guava의 Iterable 중, Iterable.transform()이 많이 사용된다.


http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Iterables.html



List<Member>의 name을 List<String>을 얻는 방법이다. 


예제

import java.util.List;


import org.junit.Test;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

public class IterableTest {

  @Test
  public void test() {

ImmutableList<Member>
member = new ImmutableList.Builder<Member>()
        .add(
new Member("Samuel", 1))
        .add(
new Member("Chacha", 2))
        .add(
new Member("Jason", 3))
        .add(
new Member("Kalley", 4))
        .build();

         List<String>
result = Lists.transform(member, new Function<Member, String>() {
             
@Override
public String apply(Member person) {
                 
return person.name;
             }
         });

          System.
out.println(result);
  }
}


class Member {
  String
name;
 
int id;
 
public Member (String name, int id) {
 
this.name = name;
 
this.id = id;
  }
}


결과

[Samuel, Chacha, Jason, Kalley]



두번째로 잘 사용하는 메소드는 Iterables.removeIf()으로, 자주 사용하는 편이다. collection에서 특정 조건(predicate)에 걸리면 삭제하도록 한다. 


import java.util.List;

import org.junit.Test;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

public class IterableTest {

@Test
public void test() {
List<String>
numbers = Lists.newArrayList();
numbers.add("1111");
numbers.add("2222");
numbers.add("3333");
numbers.add("0000");

// 0으로 나타나는 String은 제외.
boolean result = Iterables.removeIf(numbers, new Predicate<String>() {
@Override
public boolean apply(String input) {
int inputNumber = Integer.parseInt(input);
if (inputNumber == 0) {
return true;
}
return false;
}

});

    System.
out.println(result);
    System.
out.println(numbers);
}
}


결과

true

[1111, 2222, 3333]




참고로, transform() 메소드를 포함하는 Guava 클래스는 Lists와 Collections이다. 특이하게 Sets는 없다. (만들어질 때부터 의도와 관련된 정책적인 판단이 있다. https://github.com/google/guava/issues/219) ImmutableSet.copyof()를 쓰도록 권고하고 있다.


다음 예제는 Lists.transform()을 이용하여 <String> 타입을 List<Integer>로 변환하는 예제이다.




import java.util.List;

import org.junit.Test;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

public class IterableTest {

@Test
public void test() {
    ImmutableList<String>
numberString = new ImmutableList.Builder<String>()
                .add(
"11111")
                .add(
"22222")
                .add(
"33333")
                .add(
"44444")
                .build();

            List<Integer>
result = Lists.transform(numberString,
                   new Function<String, Integer>() {
                          
@Override
  
public Integer apply(String value) {
                                
return Integer.parseInt(value);
                          }
            });

            System.
out.println(result);
}
}



결과

[11111, 22222, 33333, 44444]







Posted by '김용환'
,