Skip to content

Instantly share code, notes, and snippets.

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