本文簡單介紹下apache collection4中的PatriciaTrie的使用。算法
Trie樹,又稱字典樹,單詞查找樹或者前綴樹,是一種用於快速檢索的多叉樹結構。apache
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.1</version> </dependency>
@Test public void testContains(){ PatriciaTrie<Double> t = new PatriciaTrie<Double>(); t.put("ronak", 100.0); t.put("ronald", 90.0); t.put("rat", 50.0); t.put("robert", 200.0); t.put("bat", 44.0); t.put("batman", 440.0); System.out.println(t.containsKey("ronak")); System.out.println(t.selectKey("ro")); System.out.println(t.prefixMap("r")); System.out.println(t.prefixMap("ro")); System.out.println(t.prefixMap("ron")); }
輸出數據結構
true robert {rat=50.0, robert=200.0, ronak=100.0, ronald=90.0} {robert=200.0, ronak=100.0, ronald=90.0} {ronak=100.0, ronald=90.0}