《Guava Booleans类》学习笔记

Booleans是布尔型基本的实用工具类。

 

 

类声明

 

以下是com.google.common.primitives.Booleans类的声明:

 

@GwtCompatible(emulated=true)
   public final class Booleans
      extends Object

 

 

方法

 

 

 

方法继承

 

这个类继承了以下类方法:java.lang.Object

 

 

Booleans 示例

 

使用所选择的任何编辑器创建下面的java程序 C:/> Guava


GuavaTester.java

 

import java.util.List;
import com.google.common.primitives.Booleans;

public class GuavaTester {
   public static void main(String args[]){
      GuavaTester tester = new GuavaTester();
      tester.testBooleans();
   }

   private void testBooleans(){
      boolean[] booleanArray = {true,true,false,true,true,false,false};

      //convert array of primitives to array of objects
      List<Boolean> objectArray = Booleans.asList(booleanArray);
      System.out.println(objectArray.toString());

      //convert array of objects to array of primitives
      booleanArray = Booleans.toArray(objectArray);
      System.out.print("[ ");
      for(int i = 0; i< booleanArray.length ; i++){
         System.out.print(booleanArray[i] + " ");
      }
      System.out.println("]");
      //check if element is present in the list of primitives or not
      System.out.println("true is in list? "+ Booleans.contains(booleanArray, true));

      //return the first index of element
      System.out.println("true position in list "+ Booleans.indexOf(booleanArray, true));

      //Returns the count of true values	
      System.out.println("true occured: " + Booleans.countTrue());

      //Returns the comparisons		
      System.out.println("false Vs true: " + Booleans.compare(false, true));	
      System.out.println("false Vs false: " + Booleans.compare(false, false));	
      System.out.println("true Vs false: " + Booleans.compare(true, false));	
      System.out.println("true Vs true: " + Booleans.compare(true, true));	
   }
}

 

 

验证结果

 

使用javac编译器编译如下类

 

C:\Guava>javac GuavaTester.java

 

现在运行GuavaTester看到的结果

 

C:\Guava>java GuavaTester

 

看到结果

 

[true, true, false, true, true, false, false]
[ true true false true true false false ]
true is in list? true
true position in list 0
true occured: 0
false Vs true: -1
false Vs false: 0
true Vs false: 1
true Vs true: 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值