具有FastUtil的精简Java集合

针对我最近在GNU Trove库上发表的《 发现Java原始资源集合的处理 》一TheAlchemist指出了fastutil优于trove的一些优点:“我更喜欢fastutil( http://fastutil.di.unimi.it/ ),因为它仍在积极开发中,具有更多功能,支持大尺寸(> 2 ^ 32),并且具有更好的文档。” 阿提拉-米哈伊·巴拉兹借调第二个“I @ TheAlchemist的建议为fastutil:此! 这是一个很棒的图书馆。” 在这篇文章中,我从以前看过trove的相同角度研究了fastutil

fastutil主页上,将fastutil描述为Java TM Collections Framework的扩展,它提供了“特定类型的映射,集合,列表和队列,具有较小的内存占用以及快速的访问和插入”,以及“大型(64位)数组,设置和列表,以及用于二进制和文本文件的快速,实用的I / O类。” fastutil许可证Apache License,版本2 ,而fastutil的当前版本需要Java 7或更高版本。 目前(截至撰写本文时), Java 6Java 5也可以下载“未维护”的fastutil版本。

通过与标准Java集合使用的API调用相同的API调用,可以将元素添加到FastUtil集合中,如下面的代码清单所示,该代码清单将在JDK ArrayList中插入元素与在FastUtil DoubleArrayList中插入元素进行比较。

使用JDK插入双打和使用FastUtil DoubleArrayList插入双打

/**
 * Demonstrate standard JDK {@code ArrayList<Double>}
 * with some JDK 8 functionality.
 */
public void demonstrateJdkArrayListForDoubles()
{
   final ArrayList<Double> doubles = new ArrayList<>();
   doubles.add(15.5);
   doubles.add(24.4);
   doubles.add(36.3);
   doubles.add(67.6);
   doubles.add(10.0);
   out.println("JDK ArrayList<Double>:");
   out.println("\tDoubles List: " + doubles);
}

/**
 * Demonstrate use of DoubleArrayList and show how
 * similar using it is to using {@code ArrayList<Double>}.
 */
public void demonstrateFastUtilArrayListForDoubles()
{
   // Demonstrate adding elements to DoubleArrayList is
   // exactly like adding elements to ArrayList<Double>.
   final DoubleArrayList doubles = new DoubleArrayList();
   doubles.add(15.5);
   doubles.add(24.4);
   doubles.add(36.3);
   doubles.add(67.6);
   doubles.add(10.0);
   out.println("FastUtil DoubleArrayList:");  // DoubleArrayList overrides toString()
   out.println("\tDoubles List: " + doubles);
}

当执行上述两种方法时,写入标准输出的双精度项列表看起来完全相同,即使用相同的方括号括住以逗号分隔的双精度值。

FastUtil集合倾向于实现适当的JDK集合接口。 例如,刚刚演示的类DoubleArrayList实现了几个接口,包括Collection <Double>和List <Double>。 事实证明, DoubleArrayList还实现了it.unimi.dsi.fastutil.doubles.DoubleStackit.unimi.dsi.fastutil.Stack <Double> 。 下一个代码清单中展示了使用此类作为堆栈的能力。

使用FastUtil的DoubleArrayList作为堆栈

/**
 * Demonstrate FastUtil's Double Stack.
 *
 * FastUtil's DoubleStack allows access to its
 * contents via push, pop, and peek. It is declared
 * as a DoubleArrayList type here so that the size()
 * method is available without casting.
 */
public void demonstrateFastUtilDoubleStack()
{
   final DoubleArrayList stack = new DoubleArrayList();
   stack.push(15.5);
   stack.push(17.3);
   stack.push(16.6);
   stack.push(2.2);
   out.println("FastUtil Stack of Doubles");
   out.println("\tPeek: " + stack.peek(0) + "; After Size: " + stack.size());
   out.println("\tPop:  " + stack.pop() + "; After Size: " + stack.size());
   out.println("\tPeek: " + stack.peek(0) + "; After Size: " + stack.size());
}

正如我在Trove上博客文章中所讨论的那样,Trove提供了一个gnu.trove.TCollections类,该类类似于java.util.Collections的 (子集)。 FastUtil提供了类似的功能,但是这种提供静态方法以对FastUtil集合起作用的方法通过静态方法被分为特定于类型的类型和特定于结构的类,而不是在具有静态方法的单个类中。 下一个代码清单演示将这些特定于类型和特定于结构的类之一与静态方法IntSets结合使用, 并将其与FastUtil IntLinkedOpenHashSet结合使用。 顾名思义, IntSets类提供了“使用[int]特定的集可以做有用的事情的静态方法和对象。”

将IntSet与IntLinkedOpenHashSet一起使用

/**
 * Demonstrate one of FastUtil's "equivalent"s of the
 * java.util.Collections class. FastUtil separates its
 * grouping of static methods into classes that are
 * specific to the data type of the collection and to
 * the data structure type of the collection.
 */
public void demonstrateFastUtilCollectionsClass()
{
   final IntLinkedOpenHashSet integers = new IntLinkedOpenHashSet();
   integers.add(5);
   integers.add(7);
   integers.add(3);
   integers.add(1);
   final IntSet unmodifiableIntegers = IntSets.unmodifiable(integers);
   out.println("Unmodifiable Integers:");
   out.println("\tClass: " + unmodifiableIntegers.getClass().getCanonicalName());
   try
   {
      unmodifiableIntegers.add(15);
   }
   catch (Exception ex)
   {
      out.println("\tException caught: " + ex);
   }
}

FastUtil支持使用显式迭代器和Java SE 5引入的for-each循环的标准Java迭代方法。 因为FastUtil集合实现java.lang.Iterable,所以FastUtil集合甚至使用.forEach支持JDK 8样式 (假定代码是在JDK 8上构建和运行的)。 这些将在下一个代码清单中进行演示。

以标准Java样式迭代FastUtil集合

/**
 * Demonstrate "traditional" Java iteration of a
 * FastUtil collection.
 */
public void demonstrateIterationWithIterator()
{
   final LongOpenHashSet longs = new LongOpenHashSet();
   longs.add(15);
   longs.add(6);
   longs.add(12);
   longs.add(13);
   longs.add(2);
   final LongIterator longIterator = longs.iterator();
   while (longIterator.hasNext())
   {
      final long longValue = longIterator.next();
      out.print(longValue + " ");
   }
}

/**
 * Demonstrate iteration of a FastUtil collection
 * using Java's enhanced for-each approach.
 */
public void demonstrateIterationWithForEach()
{
   final LongLinkedOpenHashSet longs = new LongLinkedOpenHashSet();
   longs.add(15);
   longs.add(6);
   longs.add(12);
   longs.add(13);
   longs.add(2);
   for (final long longValue : longs)
   {
      out.println(longValue + " ");
   }
}

/**
 * Demonstrate iteration of a FastUtil collection
 * using JDK 8 .forEach approach.
 */
public void demonstrateIterationWithJdk8ForEach()
{
   final LongLinkedOpenHashSet longs = new LongLinkedOpenHashSet();
   longs.add(15);
   longs.add(6);
   longs.add(12);
   longs.add(13);
   longs.add(2);
   longs.forEach(longValue -> out.print(longValue + " "));
}

与FastUtil相关的其他观察

  • 由于FastUtil集合实现标准的JDK 8集合接口,因此可以很容易地使用熟悉的Java习惯用法来获取和使用这些API。
  • FastUtil集合通常提供一个构造函数,该构造函数接受基础数据类型的数组以及重写的toArray()方法和特定于类型的方法(例如toDoubleArray() [用于双向定位的集合])以数组形式提供其数据元素。原语。
  • FastUtil集合通常提供显式覆盖的toString()实现,这些实现允许像JDK集合一样轻松编写各个数据元素,并且与Java数组(需要Arrays.toString()方法)不同。
  • FastUtil的Java软件包通常按原始类型进行组织,并且该原始类型的各种数据结构类型的特定实现都在同一软件包中进行。 例如,程序包的名称类似it.unimi.dsi.fastutil.doublesit.unimi.dsi.fastutil.ints等。
  • 因为每个FastUtil集合特定于特定的原始数据类型,所以每个集合都不需要通用参数,并且没有与通用相关的问题(例如擦除)。 我还没有看到FastUtil像Trove那样利用特定于类型的方法来收集特定于类型的方法,这可能是因为FastUtil更紧密地实现了相应的Java集合接口。
  • 学习使用FastUtil时, FastUtil的Javadoc API文档可能是最佳的起点。 在FastUtil的基于Javadoc的API文档中,类,接口,枚举和包通常都记录得很好。

结论

Trove一样,FastUtil是一个库,可以用来更有效地(就内存和性能而言)与Java集合一起使用。 虽然Trove以前似乎是众多选择中最受欢迎的,但由于包括TheAlchemist列举的原因,FastUtil可能是当前最受欢迎的选择 :“仍在积极开发中,具有更多功能,支持大尺寸(> 2 ^ 32 ),并具有更好的文档。” 除Trove和FastUtil外, 类似的库还包括Java的高性能基元集合HPPC ), KolobokeGoldman Sachs集合Mahout集合Javolution

翻译自: https://www.javacodegeeks.com/2016/01/leaner-java-collections-with-fastutil.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值