hashset

Optimal HashSet Initialization (Scala | Java)

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-04 14:06:42
问题 I'm writing an A.I. to solve a "Maze of Life" puzzle. Attempting to store states to a HashSet slows everything down. It's faster to run it without a set of explored states. I'm fairly confident my node (state storage) implements equals and hashCode well as tests show a HashSet doesn't add duplicate states. I may need to rework the hashCode function, but I believe what's slowing it down is the HashSet rehashing and resizing. I've tried setting the initial capacity to a very large number, but

Altering hashCode of object inside of HashSet / HashMap

半腔热情 提交于 2020-01-04 06:24:20
问题 I am relatively new to Java and am puzzled about the following thing: I usually add objects to an ArrayList before setting its content. I.e., List<Bla> list = new ArrayList<>(); Bla bla = new Bla(); list.add(bla); bla.setContent(); // content influences hashCode This approach works great. I am concerned whether this approach will give me trouble when used with HashSet s or HashMap s. The internal hash table get set at the time the object is added. What will happen if setContent() gets called

HashSet.contains returns false when it shouldn't

家住魔仙堡 提交于 2020-01-04 05:22:28
问题 I have this code: public class Tray { private Set<Block> blocks; private int numColumns; private int numRows; //constructor public Tray (int numRows, int numColumns){ this.numColumns = numColumns; this.numRows = numRows; blocks = new HashSet<>(); } public boolean satisfiesGoal(Tray other){ Block thisBlock = this.blocks.iterator().next(); Block otherBlock = other.blocks.iterator().next(); boolean hashesEqual = thisBlock.hashCode() == otherBlock.hashCode(); // this is true boolean areEqual =

Why does a HashSet sort single alphabetic characters?

感情迁移 提交于 2020-01-03 17:47:26
问题 So what I know is that a HashSet has no real sorting capabilities like a SortedSet, however I stumbled upon this : When I run the following code : public static void main(String[] args) { Set<String> collection = new HashSet<String>(2000); String[] data = {"a", "c", "g", "f", "b", "f", "b", "d","q","r","d","m"}; for(String input: data) { collection.add(input); } System.out.println("Output: " + collection); } I get the following output : Output: [a, b, c, d, f, g, m, q, r] Which is

Can I randomly sample from a HashSet efficiently?

ぐ巨炮叔叔 提交于 2020-01-03 09:05:10
问题 I have a std::collections::HashSet , and I want to sample and remove a uniformly random element. Currently, what I'm doing is randomly sampling an index using rand.gen_range , then iterating over the HashSet to that index to get the element. Then I remove the selected element. This works, but it's not efficient. Is there an efficient way to do randomly sample an element? Here's a stripped down version of what my code looks like: use std::collections::HashSet; extern crate rand; use rand:

HashSet - ensuring the earlier object persistence

a 夏天 提交于 2020-01-02 10:03:31
问题 I have to use a HashSet where a lot of duplicate value may be inserted. But I want to preserve the earlier data inserted in the hash when a later insertion makes the duplicate. To examine this I have write the following code and insert many duplicate value, but it doesn't satisfy me. Please see the code below - import java.util.HashSet; import java.util.Set; public class SetTest { private static Set<Student> studentSet = new HashSet<Student>(); private static Student s1, s2, s3, s4, s5, s6,

HashSet - ensuring the earlier object persistence

南笙酒味 提交于 2020-01-02 10:03:19
问题 I have to use a HashSet where a lot of duplicate value may be inserted. But I want to preserve the earlier data inserted in the hash when a later insertion makes the duplicate. To examine this I have write the following code and insert many duplicate value, but it doesn't satisfy me. Please see the code below - import java.util.HashSet; import java.util.Set; public class SetTest { private static Set<Student> studentSet = new HashSet<Student>(); private static Student s1, s2, s3, s4, s5, s6,

Remove a key in hashmap when the value's hashset is Empty

≯℡__Kan透↙ 提交于 2020-01-02 03:48:06
问题 I have a hashmap that maps strings keys to hashsets values, and I want to remove a key from the hashmap when the hashmaps's hashset value is empty. I'm having trouble approaching this. Here's what I've tried but I'm very stuck: for(Map.Entry<String, HashSet<Integer>> entr : stringIDMap.entrySet()) { String key = entr.getKey(); if (stringIDMap.get(key).isEmpty()) { stringIDMap.remove(key); continue; } //few print statements... } 回答1: In order to avoid ConcurrentModificationException, you need

HashSet vs ArrayList Speed? Insert vs Lookup ( Java )

与世无争的帅哥 提交于 2020-01-01 17:11:18
问题 Looking at this question it made me curious as to which to use, Hashset vs ArrayList. The Hashset seems to have a better lookup and ArrayList has a better insert (for many many objects). So I my question is, since I can't insert using an ArrayList, then search through it using a HashSet, I'm going to have to pick one or the other. Would inserting with an ArrayList, converting to a HashSet to do the lookups, be SLOWER overall than to just insert into a HashSet then lookup? Or just stick with

What does a hashset do with memory when initializing a collection?

天涯浪子 提交于 2020-01-01 08:48:09
问题 I stumbled upon the following problem. I want a hashset with all numbers from 1 to 100.000.000. I tried the following code: var mySet = new HashSet<int>(); for (var k = 1; k <= 100000000; k++) mySet.Add(k); That code didn't make it since I got memory overflow somewhere around the 49mil. This was also pretty slow and memory grew excessively. Then I tried this. var mySet = Enumerable.Range(1, 100000000).ToHashSet(); where ToHashSet() is the following code: public static HashSet<T> ToHashSet<T>