下面列出了java.util.TreeMap#pollFirstEntry ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public ItemSet findItemSet(String name) throws ItemSetNotFoundException {
ItemSet set = itemSets.get(name);
if (set == null) {
TreeMap<Integer, String> lvs = new TreeMap<Integer, String>();
for (String id : names) {
lvs.put(levenshteinDistance(name, id, 2, 1, 1), id);
}
ArrayList<String> suggestions = new ArrayList<String>();
Map.Entry<Integer, String> it;
while ((it = lvs.pollFirstEntry()) != null && suggestions.size() < 3) {
if (it.getKey() < 8)
suggestions.add(it.getValue());
}
Collections.reverse(suggestions);
throw new ItemSetNotFoundException(suggestions);
}
return set;
}
/**
* pollFirstEntry returns entries in order
*/
public void testPollFirstEntry() {
TreeMap map = map5();
Map.Entry e = map.pollFirstEntry();
assertEquals(one, e.getKey());
assertEquals("A", e.getValue());
e = map.pollFirstEntry();
assertEquals(two, e.getKey());
map.put(one, "A");
e = map.pollFirstEntry();
assertEquals(one, e.getKey());
assertEquals("A", e.getValue());
e = map.pollFirstEntry();
assertEquals(three, e.getKey());
map.remove(four);
e = map.pollFirstEntry();
assertEquals(five, e.getKey());
try {
e.setValue("A");
shouldThrow();
} catch (UnsupportedOperationException success) {}
e = map.pollFirstEntry();
assertNull(e);
}
/**
* pollFirstEntry returns entries in order
*/
public void testPollFirstEntry() {
TreeMap map = map5();
Map.Entry e = map.pollFirstEntry();
assertEquals(one, e.getKey());
assertEquals("A", e.getValue());
e = map.pollFirstEntry();
assertEquals(two, e.getKey());
map.put(one, "A");
e = map.pollFirstEntry();
assertEquals(one, e.getKey());
assertEquals("A", e.getValue());
e = map.pollFirstEntry();
assertEquals(three, e.getKey());
map.remove(four);
e = map.pollFirstEntry();
assertEquals(five, e.getKey());
try {
e.setValue("A");
shouldThrow();
} catch (UnsupportedOperationException success) {}
e = map.pollFirstEntry();
assertNull(e);
}
/**
* Merges already-sorted sections, reading one value from each dex into memory
* at a time.
*/
public final void mergeSorted() {
TableOfContents.Section[] sections = new TableOfContents.Section[dexes.length];
Dex.Section[] dexSections = new Dex.Section[dexes.length];
int[] offsets = new int[dexes.length];
int[] indexes = new int[dexes.length];
// values contains one value from each dex, sorted for fast retrieval of
// the smallest value. The list associated with a value has the indexes
// of the dexes that had that value.
TreeMap<T, List<Integer>> values = new TreeMap<T, List<Integer>>();
for (int i = 0; i < dexes.length; i++) {
sections[i] = getSection(dexes[i].getTableOfContents());
dexSections[i] = sections[i].exists() ? dexes[i].open(sections[i].off) : null;
// Fill in values with the first value of each dex.
offsets[i] = readIntoMap(
dexSections[i], sections[i], indexMaps[i], indexes[i], values, i);
}
getSection(contentsOut).off = out.getPosition();
int outCount = 0;
while (!values.isEmpty()) {
Map.Entry<T, List<Integer>> first = values.pollFirstEntry();
for (Integer dex : first.getValue()) {
updateIndex(offsets[dex], indexMaps[dex], indexes[dex]++, outCount);
// Fetch the next value of the dexes we just polled out
offsets[dex] = readIntoMap(dexSections[dex], sections[dex],
indexMaps[dex], indexes[dex], values, dex);
}
write(first.getKey());
outCount++;
}
getSection(contentsOut).size = outCount;
}
/**
* Merges already-sorted sections, reading one value from each dex into memory
* at a time.
*/
public final void mergeSorted() {
TableOfContents.Section[] sections = new TableOfContents.Section[dexes.length];
Dex.Section[] dexSections = new Dex.Section[dexes.length];
int[] offsets = new int[dexes.length];
int[] indexes = new int[dexes.length];
// values contains one value from each dex, sorted for fast retrieval of
// the smallest value. The list associated with a value has the indexes
// of the dexes that had that value.
TreeMap<T, List<Integer>> values = new TreeMap<T, List<Integer>>();
for (int i = 0; i < dexes.length; i++) {
sections[i] = getSection(dexes[i].getTableOfContents());
dexSections[i] = sections[i].exists() ? dexes[i].open(sections[i].off) : null;
// Fill in values with the first value of each dex.
offsets[i] = readIntoMap(
dexSections[i], sections[i], indexMaps[i], indexes[i], values, i);
}
getSection(contentsOut).off = out.getPosition();
int outCount = 0;
while (!values.isEmpty()) {
Map.Entry<T, List<Integer>> first = values.pollFirstEntry();
for (Integer dex : first.getValue()) {
updateIndex(offsets[dex], indexMaps[dex], indexes[dex]++, outCount);
// Fetch the next value of the dexes we just polled out
offsets[dex] = readIntoMap(dexSections[dex], sections[dex],
indexMaps[dex], indexes[dex], values, dex);
}
write(first.getKey());
outCount++;
}
getSection(contentsOut).size = outCount;
}
private <T extends BaseFileItem> void saveTree(
File baseDir
, String fileName
, String listNodeName
, String nodeName
, Class<T> clazz
, Map<Long, Long> folders
, Consumer<T> save
)
{
TreeMap<Long, T> items = new TreeMap<>();
readList(baseDir, fileName, listNodeName, nodeName, clazz, f -> {
items.put(f.getId(), f);
}, false);
FileTree<T> tree = new FileTree<>();
TreeMap<Long, T> remain = new TreeMap<>();
int counter = items.size(); //max iterations
while (counter > 0 && !items.isEmpty()) {
Entry<Long, T> e = items.pollFirstEntry();
if (e == null) {
break;
} else {
if (!tree.add(e.getValue())) {
remain.put(e.getKey(), e.getValue());
}
}
if (items.isEmpty()) {
counter = Math.min(counter - 1, remain.size());
items.putAll(remain);
remain.clear();
}
}
remain.entrySet().forEach(e -> log.warn("Doungling file/recording: {}", e.getValue()));
tree.process(f -> isInvalidFile(f, folders), save);
}