Convert List of Boolean to boolean Array - Android java.util

Android examples for java.util:List Convert

Description

Convert List of Boolean to boolean Array

Demo Code


//package com.book2s;

import java.util.Collection;

import java.util.List;

import java.util.Map;

public class Main {
    public static void main(String[] argv) {
        List list = java.util.Arrays.asList("asdf", "book2s.com");
        boolean defaultValue = true;
        System.out.println(java.util.Arrays.toString(toBooleanArray(list,
                defaultValue)));//from  ww  w. jav  a2 s  .  c  om
    }

    public static boolean[] toBooleanArray(List<Boolean> list,
            boolean defaultValue) {
        if (isEmpty(list)) {
            return new boolean[0];
        }
        boolean[] array = new boolean[list.size()];
        for (int I = 0; I < list.size(); I++) {
            Boolean v = list.get(I);
            if (v == null) {
                array[I] = defaultValue;
            } else {
                array[I] = v.booleanValue();
            }
        }
        return array;
    }

    public static boolean isEmpty(Collection<?> c) {
        return c == null || c.isEmpty();
    }

    public static boolean isEmpty(Map<?, ?> map) {
        return map == null || map.isEmpty();
    }

    @SafeVarargs
    public static <T> boolean isEmpty(T... array) {
        return array == null || array.length == 0;
    }
}

Related Tutorials