LoginSignup
4
4

More than 5 years have passed since last update.

CollectionUtilsのとっても簡単なサンプル

Last updated at Posted at 2016-02-03

初めましてこんにちは。

Qiitaデビューということで題材を悩みましたが、
JavaでApache Commons Collectionsを使用するサンプルを記載します。

ClosureとかPredicate、Transformerなんて分かりにくいですよね?
参考にしてみてください。

Apache Commons Collections 4.1 API

※ちょっとコード修正しました。

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.Closure;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.Transformer;

import lombok.AllArgsConstructor;
import lombok.Data;

/** サンプルクラス */
public class CommonsCollectionsSample {
  /** 人物クラス */
  @AllArgsConstructor
  @Data
  class Person {
    /** ID */
    private int id;
    /** 名前 */
    private String name;
    /** グループ */
    private int group;
  }

  /** サンプル */
  public void sample() {
    // 人物リスト
    @SuppressWarnings("serial")
    List<Person> list = new ArrayList<Person>() {
      {
        add(new Person(1, "a", 1));
        add(new Person(2, "b", 1));
        add(new Person(3, "c", 2));
        add(new Person(4, "d", 2));
        add(new Person(5, "e", 2));
      }
    };

    // 全て出力
    IterableUtils.forEach(list, new Closure<Person>() {
      @Override
      public void execute(Person person) {
        System.out.println(person.toString());
      }
    });
    System.out.println("----------");

    // 名前リストを作る
    List<String> nameList = CollectionUtils.collect(
      list, new Transformer<Person, String>() {
      @Override
      public String transform(Person person) {
        return person.getName();
      }}, new ArrayList<String>());

    // 全て出力
    IterableUtils.forEach(nameList, new Closure<String>() {
      @Override
      public void execute(String name) {
        System.out.println(name);
      }
    });
    System.out.println("----------");

    // グループ1だけに絞る
    CollectionUtils.filter(list, new Predicate<Person>() {
      @Override
      public boolean evaluate(Person person) {
        return person.getGroup() == 1;
      }
    });

    // 全て出力
    IterableUtils.forEach(list, new Closure<Person>() {
      @Override
      public void execute(Person person) {
        System.out.println(person.toString());
      }
    });
  }

  public static void main(String[] args) {
    new CommonsCollectionsSample() {
      {
        sample();
      }
    };
  }
}

実行結果は下記の通りです。

CommonsCollectionsSample.Person(id=1, name=a, group=1)
CommonsCollectionsSample.Person(id=2, name=b, group=1)
CommonsCollectionsSample.Person(id=3, name=c, group=2)
CommonsCollectionsSample.Person(id=4, name=d, group=2)
CommonsCollectionsSample.Person(id=5, name=e, group=2)
----------
a
b
c
d
e
----------
CommonsCollectionsSample.Person(id=1, name=a, group=1)
CommonsCollectionsSample.Person(id=2, name=b, group=1)
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4