Androidに最適化されたJavaクラス一覧

はじめに

Androidではモバイル端末での処理に最適化されたいくつかの代替クラスが用意されています。 ほとんどがHashMap関係のメモリ消費を抑えるためのものですが、Android Developersにも代替クラスをまとめたページはないようなので、知っている範囲でまとめてみました。

SparseArray

似ている機能:HashMap<Integer,T> SparseArrayはAndroid向けにパフォーマンスが改良されたHashMap<Integer,T>の代替クラス。 Supportライブラリに最新のSparseArrayと同じ実装になったSparseArrayCompatがある。 http://developer.android.com/reference/android/support/v4/util/SparseArrayCompat.html

LongSparseArray

似ている機能:HashMap<Long,T> API level 16から追加された、keyがintからlongになったSparseArray。 Supportライブラリにもある。 http://developer.android.com/reference/android/support/v4/util/LongSparseArray.html

SparseBooleanArray

似ている機能:HashMap<Integer,Boolean> valueがboolean型のSparseArrayHashMap<Integer,Boolean>の置き換えができるが、value型がBooleanではなくbooleanなので注意。 http://developer.android.com/reference/android/util/SparseBooleanArray.html

SparseIntArray

似ている機能:HashMap<Integer,Integer> valueがint型のSparseArrayHashMap<Integer,Integer>の置き換えができるが、value型がIntegerではなくintなので注意。 http://developer.android.com/reference/android/util/SparseIntArray.html

SparseLongArray

似ている機能:HashMap<Integer,Long> API level 18から追加された、valueがlong型のSparseArrayHashMap<Integer,Long>の置き換えができるが、value型がLongではなくlongなので注意。 Supporライブラリには存在しない。 https://developer.android.com/reference/android/util/SparseLongArray.html

ArrayMap

似ている機能:HashMap<K,V> API level 19で追加された、Map<K,V>の新しい実装。 HashMapよりもメモリ効率が良い。 Supportライブラリにもある。 http://developer.android.com/reference/android/support/v4/util/ArrayMap.html

SimpleArrayMap

似ている機能:HashMap<K,V> Supportライブラリにしか存在しない。 ArrayMapからMapインターフェイスの実装を外したバージョン。 …というよりも、ArrayMapがこのクラスにMapの実装を追加している。 おそらくこちらのほうが軽い。 http://developer.android.com/reference/android/support/v4/util/SimpleArrayMap.html

FloatMath

似ている機能:Math(一部) float専用の数値演算処理クラス。 javaMathに比べて速いが、肝心のメソッドが少ない…。 http://developer.android.com/reference/android/util/FloatMath.html

Pair

似ている機能:AbstractMap.SimpleEntry<K,V> key,valueの2つの値を任意の型で保持することができるクラス。 AndroidSDK上ではPairのほうが先に実装されているが、AbstractMap.SimpleEntry<K,V>とほぼ同様。 http://developer.android.com/reference/android/util/Pair.html