Java Code Examples for java.util.TreeMap#pollFirstEntry()

The following examples show how to use java.util.TreeMap#pollFirstEntry() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
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;
}
 
Example 2
/**
 * 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);
}
 
Example 3
Source Project: j2objc   File: TreeMapTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 4
/**
 * 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;
}
 
Example 5
Source Project: javaide   File: DexMerger.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source Project: openmeetings   File: BackupImport.java    License: Apache License 2.0 5 votes vote down vote up
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);
}