List of usage examples for java.util Collections sort
@SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> void sort(List<T> list, Comparator<? super T> c)
From source file:Main.java
public static <K> List<K> sorted(Collection<K> collection, Comparator<? super K> order) { List<K> requiredTicketIds = new ArrayList<>(collection); Collections.sort(requiredTicketIds, order); return requiredTicketIds; }
From source file:Main.java
/** Returns an alphabetically sorted copy of vector. */ public static void sortListAlphabetically(List list) { Collections.sort(list, new Comparator() { public int compare(Object a, Object b) { String textA = a.toString(); String textB = b.toString(); return textA.compareToIgnoreCase(textB); }/*w ww . j av a 2 s . c o m*/ }); }
From source file:Main.java
public static Point findBestPreviewSizeValue(Camera.Parameters parameters, Point screenResolution) { List<Camera.Size> rawSupportedSizes = parameters.getSupportedPreviewSizes(); if (rawSupportedSizes == null) { Log.w(TAG, "Device returned no supported preview sizes; using default"); Camera.Size defaultSize = parameters.getPreviewSize(); if (defaultSize == null) { throw new IllegalStateException("Parameters contained no preview size!"); }/*from w w w . ja v a 2s . co m*/ return new Point(defaultSize.width, defaultSize.height); } // Sort by size, descending List<Camera.Size> supportedPreviewSizes = new ArrayList<Camera.Size>(rawSupportedSizes); Collections.sort(supportedPreviewSizes, new Comparator<Camera.Size>() { @Override public int compare(Camera.Size a, Camera.Size b) { int aPixels = a.height * a.width; int bPixels = b.height * b.width; if (bPixels < aPixels) { return -1; } if (bPixels > aPixels) { return 1; } return 0; } }); if (Log.isLoggable(TAG, Log.INFO)) { StringBuilder previewSizesString = new StringBuilder(); for (Camera.Size supportedPreviewSize : supportedPreviewSizes) { previewSizesString.append(supportedPreviewSize.width).append('x') .append(supportedPreviewSize.height).append(' '); } Log.i(TAG, "Supported preview sizes: " + previewSizesString); } double screenAspectRatio = (double) screenResolution.x / (double) screenResolution.y; // Remove sizes that are unsuitable Iterator<Camera.Size> it = supportedPreviewSizes.iterator(); while (it.hasNext()) { Camera.Size supportedPreviewSize = it.next(); int realWidth = supportedPreviewSize.width; int realHeight = supportedPreviewSize.height; if (realWidth * realHeight < MIN_PREVIEW_PIXELS) { it.remove(); continue; } boolean isCandidatePortrait = realWidth < realHeight; int maybeFlippedWidth = isCandidatePortrait ? realHeight : realWidth; int maybeFlippedHeight = isCandidatePortrait ? realWidth : realHeight; double aspectRatio = (double) maybeFlippedWidth / (double) maybeFlippedHeight; double distortion = Math.abs(aspectRatio - screenAspectRatio); if (distortion > MAX_ASPECT_DISTORTION) { it.remove(); continue; } if (maybeFlippedWidth == screenResolution.x && maybeFlippedHeight == screenResolution.y) { Point exactPoint = new Point(realWidth, realHeight); Log.i(TAG, "Found preview size exactly matching screen size: " + exactPoint); return exactPoint; } } // If no exact match, use largest preview size. This was not a great // idea on older devices because // of the additional computation needed. We're likely to get here on // newer Android 4+ devices, where // the CPU is much more powerful. if (!supportedPreviewSizes.isEmpty()) { Camera.Size largestPreview = supportedPreviewSizes.get(0); Point largestSize = new Point(largestPreview.width, largestPreview.height); Log.i(TAG, "Using largest suitable preview size: " + largestSize); return largestSize; } // If there is nothing at all suitable, return current preview size Camera.Size defaultPreview = parameters.getPreviewSize(); if (defaultPreview == null) { throw new IllegalStateException("Parameters contained no preview size!"); } Point defaultSize = new Point(defaultPreview.width, defaultPreview.height); Log.i(TAG, "No suitable preview sizes, using default: " + defaultSize); return defaultSize; }
From source file:Main.java
/** * Group Data into {@link List}.//from w ww . j a v a 2 s.c om * @param <T> Type of object * @param list {@link List} * @param sortBy {@link Comparator} * @param groupBy {@link Comparator} * @return {@link List} */ public static <T> List<List<T>> groupBy(final List<T> list, final Comparator<T> sortBy, final Comparator<T> groupBy) { T prev = null; List<T> currentlist = new ArrayList<>(); List<List<T>> results = new ArrayList<>(); results.add(currentlist); List<T> newlist = new ArrayList<>(list); Collections.sort(newlist, sortBy); Iterator<T> itr = newlist.iterator(); while (itr.hasNext()) { T curr = itr.next(); if (prev != null) { int result = groupBy.compare(curr, prev); if (result != 0) { currentlist = new ArrayList<>(); results.add(currentlist); } } currentlist.add(curr); prev = curr; } return results; }
From source file:MapUtils.java
/** * Sorts map entries by value in ascending order. * //www . j a v a 2 s .c om * @param <K> * map keys type * @param <V> * map values type * @param entries * @return */ private static <K, V extends Comparable<V>> List<Entry<K, V>> sortEntriesByValue(Set<Entry<K, V>> entries) { List<Entry<K, V>> sortedEntries = new ArrayList<Entry<K, V>>(entries); Collections.sort(sortedEntries, new ValueComparator<V>()); return sortedEntries; }
From source file:Main.java
public TableColumn[] getColumnsInModel(JTable table) { List result = new ArrayList(); for (Enumeration e = table.getColumnModel().getColumns(); e.hasMoreElements();) { result.add((TableColumn) e.nextElement()); }// w ww . j av a 2 s .com Collections.sort(result, new TableColumnComparator()); return (TableColumn[]) result.toArray(new TableColumn[result.size()]); }
From source file:Main.java
public static List<String> sortSimpleName(List<String> list) { if (list != null && list.size() > 0) { Collections.sort(list, SIMPLE_NAME_COMPARATOR); }// w w w. ja v a2s . c om return list; }
From source file:JavaSort.java
public JavaSort() { Vector list = new Vector(); list.add("\u00e4pple"); list.add("banan"); list.add("p\u00e4ron"); list.add("orange"); // Obtain a Swedish collator Collator collate = Collator.getInstance(new Locale("sv", "")); Collections.sort(list, collate); StringBuffer result = new StringBuffer(); for (int i = 0; i < list.size(); i++) { result.append(list.elementAt(i)); result.append(" "); }/*from w ww . j a v a 2 s .c o m*/ add(new JLabel(result.toString())); }
From source file:Main.java
/** * Compare two lists using the {@code comparator} for all comparisons (not using the equals() operator) * @param lhs Left hand side/*from w w w. jav a2 s . c om*/ * @param rhs Right hand side * @param comparator Comparator which will be used for all comparisons (equals() on objects will not be used) * @return True if {@code lhs} == {@code rhs} according to {@code comparator}. False otherwise. */ public static <T> boolean equals(List<T> lhs, List<T> rhs, Comparator<? super T> comparator) { final int lhsSize = lhs.size(); if (lhsSize != rhs.size()) { return false; } // Don't use a TreeSet to do the comparison. We want to force the comparator // to be used instead of the object's equals() Collections.sort(lhs, comparator); Collections.sort(rhs, comparator); for (int i = 0; i < lhsSize; ++i) { if (comparator.compare(lhs.get(i), rhs.get(i)) != 0) { return false; } } return true; }
From source file:Main.java
/** * /*from w w w. j a v a2 s . c om*/ * @param <T> * @param list * @param comparator * @return * @author sabuj.das */ public static <T> T lowestValue(List<T> list, Comparator<T> comparator) { if (list == null || list.isEmpty()) { return null; } Collections.sort(list, comparator); return list.get(0); }