List of usage examples for java.util TreeMap comparator
Comparator comparator
To view the source code for java.util TreeMap comparator.
Click Source Link
From source file:Main.java
public static void main(String[] args) { TreeMap<Integer, String> treemap = new TreeMap<Integer, String>(); // populating tree map treemap.put(2, "two"); treemap.put(1, "one"); treemap.put(3, "three"); treemap.put(6, "six"); treemap.put(5, "from java2s.com"); // using comparator Comparator comp = treemap.comparator(); System.out.println("Following natural ordering"); System.out.println("Comparator value: " + comp); }
From source file:gsn.storage.SQLUtils.java
/** * Table renaming, note that the renameMapping should be a tree map. This * method gets a sql query and changes the table names using the mappings * provided in the second argument.<br> * //from w w w . j a v a 2 s.c o m * @param query * @param renameMapping * @return */ public static StringBuilder newRewrite(CharSequence query, TreeMap<CharSequence, CharSequence> renameMapping) { // Selecting strings between pair of "" : (\"[^\"]*\") // Selecting tableID.tableName or tableID.* : (\\w+(\\.(\w+)|\\*)) // The combined pattern is : (\"[^\"]*\")|(\\w+\\.((\\w+)|\\*)) Pattern pattern = Pattern.compile("(\"[^\"]*\")|((\\w+)(\\.((\\w+)|\\*)))", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(query); StringBuffer result = new StringBuffer(); if (!(renameMapping.comparator() instanceof CaseInsensitiveComparator)) throw new RuntimeException("Query rename needs case insensitive treemap."); while (matcher.find()) { if (matcher.group(2) == null) continue; String tableName = matcher.group(3); CharSequence replacement = renameMapping.get(tableName); // $4 means that the 4th group of the match should be appended to the // string (the forth group contains the field name). if (replacement != null) matcher.appendReplacement(result, new StringBuilder(replacement).append("$4").toString()); } String toReturn = matcher.appendTail(result).toString().toLowerCase(); //TODO " from " has to use regular expressions because now from is separated through space which is not always the case, for instance if the user uses \t(tab) for separating "from" from the rest of the query, then we get exception. The same issue with other sql keywords in this method. int indexOfFrom = toReturn.indexOf(" from ") >= 0 ? toReturn.indexOf(" from ") + " from ".length() : 0; int indexOfWhere = (toReturn.lastIndexOf(" where ") > 0 ? (toReturn.lastIndexOf(" where ")) : toReturn.length()); String selection = toReturn.substring(indexOfFrom, indexOfWhere); Pattern fromClausePattern = Pattern.compile("\\s*(\\w+)\\s*", Pattern.CASE_INSENSITIVE); Matcher fromClauseMather = fromClausePattern.matcher(selection); result = new StringBuffer(); while (fromClauseMather.find()) { if (fromClauseMather.group(1) == null) continue; String tableName = fromClauseMather.group(1); CharSequence replacement = renameMapping.get(tableName); if (replacement != null) fromClauseMather.appendReplacement(result, replacement.toString() + " "); } String cleanFromClause = fromClauseMather.appendTail(result).toString(); String finalResult = StringUtils.replace(toReturn, selection, cleanFromClause); return new StringBuilder(finalResult); }
From source file:com.google.gwt.emultest.java.util.TreeMapTest.java
/** * Test method for 'java.util.TreeMap.TreeMap(SortedMap)'. * * @see java.util.TreeMap#TreeMap(SortedMap) *///from w w w .j av a 2 s . c o m public void testConstructor_SortedMap() { K[] keys = getKeys(); V[] values = getValues(); SortedMap<K, V> sourceMap = new TreeMap<K, V>(); _assertEmpty(sourceMap); // populate the source map sourceMap.put(keys[0], values[0]); sourceMap.put(keys[1], values[1]); sourceMap.put(keys[2], values[2]); TreeMap<K, V> copyConstructed = new TreeMap<K, V>(sourceMap); _assertEquals(sourceMap, copyConstructed); Comparator<K> comp = Collections.reverseOrder(getComparator()); TreeMap<K, V> reversedTreeMap = new TreeMap<K, V>(comp); reversedTreeMap.put(keys[0], values[0]); reversedTreeMap.put(keys[1], values[1]); TreeMap<K, V> anotherTreeMap = new TreeMap<K, V>(reversedTreeMap); assertTrue(anotherTreeMap.comparator() == comp); assertEquals(keys[1], anotherTreeMap.firstKey()); assertEquals(keys[0], anotherTreeMap.lastKey()); }
From source file:com.google.gwt.emultest.java.util.TreeMapTest.java
/** * Test method for 'java.util.TreeMap.TreeMap(Comparator)'. * * @see java.util.TreeMap#TreeMap(Comparator) *//* w w w. ja v a 2 s . com*/ public void testConstructor_comparator() { TreeMap<K, V> treeMap = new TreeMap<K, V>(getComparator()); _assertEmpty(treeMap); if (isNaturalOrder()) { assertNull(treeMap.comparator()); } else { assertSame(getComparator(), treeMap.comparator()); } }
From source file:com.google.gwt.emultest.java.util.TreeMapTest.java
/** * Test method for 'java.util.SortedMap.comparator()'. * * @see java.util.SortedMap#comparator() */// w w w . j a v a 2 s .co m public void testComparator() { SortedMap<K, V> sortedMap = createNavigableMap(); if (isNaturalOrder()) { assertEquals(null, sortedMap.comparator()); } else { assertEquals(getComparator(), sortedMap.comparator()); } TreeMap<K, V> treeMap = new TreeMap<>(); TreeMap<K, V> secondTreeMap = new TreeMap<>(treeMap); assertNull(treeMap.comparator()); assertNull(secondTreeMap.comparator()); treeMap = new TreeMap<>((Comparator<? super K>) null); secondTreeMap = new TreeMap<>(treeMap); assertNull(treeMap.comparator()); assertNull(secondTreeMap.comparator()); final Comparator<? super K> customComparator = new Comparator<K>() { @Override public int compare(K o1, K o2) { return o1.compareTo(o2); } }; treeMap = new TreeMap<>(customComparator); secondTreeMap = new TreeMap<>(treeMap); assertSame(customComparator, treeMap.comparator()); assertSame(customComparator, secondTreeMap.comparator()); treeMap = new TreeMap<>(new HashMap<K, V>()); secondTreeMap = new TreeMap<>(treeMap); assertNull(treeMap.comparator()); assertNull(secondTreeMap.comparator()); }