zoukankan      html  css  js  c++  java
  • guava 基础类型应用

      guava提供了Bytes/Shorts/Ints/Iongs/Floats/Doubles/Chars/Booleans这些基本数据类型的扩展支持,只有你想不到的,没有它没有的!对JDK集合的有效补充

      对于程序员来说直接上代码比讲解来的更实际一些,这里注重聊一下 Ints 的使用,其他类型的都类似。来吧,骚年们:

    import java.util.List;
    
    import com.google.common.primitives.Ints;
    
    public class IntsDome {
        public static void main(String args[]) {
            IntsDome tester = new IntsDome();
            tester.testInts();
        }
    
        private void testInts() {
            int[] intArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    
            // 将 int 数组转成 int list
            List<Integer> objectArray = Ints.asList(intArray);
            System.out.println(objectArray.toString());
    
            // 将 int list 转成 int array 并格式化输出
            intArray = Ints.toArray(objectArray);
            System.out.print("[ ");
            for (int i = 0; i < intArray.length; i++) {
                System.out.print(intArray[i] + " ");
            }
            System.out.println("]");
    
            // 判断一个整数是否在一个数组中
            System.out.println("5 is in list? " + Ints.contains(intArray, 5));
    
            // 返回一个数组中最小的数字
            System.out.println("Min: " + Ints.min(intArray));
    
            // 返回一个数组中最大的数字
            System.out.println("Max: " + Ints.max(intArray));
    
            // 将一个整数转换成一个字节数组并输出
            byte[] byteArray = Ints.toByteArray(20000);
            for (int i = 0; i < byteArray.length; i++) {
                System.out.print(byteArray[i] + " ");
            }
        }
    }

      最终的打印结果为:

    [1, 2, 3, 4, 5, 6, 7, 8, 9]
    [ 1 2 3 4 5 6 7 8 9 ]
    5 is in list? true
    Min: 1
    Max: 9
    0 0 78 32 
  • 相关阅读:
    Centos6.6部署Redis集群
    贪心算法解+-字符串
    水题记录--排序
    项目总结之HashMap问题
    水题记录--大整数求阶乘
    水题记录--组合数
    水题记录-成绩转换
    水题记录
    简单排序
    数组
  • 原文地址:https://www.cnblogs.com/liang1101/p/13644305.html
Copyright © 2011-2022 走看看