Skip to content

Instantly share code, notes, and snippets.

@YukiYoshikawa
Last active December 17, 2015 02:39
Show Gist options
  • Save YukiYoshikawa/5537457 to your computer and use it in GitHub Desktop.
Save YukiYoshikawa/5537457 to your computer and use it in GitHub Desktop.
package trial.yy.guava.client.primitives;
import com.google.common.primitives.Doubles;
import java.util.Arrays;
import java.util.List;
/**
* com.google.common.primitives.Doublesを試すためのサンプル
* User: yy
*/
public class DoublesClient {
public static void main(String[] args) {
// double[] ⇒ List<Double> の変換
System.out.println("### Doubles.asList execute");
double[] srcArray = new double[] { 1.0, 2.1, 3.2, 5.3 };
List<Double> destList = Doubles.asList(srcArray);
System.out.println("destList : " + destList);
// double[] ⇒ List<Double> の変換はArrays.asListではできない
// List<Double> bList = Arrays.asList(srcArray); ← コンパイルエラー
// DoubleのCollection ⇒ double[] の変換
System.out.println("### Doubles.toArray execute");
List<Double> srcList = Doubles.asList(1.1, 1.11, 10.111, 100.1111);
double[] destArray1 = Doubles.toArray(srcList);
System.out.println("destArray1 : " + Arrays.toString(destArray1));
// List<Double> ⇒ double[] の変換はCollection.toArrayではできない
// double[] array = (double[]) srcList.toArray(new double[srcList.size()]); ← コンパイルエラー
// 複数のdouble配列を結合して一つのdouble配列にする
System.out.println("### Doubles.concat execute");
double[] srcArray1 = new double[] { 1.0, 2.123 };
double[] srcArray2 = new double[] { 1000.9, 2000.5 };
double[] destArray2 = Doubles.concat(srcArray1, srcArray2);
System.out.println("destArray2 : " + Arrays.toString(destArray2));
// double配列の各要素を指定した区切り文字列で連結した文字列を取得
System.out.println("### Doubles.join execute");
double[] srcArray3 = new double[] { 2013.0, 4.0, 10.0 };
String joinedStr = Doubles.join("/", srcArray3);
System.out.println("joinedStr : " + joinedStr);
// double配列の中の最大値、最小値を取得
System.out.println("### Doubles.max,min execute");
double[] array1 = new double[] { 10.0, 11.1, 12.2, 5.3, 1.4, 100.5, 200.6, 300.7 };
System.out.println(Arrays.toString(array1) + " max -> " + Doubles.max(array1));
System.out.println(Arrays.toString(array1) + " min -> " + Doubles.min(array1));
// double配列の中の指定の値を含むかどうかを取得
System.out.println("### Doubles.contains execute");
double[] array2 = new double[] { 10.0, 11.1, 12.2, 5.3, 1.4, 100.5, 200.6, 300.7 };
System.out.println(Arrays.toString(array2) + " contains 5.3 -> " + Doubles.contains(array2, 5.3));
System.out.println(Arrays.toString(array2) + " contains 0.0 -> " + Doubles.contains(array2, 0.0));
// double型の値の比較(compare)
System.out.println("### Doubles.compare execute");
System.out.println("1.9 compare 2.0 -> " + Doubles.compare(1.9, 2.0));
System.out.println("2.01 compare 1.99 -> " + Doubles.compare(2.01, 1.99));
System.out.println("1.001 compare 1.001 -> " + Doubles.compare(1.001, 1.001));
// 有限値かどうかをチェックする
System.out.println("### Doubles.isFinite execute");
System.out.println("1000" + " isFinite -> " + Doubles.isFinite(1000));
System.out.println("1.9" + " isFinite -> " + Doubles.isFinite(1.9999));
System.out.println(Double.POSITIVE_INFINITY + " isFinite -> " + Doubles.isFinite(Double.POSITIVE_INFINITY));
System.out.println(Double.NEGATIVE_INFINITY + " isFinite -> " + Doubles.isFinite(Double.NEGATIVE_INFINITY));
System.out.println(Double.NaN + " isFinite -> " + Doubles.isFinite(Double.NaN));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment