Skip to content

Instantly share code, notes, and snippets.

@YukiYoshikawa
Last active December 16, 2015 19:49
Show Gist options
  • Save YukiYoshikawa/5487663 to your computer and use it in GitHub Desktop.
Save YukiYoshikawa/5487663 to your computer and use it in GitHub Desktop.
package trial.yy.guava.client.primitives;
import com.google.common.primitives.Ints;
import java.util.Arrays;
import java.util.List;
/**
* com.google.common.primitives.Intsを試すためのサンプル
* User: yy
*/
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));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment