List of usage examples for org.apache.commons.collections4 MultiMap entrySet
Set<Map.Entry<K, V>> entrySet();
From source file:name.abhijitsarkar.programminginterviews.hashtables.PracticeQuestionsCh12.java
@SuppressWarnings("unchecked") public static List<List<String>> anagrams(final List<String> dictionary) { final MultiMap<Integer, Integer> anagramMap = new MultiValueMap<Integer, Integer>(); char[] arr = null; int hashCode = -1; for (int i = 0; i < dictionary.size(); ++i) { arr = dictionary.get(i).toCharArray(); Arrays.sort(arr);//w ww . j a v a2 s . com hashCode = String.valueOf(arr).hashCode(); anagramMap.put(hashCode, i); } final List<List<String>> anagrams = new ArrayList<>(); final Set<Entry<Integer, Object>> anagramIndices = anagramMap.entrySet(); List<Integer> aSet = null; for (final Object o : anagramIndices) { aSet = (List<Integer>) ((Entry<Integer, Object>) o).getValue(); if (aSet.size() > 1) { final List<String> an = new ArrayList<>(); for (final int i : aSet) { an.add(dictionary.get(i)); } anagrams.add(an); } } return anagrams; }
From source file:org.shaman.terrain.vegetation.ImpositorCreator.java
@Override public void simpleInitApp() { File folder = new File(OUTPUT_FOLDER); if (folder.exists()) { assert (folder.isDirectory()); } else {/*from w ww. j ava 2s . c o m*/ folder.mkdir(); } List<TreeInfo> trees = new ArrayList<>(); List<String> errors = new ArrayList<>(); //collect input MultiMap<Biome, Pair<String, Float>> treeDef = new MultiValueMap<>(); Map<String, Float> probabilities = new HashMap<>(); try (BufferedReader in = new BufferedReader(new FileReader(TREE_DEF_FILE))) { in.readLine(); //skip head while (true) { String line = in.readLine(); if (line == null) break; String[] parts = line.split(";"); Biome biome = Biome.valueOf(parts[0]); String treeName = parts[1]; float prob = Float.parseFloat(parts[2]) / 100f; treeDef.put(biome, new ImmutablePair<>(treeName, prob)); Float p = probabilities.get(treeName); if (p == null) { p = prob; } else { p += prob; } probabilities.put(treeName, p); } } catch (IOException ex) { Logger.getLogger(ImpositorCreator.class.getName()).log(Level.SEVERE, null, ex); } LOG.info("TreeDef: " + treeDef); LOG.info("TreeProb: " + probabilities); //create trees treeCreation: for (Map.Entry<String, Float> entry : probabilities.entrySet()) { try { String treeName = entry.getKey(); List<TreeInfo> treeInfos = new ArrayList<>(); float prob = entry.getValue(); if (prob <= MAX_PROP) { TreeInfo info = createTree(null, treeName, 0, 1); if (info != null) { treeInfos.add(info); } else { continue treeCreation; } } else { int n = (int) Math.ceil(prob / MAX_PROP); float p = prob / n; for (int i = 0; i < n; ++i) { TreeInfo info = createTree(null, treeName, i, p); if (info != null) { treeInfos.add(info); } else { continue treeCreation; } } } //create tree infos for (Map.Entry<Biome, Object> treeDefE : treeDef.entrySet()) { for (Pair<String, Float> v : (Collection<Pair<String, Float>>) treeDefE.getValue()) { if (treeName.equals(v.getKey())) { for (TreeInfo i : treeInfos) { TreeInfo i2 = i.clone(); i2.biome = treeDefE.getKey(); i2.probability = v.getValue() / treeInfos.size(); trees.add(i2); } } } } } catch (IOException ex) { Logger.getLogger(ImpositorCreator.class.getName()).log(Level.SEVERE, null, ex); } } System.out.println("trees:"); for (TreeInfo t : trees) { System.out.println(" " + t); } LOG.log(Level.INFO, "save tree infos, {0} trees in total", trees.size()); try (ObjectOutputStream out = new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream(TREE_DATA_FILE)))) { out.writeObject(trees); } catch (IOException ex) { Logger.getLogger(ImpositorCreator.class.getName()).log(Level.SEVERE, null, ex); } LOG.info("done"); stop(); }
From source file:org.shaman.terrain.vegetation.TreePlanter.java
private void plantTrees() { // Box testMesh = new Box(1, 1, 1); // Material testMat = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md"); // testMat.setColor("Color", ColorRGBA.Red); //bucketing for speed improvement MultiMap<TreeInfo, TreeNode> nodes = new MultiValueMap<>(); //first, simple planting algorithm float density = 0.2f / size; Random rand = new Random(); Biome[] allBiomes = Biome.values();//from w ww . java 2 s.c o m for (int x = 0; x < biomes.getSize(); ++x) { for (int y = 0; y < biomes.getSize(); ++y) { if (map.getHeightAt(x, y) <= 0) { continue; } //check if we can plant here if (rand.nextFloat() > density) { continue; } //find highest scoring biome float[] v = biomes.getVectorAt(x, y); float max = 0; Biome biome = null; for (int i = 0; i < v.length; ++i) { if (v[i] > max) { max = v[i]; biome = allBiomes[i]; } } if (biome == null) { LOG.log(Level.WARNING, "no biome found at ({0},{1})", new Object[] { x, y }); continue; } //get tree sample Pair<Float, TreeInfo>[] trees = TREES.get(biome); float f = rand.nextFloat(); TreeInfo tree = null; for (int i = 0; i < trees.length; ++i) { if (trees[i].getLeft() > f) { tree = trees[i].getRight(); break; } } if (tree == null) { continue; } //create tree node TreeNode treeNode = new TreeNode(tree, app.getAssetManager(), app.getCamera()); treeNode.setUseHighRes(false); // Geometry treeNode = new Geometry("tree", testMesh); // treeNode.setMaterial(testMat); treeNode.setLocalScale(size); treeNode.rotate(-FastMath.HALF_PI, 0, 0); Vector3f pos = app.getHeightmapPoint(x + rand.nextFloat() - 0.5f, y + rand.nextFloat() - 0.5f); pos.x *= scaleFactor; pos.z *= scaleFactor; treeNode.setLocalTranslation(pos); nodes.put(tree, treeNode); } } for (Map.Entry<TreeInfo, Object> entries : nodes.entrySet()) { Collection<TreeNode> col = (Collection<TreeNode>) entries.getValue(); for (TreeNode n : col) { sceneNode.attachChild(n); } } LOG.info(sceneNode.getChildren().size() + " trees planted"); }