Here you can find the source of createTargetSorted(List
public static <T> List<Double> createTargetSorted(List<T> labels, Comparator<T> comp)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { public static <T> List<Double> createTargetSorted(List<T> labels, Comparator<T> comp) { return createTargetSorted(labels, new HashMap<T, Double>(), comp); }/* w ww . j a v a 2s.com*/ /** * Convert a list of arbitary labels into al list of Doubles which is used as the training target in LibSVM/LINEAR * * @param labels, this list needs to be sortable, i.e. we need a Comparator for T * @param labelMap * @return */ public static <T> List<Double> createTargetSorted(List<T> labels, Map<T, Double> labelMap, Comparator<T> comp) { List<T> sorted = new ArrayList<T>(labels); Collections.sort(sorted, comp); double t = 0; for (T label : sorted) { if (!labelMap.containsKey(label)) { t += 1; labelMap.put(label, t); } } return createTarget(labels, labelMap); } public static <T> List<Double> createTarget(List<T> labels) { return createTarget(labels, new HashMap<T, Double>()); } public static <T> List<Double> createTarget(List<T> labels, Map<T, Double> labelMap) { List<Double> target = new ArrayList<Double>(); double t = 0; for (T label : labels) { if (!labelMap.containsKey(label)) { t += 1; labelMap.put(label, t); } target.add((double) labelMap.get(label)); } return target; } }