hashset

HashSet remove element upon condition

只愿长相守 提交于 2019-12-11 14:19:42
问题 I have a HashSet containing tens of thousands of rectangles, when the Y is less than 0 I want to remove it from the HashSet right now my code looks like the following for (Rectangle p : point) { if(p.y<0){ point.remove(p); System.out.println("removing p"); continue; } Here is the code for my HashSet public HashSet<Rectangle> point; it never removes the Rectangle that has a Y less than 0 but the System.out.println("removing p"); runs. 回答1: The problem is HashSet does not allow you to remove

How should I override Equals and GetHashCode for HashSet?

混江龙づ霸主 提交于 2019-12-11 11:38:16
问题 Lets say I Have class: public class Ident { public String Name { get; set; } public String SName { get; set; } } and also one more: class IdenNode { public Ident id { get; set; } public List<IdenNode> Nodes { get; set; } public IdenNode() { Nodes = new List<IdenNode>(); } } I want to use HashSet<IdenNode> with mind that two elements of it are same(Equal) if and only if their id.Names are Equal. So, I'm gonna override Equals and GetHashCode like next: public override bool Equals(object obj) {

Most Efficient Way to Check File for List of Words

不羁的心 提交于 2019-12-11 10:04:36
问题 I just had a homework assignment that wanted me to add all the Java keywords to a HashSet. Then read in a .java file, and count how many times any keyword appeared in the .java file. The route I took was: Created an String[] array that contained all the keywords. Created a HashSet, and used Collections.addAll to add the array to the HashSet. Then as I iterated through the text file I would check it by HashSet.contains(currentWordFromFile); Someone recommended using a HashTable to do this.

2D array traversal to get distinct 7 digit number combos

匆匆过客 提交于 2019-12-11 07:54:24
问题 I ran into a tricky question from an interview prep book which goes.. You have a 3 by 3 matrix containing integers 1 to 9 as shown below 1 2 3 4 5 6 7 8 9 How do you get unique 7 digit number combos with the first numbers all starting with 4 (matrix[1][0]). The traversal is meant to be like that of a rook on a chess board.. 1 way either horizontally or vertically...(Having 4125874 is valid 7 digit combo btw). I tried writing some code and doing regular 2D matrix traversal with a boolean

O(1) maintained in hashset lookups when using alternative comparator?

限于喜欢 提交于 2019-12-11 06:04:27
问题 If I define my own comparator for the generic HashSet<T> in System.Collections.Generic , and its runtime is O(1), is the lookup time of the hashset still O(1)? I would think no just because there doesn't appear to be a way to set the comparator. 回答1: The reason that the lookup time of a regular hashset is O(1) is because it uses open addressing to place objects into the array, so that won't change even if you use your own comparator. 回答2: In its best case it will have a insertion of amortised

what's the difference between HashSet and LinkedHashSet

℡╲_俬逩灬. 提交于 2019-12-11 04:43:00
问题 I saw that LinkedHashSet extends HashSet and I know it preserves order. However, from checking the code in the JDK it seems that LinkedHashSet contains only constuctor and no implementation, so I guess all the logic happens in HashSet ? If that is correct, why is it designed like that? it seems very confusing. EDIT: there was an unfortunate mistake in the question. I wrote HashMap and LinkedHashMap instead of HashSet and LinkedHashSet . I fixed the question answer it if possible. Also, I was

How to copy hashset and hashmap, and does the Java use pointers?

怎甘沉沦 提交于 2019-12-11 03:56:21
问题 I have two questions: First: I have a function which returns a HashMap. To read the returned value, I write it like this: HashMap<Integer,String> hs=my_func2(); I do the same if the function returns a HashSet. HashSet<Integer> hs=my_func(); I wanted to know if in this way the returned value is copied into hs, or I should write a deep copy for it or I should write it like this: HashSet hs=new HashSet(my_func()); HashMap hm=new HashMap(my_func2()); Second quesion: I make a matrix by calling

SortedSet - custom order when storing a class object

偶尔善良 提交于 2019-12-11 03:15:42
问题 I'm looking at replacing a HashSet with a SortedSet because it is better suited to the data I'm storing. However, all the examples I've seen so far relate to storing simple objects - int's, strings etc. I want to implement this for a custom class with a number of properties, however the class also includes a date that I want to use as the 'indexer'. The question is how do I go about declaring a custom indexer for the set to use which will override the default behaviour? Thanks in advance. 回答1

Java HashSet contains function not working

为君一笑 提交于 2019-12-11 03:12:00
问题 I am writing a simple program as follow: Given two numbers M and N, p is from [M,N] and q is from [1,p-1], find all irreducible fractions of p/q. My idea is brute force all possible value of p, q. And using HashSet to avoid duplicated fraction. However, somehow the contains function not working as expected. My code import java.util.HashSet; import java.util.Set; public class Fraction { private int p; private int q; Fraction(int p, int q) { this.p = p; this.q = q; } public static int getGCD

Subclass HashSet so that it always uses a certain IEqualityComparer when used in another set

孤者浪人 提交于 2019-12-11 02:25:28
问题 I want to subclass HashSet<Point> so that it uses HashSet<Point>.CreateSetComparer() as an IEqualityComparer whenever I use it inside another set. Basically every time I do this: var myDict = new Dictionary<MySubclassOfHashSet, Char>(); I want it automatically treated as : var myDict = new Dictionary<HashSet<Point>, Char>(HashSet<Point>.CreateSetComparer()); As per this question. I have currently done this manually as follows: class MySubclassOfHashSet: HashSet<Point> { public override bool