Guavaのcom.google.common.primitives.Booleans
の利用サンプルです。
Booleansクラスはboolean型の値に対するユーティリティメソッドなどを提供しています。
サンプルコードで試したのは以下の通りです。
boolean配列 ⇒ List<Boolean> の変換
■API
Booleans#asList
■サンプル
boolean[] srcArray = new boolean[] { true, false, true }; List<Boolean> destList = Booleans.asList(srcArray); System.out.println("destList : " + destList); // boolean[] ⇒ List<Boolean> の変換はArrays.asListではできない // List<Boolean> bList = Arrays.asList(srcArray); ← コンパイルエラー
■実行結果
destList : [true, false, true]
BooleanのCollection ⇒ boolean配列 の変換
■API
Booleans#toArray
■サンプル
List<Boolean> srcList = Booleans.asList(true, true, false, false); boolean[] destArray1 = Booleans.toArray(srcList); System.out.println("destArray1 : " + Arrays.toString(destArray1)); // List<Boolean> ⇒ boolean[] の変換はCollection.toArrayではできない // boolean[] array = (boolean[]) srcList.toArray(new boolean[srcList.size()]); ← コンパイルエラー
■実行結果
destArray1 : [true, true, false, false]
複数のboolean配列を結合して一つのboolean配列にする
■API
Booleans#concat
■サンプル
boolean[] srcArray1 = new boolean[] { true, false}; boolean[] srcArray2 = new boolean[] { false, false}; boolean[] destArray2 = Booleans.concat(srcArray1, srcArray2); System.out.println("destArray2 : " + Arrays.toString(destArray2));
■実行結果
destArray2 : [true, false, false, false]
boolean配列の各要素を指定した区切り文字列で連結
■API
Booleans#join
■サンプル
boolean[] srcArray3 = new boolean[] { false, false, true, false}; String joinedStr = Booleans.join(" / ", srcArray3); System.out.println("joinedStr : " + joinedStr);
■実行結果
joinedStr : false / false / true / false
試したソースコードの全体は以下です。
import java.util.Arrays; import java.util.List; import com.google.common.primitives.Booleans; public class BooleansClient { public static void main(String[] args) { // boolean[] ⇒ List<Boolean> の変換 System.out.println("### Booleans.asList execute"); boolean[] srcArray = new boolean[] { true, false, true }; List<Boolean> destList = Booleans.asList(srcArray); System.out.println("destList : " + destList); // boolean[] ⇒ List<Boolean> の変換はArrays.asListではできない // List<Boolean> bList = Arrays.asList(srcArray); ← コンパイルエラー // BooleanのCollection ⇒ boolean[] の変換 System.out.println("### Booleans.toArray execute"); List<Boolean> srcList = Booleans.asList(true, true, false, false); boolean[] destArray1 = Booleans.toArray(srcList); System.out.println("destArray1 : " + Arrays.toString(destArray1)); // List<Boolean> ⇒ boolean[] の変換はCollection.toArrayではできない // boolean[] array = (boolean[]) srcList.toArray(new boolean[srcList.size()]); ← コンパイルエラー // 複数のboolean配列を結合して一つのboolean配列にする System.out.println("### Booleans.concat execute"); boolean[] srcArray1 = new boolean[] { true, false}; boolean[] srcArray2 = new boolean[] { false, false}; boolean[] destArray2 = Booleans.concat(srcArray1, srcArray2); System.out.println("destArray2 : " + Arrays.toString(destArray2)); // boolean配列の各要素を指定した区切り文字列で連結した文字列を取得 System.out.println("### Booleans.join execute"); boolean[] srcArray3 = new boolean[] { false, false, true, false}; String joinedStr = Booleans.join(" / ", srcArray3); System.out.println("joinedStr : " + joinedStr); } }