覚えたら書く

IT関係のデベロッパとして日々覚えたことを書き残したいです。twitter: @yyoshikaw

com.google.common.primitives.Ints

Guavacom.google.common.primitives.Intsの利用サンプルです。
Intsクラスはint型の値に対するユーティリティメソッドなどを提供しています。

サンプルコードで試したのは以下の通りです。


int配列 ⇒ List<Integer> の変換

■API

  • Ints#asList

■サンプル

System.out.println("### Ints.asList execute");
int[] srcArray = new int[] { 1, 2, 3, 5 };
List<Integer> destList = Ints.asList(srcArray);
System.out.println("destList : " + destList);


// ちなみに int[] ⇒ List<Integer> の変換は Arrays.asList ではできない
// List<Integer> bList = Arrays.asList(srcArray);  ← コンパイルエラー

■実行結果

### Ints.asList execute
destList : [1, 2, 3, 5]


IntegerのCollection ⇒ int配列 の変換

■API

  • Ints#toArray

■サンプル

System.out.println("### Ints.toArray execute");
List<Integer> srcList = Ints.asList(1, 1, 10, 100);
int[] destArray1 = Ints.toArray(srcList);
System.out.println("destArray1 : " + Arrays.toString(destArray1));

// ちなみに List<Integer> ⇒ int[] の変換は Collection.toArray ではできない
// int[] array = (int[]) srcList.toArray(new int[srcList.size()]);  ← コンパイルエラー

■実行結果

### Ints.toArray execute
destArray1 : [1, 1, 10, 100]


複数のint配列を結合して一つのint配列にする

■API

  • Ints#concat

■サンプル

System.out.println("### Ints.concat execute");
int[] srcArray1 = new int[] { 1, 2 };
int[] srcArray2 = new int[] { 1000, 2000 };
int[] destArray2 = Ints.concat(srcArray1, srcArray2);
System.out.println("destArray2 : " + Arrays.toString(destArray2));

■実行結果

### Ints.concat execute
destArray2 : [1, 2, 1000, 2000]


int配列の各要素を指定した区切り文字列で連結

■API

  • Ints#join

■サンプル

System.out.println("### Ints.join execute");
int[] srcArray3 = new int[] { 2013, 4, 10 };
String joinedStr = Ints.join("/", srcArray3);
System.out.println("joinedStr : " + joinedStr);

■実行結果

### Ints.join execute
joinedStr : 2013/4/10


int配列の中の最大値、最小値を取得

■API

  • Ints#max
  • Ints#min

■サンプル

System.out.println("### Ints.max,min execute");
int[] array1 = new int[] { 10, 11, 12, 5, 1, 100, 200, 300 };
System.out.println(Arrays.toString(array1) + " max -> " + Ints.max(array1));
System.out.println(Arrays.toString(array1) + " min -> " + Ints.min(array1));

■実行結果

### Ints.max,min execute
[10, 11, 12, 5, 1, 100, 200, 300] max -> 300
[10, 11, 12, 5, 1, 100, 200, 300] min -> 1


int配列の中の指定の値を含むかどうか

■API

  • Ints#contains

■サンプル

System.out.println("### Ints.contains execute");
int[] array2 = new int[] { 10, 11, 12, 5, 1, 100, 200, 300 };
System.out.println(Arrays.toString(array2) + " contains 5 -> " + Ints.contains(array2, 5));
System.out.println(Arrays.toString(array2) + " contains 0 -> " + Ints.contains(array2, 0));

■実行結果

### Ints.contains execute
[10, 11, 12, 5, 1, 100, 200, 300] contains 5 -> true
[10, 11, 12, 5, 1, 100, 200, 300] contains 0 -> false


int型の値の比較

■API

  • Ints#compare

■サンプル

System.out.println("### Ints.compare execute");
System.out.println("1 compare 2 -> " + Ints.compare(1, 2));
System.out.println("2 compare 1 -> " + Ints.compare(2, 1));
System.out.println("1 compare 1 -> " + Ints.compare(1, 1));

■実行結果

### Ints.compare execute
1 compare 2 -> -1
2 compare 1 -> 1
1 compare 1 -> 0




試したソースコードの全体は以下です。

import java.util.Arrays;
import java.util.List;

import com.google.common.primitives.Ints;

public class IntsClient {

    public static void main(String[] args) {

        // int[] ⇒ List<Integer> の変換
        System.out.println("### Ints.asList execute");
        int[] srcArray = new int[] { 1, 2, 3, 5 };
        List<Integer> destList = Ints.asList(srcArray);
        System.out.println("destList : " + destList);
        // int[] ⇒ List<Integer> の変換はArrays.asListではできない
        // List<Integer> bList = Arrays.asList(srcArray);  ← コンパイルエラー

        // IntegerのCollection ⇒ int[] の変換
        System.out.println("### Ints.toArray execute");
        List<Integer> srcList = Ints.asList(1, 1, 10, 100);
        int[] destArray1 = Ints.toArray(srcList);
        System.out.println("destArray1 : " + Arrays.toString(destArray1));
        // List<Integer> ⇒ int[] の変換はCollection.toArrayではできない
        // int[] array = (int[]) srcList.toArray(new int[srcList.size()]);  ← コンパイルエラー

        // 複数のint配列を結合して一つのint配列にする
        System.out.println("### Ints.concat execute");
        int[] srcArray1 = new int[] { 1, 2 };
        int[] srcArray2 = new int[] { 1000, 2000 };
        int[] destArray2 = Ints.concat(srcArray1, srcArray2);
        System.out.println("destArray2 : " + Arrays.toString(destArray2));

        // int配列の各要素を指定した区切り文字列で連結した文字列を取得
        System.out.println("### Ints.join execute");
        int[] srcArray3 = new int[] { 2013, 4, 10 };
        String joinedStr = Ints.join("/", srcArray3);
        System.out.println("joinedStr : " + joinedStr);

        // int配列の中の最大値、最小値を取得
        System.out.println("### Ints.max,min execute");
        int[] array1 = new int[] { 10, 11, 12, 5, 1, 100, 200, 300 };
        System.out.println(Arrays.toString(array1) + " max -> " + Ints.max(array1));
        System.out.println(Arrays.toString(array1) + " min -> " + Ints.min(array1));

        // int配列の中の指定の値を含むかどうかを取得
        System.out.println("### Ints.contains execute");
        int[] array2 = new int[] { 10, 11, 12, 5, 1, 100, 200, 300 };
        System.out.println(Arrays.toString(array2) + " contains 5 -> " + Ints.contains(array2, 5));
        System.out.println(Arrays.toString(array2) + " contains 0 -> " + Ints.contains(array2, 0));

        // int型の値の比較(compare)
        System.out.println("### Ints.compare execute");
        System.out.println("1 compare 2 -> " + Ints.compare(1, 2));
        System.out.println("2 compare 1 -> " + Ints.compare(2, 1));
        System.out.println("1 compare 1 -> " + Ints.compare(1, 1));
    }
}



関連エントリ