Java List Sort sortListOfDoublesAndRemoveDuplicates(List items, double tolerance, double toleranceSmallNumbers)

Here you can find the source of sortListOfDoublesAndRemoveDuplicates(List items, double tolerance, double toleranceSmallNumbers)

Description

Sort (in place this list of doubles and remove duplicate values (where 'duplicate(a,b)' is 'not diffDoubles(a,b).'

License

Open Source License

Parameter

Parameter Description
items List to be sorted and duplicates removed.

Declaration

public static void sortListOfDoublesAndRemoveDuplicates(List<Double> items, double tolerance,
        double toleranceSmallNumbers) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Collections;

import java.util.Iterator;

import java.util.List;

public class Main {
    /**//from w ww  .  j  ava2  s . co  m
     * How much two numbers (outside of (-1, 1) can differ before they are no longer considered
     * equivalent.
     */
    private static final double EQUIVALENCE_TOLERANCE = 0.0000001;
    private static final double EQUIVALENCE_TOLERANCE_SMALL_NUMBERS = 8 * Double.MIN_NORMAL;

    /**
     * Sort (in place this list of doubles and remove duplicate values (where
     * 'duplicate(a,b)' is 'not diffDoubles(a,b).'
     *
     * Sort (in place this list of doubles and remove duplicate values (where
     * 'duplicate(a,b)' is defined by the comparator). public static <E> void
     * sortAndRemoveDuplicates(List<E> items, Comparator<E> comparator) {
     * Collections.sort(items); <--- error here E prev = null; for (Iterator<E>
     * iter = items.iterator(); iter.hasNext(); ) { E item = iter.next(); if
     * (prev == null || comparator.compare(item, prev) == 0) { prev = item; } //
     * BUG: wont remover duplicate NULLS. Use a counter to see if at first item?
     * else { iter.remove(); } } }
     *
     * @param items List to be sorted and duplicates removed.
     */
    public static void sortListOfDoublesAndRemoveDuplicates(List<Double> items, double tolerance,
            double toleranceSmallNumbers) { // TODO use the above and make diffDoubles a comparator.
        Collections.sort(items);

        Double prev = null;
        for (Iterator<Double> iter = items.iterator(); iter.hasNext();) {
            Double d = iter.next();
            if (prev == null || diffDoubles(prev, d, tolerance, toleranceSmallNumbers)) {
                prev = d;
            } else {
                iter.remove();
            }
        }
    }

    public static void sortListOfDoublesAndRemoveDuplicates(List<Double> items) {
        sortListOfDoublesAndRemoveDuplicates(items, EQUIVALENCE_TOLERANCE, EQUIVALENCE_TOLERANCE_SMALL_NUMBERS);
    }

    /**
     * Compares two numbers to see if they are different.
     * 
     * @param a
     *            A number.
     * @param b
     *            A number.
     * @return True if the two given numbers differ by more than a certain
     *         tolerance.
     * @see #EQUIVALENCE_TOLERANCE
     */
    public static boolean diffDoubles(double a, double b, double tolerance, double toleranceSmallNumbers) {
        double diff = Math.abs(a - b);
        boolean firstResult = diff > tolerance;
        if (firstResult) {
            return true;
        }
        // See if we're dealing with small numbers.
        if (a > -1 && a < 1 && b > -1 && b < 1) {
            return diff > toleranceSmallNumbers;
        }
        return firstResult;
    }

    public static boolean diffDoubles(double a, double b) {
        return diffDoubles(a, b, EQUIVALENCE_TOLERANCE, EQUIVALENCE_TOLERANCE_SMALL_NUMBERS);
    }
}

Related

  1. sortList(List items)
  2. sortList(List list, boolean asc)
  3. sortListAlphabetically(List list)
  4. sortListByMapKey(List> list)
  5. sortListEntry(List firstList, List secondList)
  6. sortListStingArrayByFirstDes( List list)
  7. sortLocales(final List locales)
  8. sortLongSeqList(List firstList, List secondList)
  9. sortLoops(ArrayList list)