hashset

Check if an array exists in a HashSet<int[]>

ⅰ亾dé卋堺 提交于 2021-02-17 05:05:09
问题 How do I check if an array exists in a HashSet ? For example: int[] a = new int[]{0, 0}; HashSet<int[]> set = new HashSet<>(); set.add(a); Then: int[] b = new int[]{0, 0}; set.contains(b); // ===> true 回答1: Using arrays int[] a = new int[] { 0, 0 }; HashSet<int[]> set = new HashSet<>(); set.add(a); int[] b = new int[] { 0, 0 }; boolean contains = set.stream().anyMatch(c -> Arrays.equals(c, b)); System.out.println("Contains? " + contains); Output: Contains? true It doesn’t exploit the fast

Does HashSet and HashMap both use Hashtable ? Or Hashtable is totally different data structure?

僤鯓⒐⒋嵵緔 提交于 2021-02-08 09:51:30
问题 I already know that we use HashMap when we have Key-Value pair and we use HashSet when we do not need repetitive data. But I always get confused where Hashtable comes into play. Is it a totally different data structure? Or both HashMap and HashSet use hash function to store data in Hashtable ? Thorough explanation will be really helpful . 回答1: More specific, HashSet uses internally a HashMap , look at the implementation of HashSet.java private transient HashMap<E,Object> map; As stated in a

Hashset as constructor argument in Java

泄露秘密 提交于 2021-02-05 07:47:47
问题 is there any way I can declare constructor to take HashSet as one of arguments and then pass HashSet value directly as argument during object initialization? I mean something like this: public class Order { private Set<Product> products = new HashSet<Product>(); public Order (???) { this.products = ??? } } and then, while initializing object: Order order = new Order("product1", "product2", "product3"); is it possible? 回答1: You can use varargs : public class Order { private Set<Product>

Hashset as constructor argument in Java

瘦欲@ 提交于 2021-02-05 07:47:29
问题 is there any way I can declare constructor to take HashSet as one of arguments and then pass HashSet value directly as argument during object initialization? I mean something like this: public class Order { private Set<Product> products = new HashSet<Product>(); public Order (???) { this.products = ??? } } and then, while initializing object: Order order = new Order("product1", "product2", "product3"); is it possible? 回答1: You can use varargs : public class Order { private Set<Product>

Simple parallelisation for hashset

让人想犯罪 __ 提交于 2021-01-28 19:41:57
问题 I have 2 loops(nested), trying to do a simple parallelisation pseudocode : for item1 in data1 (~100 million row) for item2 in data2 (~100 rows) result = process(item1,item2) // couple of if conditions hashset.add(result) // while adding, incase of a duplicate i also decide wihch one to retain process(item1,item2) to be precise has 4 if conditions bases on values in item1 and item2.(time taken is less than 50ms) data1 size is Nx17 data2 size is Nx17 result size is 1x17 (result is joined into a

I have a list with over a million objects in it, trying to find the fastest way to search through it

限于喜欢 提交于 2021-01-28 04:46:01
问题 I have a list that stores well over a million objects within it. I need to look through the list and update the objects found through the below query as efficiently as possible. I was thinking of using a Dictionary or HashSet, but I'm relatively new to C# and could not figure out how to implement these other two approaches. My current code is simply a LINQ statement searching through an IList. public IList<LandObject> landObjects = new List<LandObject>(); var lObjsToUpdate = from obj in

(Java) Why is a HashSet allowed to be used synchronously if it is non synchronized?

孤街醉人 提交于 2021-01-27 07:43:36
问题 I've been reading up on the differences between a HashMap , HashSet , and HashTable . A key thing I've been noticing is that I've seen that HashMap / HashSet are not synchronized while a HashTable is. However in a code base that I've seen before there are several places where a block like this is used: synchronized (hashSet) { //Some code involving the hashset } How is this possible if a HashSet isn't synchronized? Does the synchronized block simply allow us to use a non synchronous data

Using HashSet and Contains to return TRUE if one or many fields is in the hash

你说的曾经没有我的故事 提交于 2021-01-20 09:45:36
问题 I am wondering if it is possible to use a HashSet and make the method Contains to return true if one of the field is in the hash for a giving object. This is an example of what I would like static void Main(string[] args) { HashSet<Product> hash = new HashSet<Product>(); // Since the Id is the same, both products are considered to be the same even if the URI is not the same // The opposite is also true. If the URI is the same, both products are considered to be the same even if the Id is not

Using HashSet and Contains to return TRUE if one or many fields is in the hash

杀马特。学长 韩版系。学妹 提交于 2021-01-20 09:44:55
问题 I am wondering if it is possible to use a HashSet and make the method Contains to return true if one of the field is in the hash for a giving object. This is an example of what I would like static void Main(string[] args) { HashSet<Product> hash = new HashSet<Product>(); // Since the Id is the same, both products are considered to be the same even if the URI is not the same // The opposite is also true. If the URI is the same, both products are considered to be the same even if the Id is not