Java Code Examples for org.apache.commons.collections4.trie.PatriciaTrie#prefixMap()

The following examples show how to use org.apache.commons.collections4.trie.PatriciaTrie#prefixMap() . 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
Source File: PatriciaTrieTest.java    From JQF with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Fuzz
public void testPrefixMap(HashMap<String, Integer> map, String prefix) {
    assumeTrue(prefix.length() > 0);
    // Create new trie with input `map`
    PatriciaTrie trie = new PatriciaTrie(map);
    // Get sub-map whose keys start with `prefix`
    Map prefixMap = trie.prefixMap(prefix);
    // Ensure that it contains all keys that start with `prefix`
    for (String key : map.keySet()) {
        if (key.startsWith(prefix)) {
            assertTrue(prefixMap.containsKey(key));
        }
    }
}
 
Example 2
Source File: OrgMinimalPrefixGenerator.java    From act with GNU General Public License v3.0 5 votes vote down vote up
public OrgMinimalPrefixGenerator(Iterator<Organism> orgIterator) {
  Map<String, Long> orgMap = new HashMap<>();

  while (orgIterator.hasNext()) {
    Organism org = orgIterator.next();
    orgMap.put(org.getName(), 1L);
  }

  PatriciaTrie orgPrefixTrie = new PatriciaTrie<>(orgMap);
  orgNameToMinimalPrefix = new HashMap<>();

  while (orgPrefixTrie.size() != 0) {
    String firstKey = (String) orgPrefixTrie.firstKey();
    orgNameToMinimalPrefix.put(firstKey, firstKey);
    orgPrefixTrie.remove(firstKey);

    SortedMap<String, Long> keyPrefixMap = orgPrefixTrie.prefixMap(firstKey);

    List<String> namesToRemove = new ArrayList<>();

    for (String orgWithPrefix : keyPrefixMap.keySet()) {
      orgNameToMinimalPrefix.put(orgWithPrefix, firstKey);
      namesToRemove.add(orgWithPrefix);
    }

    for (String nameToRemove : namesToRemove) {
      orgPrefixTrie.remove(nameToRemove);
    }
  }
}