Example usage for java.util Collections reverseOrder

List of usage examples for java.util Collections reverseOrder

Introduction

In this page you can find the example usage for java.util Collections reverseOrder.

Prototype

@SuppressWarnings("unchecked")
public static <T> Comparator<T> reverseOrder() 

Source Link

Document

Returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface.

Usage

From source file:app.RunApp.java

/**
 * Get top n related labels for heatmap//from w  w  w.  j a v  a 2s  . c o  m
 * 
 * @param n Number of labels to select
 * @return Selected label indices
 */
public int[] getTopRelatedHeatmap(int n) {

    ArrayList<LabelsPairValue> pairs = new ArrayList<>();
    for (int i = 0; i < heatmapCoefficients.length; i++) {
        for (int j = 0; j < heatmapCoefficients.length; j++) {
            if (heatmapCoefficients[i][j] > 0) {
                pairs.add(new LabelsPairValue(i, j, heatmapCoefficients[i][j]));
            }
        }
    }
    Collections.sort(pairs, Collections.reverseOrder());

    int numLabels = n;
    int currentSelectedLabels = 0;

    Vector<Integer> selectedLabels = new Vector<>();

    do {
        if (!selectedLabels.contains(pairs.get(0).label1)) {
            selectedLabels.add(pairs.get(0).label1);
            currentSelectedLabels++;
        }

        if (currentSelectedLabels < numLabels) {
            if (!selectedLabels.contains(pairs.get(0).label2)) {
                selectedLabels.add(pairs.get(0).label2);
                currentSelectedLabels++;
            }
        }

        pairs.remove(pairs.get(0));
    } while ((pairs.size() > 0) && (currentSelectedLabels < numLabels));

    int[] labelIndices = new int[n];

    String s;

    if (selectedLabels.size() < n) {
        int[] selectedsFreq = new int[dataset.getNumLabels()];
        for (int i = 0; i < selectedsFreq.length; i++) {
            selectedsFreq[i] = i;
        }

        int i = 0;
        do {
            if (!selectedLabels.contains((int) selectedsFreq[i])) {
                selectedLabels.add(selectedsFreq[i]);
            }

            i++;
        } while (selectedLabels.size() < n);
    }

    for (int i = 0; i < selectedLabels.size(); i++) {
        s = jTableHeatmap.getColumnName(selectedLabels.get(i));
        if (s != null) {
            labelIndices[i] = selectedLabels.get(i);
        }
    }

    return labelIndices;
}