开源项目

知识点

相关文章

更多

最近更新

更多

Guava 处理java short实用工具类-Shorts类

2019-04-23 23:16|来源: 网路

Shorts是基本类型short的实用工具类。

类声明

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

@GwtCompatiblepublic final class Shorts
   extends Object

字段

S.N. 字段及说明
1 static int BYTES
所需要的字节数来表示一个原始short的值。
2 static short MAX_POWER_OF_TWO
两个最大的幂可以被表示为short。

方法

S.N. 方法及说明
1 static List<Short> asList(short... backingArray)
返回由指定数组支持的固定大小的列表,类似 Arrays.asList(Object[]).
2 static short checkedCast(long value)
返回 short 值,该值等于value,如果可能的话。
3 static int compare(short a, short b)
比较两个指定的short值。
4 static short[] concat(short[]... arrays)
每个阵列提供组合成一个单一的阵列,则返回其值。
5 static boolean contains(short[] array, short target)
返回true,如果目标是否存在在任何地方数组元素。
6 static short[] ensureCapacity(short[] array, int minLength, int padding)
返回一个包含相同的值数组的数组,但保证是一个规定的最小长度。
7 static short fromByteArray(byte[] bytes)
返回短值,其大端表示被存储在最前2字节的字节;相当于 ByteBuffer.wrap(bytes).getShort().
8 static short fromBytes(byte b1, byte b2)
返回short值的字节表示的是给定2个字节,以 big-endian 顺序; 相当于 Shorts.fromByteArray(new byte[] {b1, b2}).
9 static int hashCode(short value)
返回值的哈希码;等于调用的结果 ((Short) value).hashCode().
10 static int indexOf(short[] array, short target)
返回值目标数组的首次出现的索引。
11 static int indexOf(short[] array, short[] target)
返回指定目标的第一个匹配的起始位置数组或-1,如果不存在这样的发生。
12 static String join(String separator, short... array)
返回包含由分离器分离所提供的短值的字符串。
13 static int lastIndexOf(short[] array, short target)
返回目标在数组中最后一个出现的索引的值。
14 static Comparator<short[]> lexicographicalComparator()
返回一个比较,比较两个 short 阵列字典顺序。
15 static short max(short... array)
返回出现在数组中的最大值。
16 static short min(short... array)
返回出现在数组的最小值。
17 static short saturatedCast(long value)
返回short最接近int的值。
18 static Converter<String,Short> stringConverter()
返回使用字符串和shorts之间的一个转换器序列化对象 Short.decode(java.lang.String) and Short.toString().
19 static short[] toArray(Collection<? extends Number> collection)
返回包含集合的每个值的数组,转换为 short 值的方式Number.shortValue().
20 static byte[] toByteArray(short value)
返回在2元素的字节数组值大尾数法表示;相当于 ByteBuffer.allocate(2).putShort(value).array().

继承的方法

这个类继承了以下类方法:

  • java.lang.Object

Shorts 示例

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

GuavaTester.java

import java.util.List;
import com.google.common.primitives.Shorts;
public class GuavaTester {

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

   private void testShorts(){
      short[] shortArray = {1,2,3,4,5,6,7,8,9};

      //convert array of primitives to array of objects
      List<Short> objectArray = Shorts.asList(shortArray);
      System.out.println(objectArray.toString());

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

      //Returns the minimum		
      System.out.println("Min: " + Shorts.min(shortArray));

      //Returns the maximum		
      System.out.println("Max: " + Shorts.max(shortArray));
      data = 2400;
      //get the byte array from an integer
      byte[] byteArray = Shorts.toByteArray(data);
      for(int i = 0; i< byteArray.length ; i++){
         System.out.print(byteArray[i] + " ");
      }
   }
 }

验证结果

使用javac编译器编译如下类

C:\Guava>javac GuavaTester.java

现在运行GuavaTester看到的结果

C:\Guava>java GuavaTester

看到结果。

[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
9 96


相关问答

更多
  • 你能帮我理解这里涉及的权衡吗? 这是一个权衡: 性能 - 不分配临时数组可以节省成本。 但是,人们需要进行一些广泛的代码分析和基准测试来量化这种节省。 (我怀疑在大多数应用程序中它都是微不足道的。请阅读@Voo提供的这个链接 !) 可读性 - 拥有一堆额外的重载会使javadoc混乱。 可维护性 - 有一堆重载是以不需要临时对象的方式实现的,这需要大量的复制/粘贴编程,这使得将来的代码维护更加困难。 实用程序 - 这些重载的使用频率是多少? 我希望答案“很少”。 字节码占用空间 - 这些额外的重载将导致使用 ...
  • 测试包不在Guava版本中发布。 我认为这意味着番石榴测试本身,而不是普通大众。 这在Javadocs中也没有提及。 The testing package is not distributed in the Guava release. I think it is meant for the Guava tests themselves, not for the general public. It's not mentioned in the Javadocs either.
  • 我使用java Enum作为我的密钥,因此有一个有效的.equals和.hashCode默认实现,对吧? 无论是equals还是hashcode都被覆盖,所以它几乎不会更快(即等于返回this == other )。 我的第二个问题是“让你的元素类型缓存自己的散列码”甚至意味着什么! 你可能有类似下面的代码,以避免多次计算 - 如果你这样做,你需要确保: 哈希码是不变的(即不能在实例的整个生命周期中改变) 你的hashcode方法是线程安全的(这可以通过使用局部变量来完成,例如或者更简单地通过使得hash变 ...
  • 好吧,你不会在创建的线程中做任何事情,除了创建一个最终在堆中的对象(在线程之间共享),但由于对它的引用是在运行后没有保持,所以它也丢失了。 myObject @Subscribe方法在调用eventBus.post(event);的同一个线程中调用eventBus.post(event); 而不是在创建myObject的线程中。 我发现的奇怪的事情是我在MyObject类中的订阅方法的所有操作/日志记录都在主线程中再次执行 如果你的MyObject类有一个@Subscribe方法,那么为什么它需要构造函数中 ...
  • Guava没有提供这个,尽管你可以通过找到给定点之前和之后的第一个范围来在RangeSet之上构建这样的东西。 但一般来说,除了类型的比较排序之外,Guava Range对距离,度量或任何其他内容一无所知 。 他们不知道10比15更接近11 。 Guava doesn't provide this, though you might be able to build such a thing on top of a RangeSet by finding the first range before and ...
  • 在RUNNING状态下调用stop()时,状态将更改为STOPPING并doStop()方法。 之后再次调用stop()不应该做任何事情。 doStop()实现应该调用notifyStopped() ,它将状态从STOPPING更改为TERMINATED 。 When you call stop() in the RUNNING state, the state is changed to STOPPING and the doStop() method gets called. Calling stop( ...
  • 好的,我明白了。 问题是RoleConverter转换为ceylon.language.String ,而api期望转换为java.lang.String 。 现在修复它: shared class RoleConverter() extends Converter() { shared actual Role doBackward(JString? name) { "Cannot convert null to role." asser ...
  • 因为你对com.google.guava:guava:23.3 org.seleniumhq.selenium:selenium-java:3.0.1依赖性com.google.guava:guava:23.3使用org.seleniumhq.selenium:selenium-java:3.0.1可能会让你在十字路口。 但是Selenium发行说明清楚地提到了以下番石榴依赖性: Selenium v2.49.0 :Bump guava到19版 Selenium v3.1.0 :需要更新最新的番石榴版本21 ...
  • 从番石榴自己的解释来看,你必须使用Throwable ,是的。 这是他们的示例代码段: public void foo() throws IOException { Closer closer = Closer.create(); try { InputStream in = closer.register(openInputStream()); OutputStream out = closer.register(openOutputStream()); // do stu ...
  • AbstractScheduledServiced实现Service 。 Service接口描述了生命周期方法,包括startAsync 。 ServiceState枚举文字包含有关其含义的文档。 处于NEW状态的服务(刚刚创建): 处于此状态的服务处于非活动状态。 它做的工作量极少,占用资源极少。 要使服务执行某些有用的操作,您必须将其转换为RUNNING状态 此状态下的服务正在运行。 这就是为什么你必须在它做任何事之前启动服务的原因。 我还建议不要从构造函数中调用startAsync,而是从创建MySe ...