List of usage examples for java.util Comparator Comparator
Comparator
From source file:Main.java
/** * Given an un-encoded URI query string, this will return a normalized, properly encoded URI query string. * <b>Important:</b> This method uses java's URLEncoder, which returns things that are * application/x-www-form-urlencoded, instead of things that are properly octet-esacped as the URI spec * requires. As a result, some substitutions are made to properly translate space characters to meet the * URI spec./*w w w .j av a2s . c om*/ * @param queryString * @return */ private static String normalizeQueryString(String queryString) throws UnsupportedEncodingException { if ("".equals(queryString) || queryString == null) return queryString; String[] pieces = queryString.split("&"); HashMap<String, String> kvp = new HashMap<String, String>(); StringBuffer builder = new StringBuffer(""); for (int x = 0; x < pieces.length; x++) { String[] bs = pieces[x].split("=", 2); bs[0] = URLEncoder.encode(bs[0], "UTF-8"); if (bs.length == 1) kvp.put(bs[0], null); else { kvp.put(bs[0], URLEncoder.encode(bs[1], "UTF-8").replaceAll("\\+", "%20")); } } // Sort the keys alphabetically, ignoring case. ArrayList<String> keys = new ArrayList<String>(kvp.keySet()); Collections.sort(keys, new Comparator<String>() { public int compare(String o1, String o2) { return o1.compareToIgnoreCase(o2); } }); // With the alphabetic list of parameter names, re-build the query string. for (int x = 0; x < keys.size(); x++) { // Some parameters have no value, and are simply present. If so, we put null in kvp, // and we just put the parameter name, no "=value". if (kvp.get(keys.get(x)) == null) builder.append(keys.get(x)); else builder.append(keys.get(x) + "=" + kvp.get(keys.get(x))); if (x < (keys.size() - 1)) builder.append("&"); } return builder.toString(); }
From source file:Main.java
/** * Constructs a comparator and calls findInsertionIndex(List<E> , E, * Comparator<E>) to find the insertion index. * //from w ww . j a va 2 s .c o m * @param <E> * type of the object * @param list * list where the element should be inserted. * @param value * element that should be inserted. * @return index (place) where the element should be inserted. */ public static <E extends Comparable<E>> int findInsertionIndex(List<E> list, E object) { Comparator<E> comparator = new Comparator<E>() { @Override public int compare(E o1, E o2) { return o1.compareTo(o2); } }; return findInsertionIndex(list, object, comparator); }
From source file:eu.scidipes.toolkits.pawebapp.util.FrameworkUtils.java
/** * Retrieves a <code>List</code> of {@link RepInfoCategory}s via the Framework which is sorted by ascending * alphabetical order based on the category's name. * /*from w w w. j av a 2s .c o m*/ * @return a list of categories sorted by name */ public static List<RepInfoCategory> getSortedCategories() { final Comparator<RepInfoCategory> ricComparator = new Comparator<RepInfoCategory>() { @Override public int compare(final RepInfoCategory cat1, final RepInfoCategory cat2) { return cat1.getName().compareTo(cat2.getName()); } }; final List<RepInfoCategory> categories = new ArrayList<>(FrameworkWrapper.getAllPredefinedCategories()); Collections.sort(categories, ricComparator); return categories; }
From source file:com.github.jinahya.sql.database.metadata.bind.Column.java
public static Comparator<Column> natural() { return new Comparator<Column>() { @Override/* w ww . ja va 2 s.c o m*/ public int compare(final Column o1, final Column o2) { // by TABLE_CAT,TABLE_SCHEM, TABLE_NAME, and ORDINAL_POSITION. return new CompareToBuilder().append(o1.getTableCat(), o2.getTableCat()) .append(o1.getTableSchem(), o2.getTableSchem()).append(o1.getTableName(), o2.getTableName()) .append(o1.getOrdinalPosition(), o2.getOrdinalPosition()).build(); } }; }
From source file:net.firejack.platform.service.registry.helper.PackageVersionHelper.java
public static FileInfo[] sortingByNameLikeNumber(FileInfo[] files, boolean desc) { Arrays.sort(files, new Comparator() { public int compare(final Object o1, final Object o2) { Integer i1 = Integer.parseInt(((FileInfo) o1).getFilename()); Integer i2 = Integer.parseInt(((FileInfo) o2).getFilename()); return i1.compareTo(i2); }// w ww . j a v a2 s . c om }); if (desc) { org.apache.commons.lang.ArrayUtils.reverse(files); } return files; }
From source file:com.model.database.Databases.java
public static HashMap<String, String[]> getDbInfo() { HashMap<String, String[]> infoMap = new HashMap<String, String[]>(); try {//from ww w .ja va 2 s .c o m path = Databases.class.getProtectionDomain().getCodeSource().getLocation().getPath(); BufferedReader reader = new BufferedReader( new FileReader(path + "../../src/main/resources/admin/resources/dbInfo.csv")); String[] keys = reader.readLine().trim().split(","); ArrayList<String[]> elem = new ArrayList<String[]>(); String line = reader.readLine(); while (line != null && !line.equals("")) { elem.add(line.trim().split(",")); line = reader.readLine(); } Collections.sort(elem, new Comparator<String[]>() { public int compare(String[] l1, String[] l2) { return Integer.parseInt(l1[0]) - Integer.parseInt(l2[0]); } }); reader.close(); for (int i = 0; i < keys.length; i++) { String[] tmp = new String[elem.size()]; for (int j = 0; j < elem.size(); j++) { tmp[j] = elem.get(j)[i]; } infoMap.put(keys[i], tmp); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("The database size is : " + String.valueOf(infoMap.get("databaseIndex").length)); return infoMap; }
From source file:com.reactive.hzdfs.dll.JarClassLoader.java
private static TreeSet<String> scanForPackages(String path) throws IOException { try (JarFile file = new JarFile(path)) { TreeSet<String> packages = new TreeSet<>(new Comparator<String>() { @Override//from w w w . j av a 2s. c om public int compare(String o1, String o2) { if (o2.length() > o1.length() && o2.contains(o1)) return -1; else if (o2.length() < o1.length() && o1.contains(o2)) return 1; else return o1.compareTo(o2); } }); for (Enumeration<JarEntry> entries = file.entries(); entries.hasMoreElements();) { JarEntry entry = entries.nextElement(); String name = entry.getName(); if (name.endsWith(".class")) { String fqcn = ClassUtils.convertResourcePathToClassName(name); fqcn = StringUtils.delete(fqcn, ".class"); packages.add(ClassUtils.getPackageName(fqcn)); } } return packages; } }
From source file:com.indeed.imhotep.web.RunningController.java
@RequestMapping("/running") @ResponseBody// ww w .ja v a 2 s . co m public State handle() { List<ExecutionManager.QueryTracker> queries = executionManager.getRunningQueries(); Collections.sort(queries, new Comparator<ExecutionManager.QueryTracker>() { @Override public int compare(ExecutionManager.QueryTracker o1, ExecutionManager.QueryTracker o2) { return o1.getStartedTime().compareTo(o2.getStartedTime()); } }); return new State(queries); }
From source file:com.xebia.devradar.Timeline.java
public void update(Collection<Event> workspaceEvents) { List<Event> workspaceSortedEvents = new ArrayList<Event>(workspaceEvents); Collections.sort(workspaceSortedEvents, Collections.reverseOrder(new Comparator<Event>() { @Override//w w w. ja va2 s. c o m public int compare(Event o1, Event o2) { return new CompareToBuilder().append(o1.timestamp, o2.timestamp).toComparison(); } })); if (workspaceSortedEvents.size() < MAX_SIZE) { this.events = workspaceSortedEvents; } else { this.events = workspaceSortedEvents.subList(0, MAX_SIZE); } }
From source file:edu.byu.nlp.util.Counters.java
/** * Get the n entries with the largest value based on some comparator. * Used by Counter's argMaxList method. *///from w w w . ja va2 s. c om public static <E, V extends Comparable<V>> List<E> argMaxList(Set<Entry<E, V>> entrySet, int topn, RandomGenerator rnd) { topn = (topn > 0) ? topn : entrySet.size(); List<Entry<E, V>> entries = Lists.newArrayList(entrySet); // shuffle to ensure that ties are broken randomly if (rnd != null) { Collections.shuffle(entries, new RandomAdaptor(rnd)); } // sort to ensure most voted-for options are at the beginning Collections.sort(entries, new Comparator<Entry<E, V>>() { @Override public int compare(Entry<E, V> o1, Entry<E, V> o2) { return (o2.getValue()).compareTo(o1.getValue()); // descending order } }); // pull out the top n values List<E> vals = Lists.newArrayList(); for (int i = 0; i < Math.min(topn, entries.size()); i++) { vals.add(entries.get(i).getKey()); } return vals; }