hashset

Collection removeAll ignoring case?

最后都变了- 提交于 2020-01-01 04:21:10
问题 Ok so here is my issue. I have to HashSet 's, I use the removeAll method to delete values that exist in one set from the other. Prior to calling the method, I obviously add the values to the Set s. I call .toUpperCase() on each String before adding because the values are of different cases in both lists. There is no rhyme or reason to the case. Once I call removeAll , I need to have the original cases back for the values that are left in the Set . Is there an efficient way of doing this

how to find and return objects in java hashset

点点圈 提交于 2019-12-31 21:30:11
问题 According to the HashSet javadoc, HashSet.contains only returns a boolean. How can I "find" an object in a hashSet and modify it (it's not a primitive data type)? I see that HashTable has a get() method, but I would prefer to use the set. 回答1: You can remove an element and add a different one. Modifying an object while it is in a hash set is a recipe for disaster (if the modification changes the hash value or equality behavior). 回答2: To quote the source of the stock Sun java.util.HashSet:

Time Complexity for an algorithm

帅比萌擦擦* 提交于 2019-12-31 04:40:09
问题 Am I correct in my explanation when calculating the time complexity of the following algorithm? A HashSet, moduleMarksheetFiles, is being used to add the files that contain the moduleName specified. for (File file: marksheetFiles){ while(csvReader.readRecord()){ String moduleName = csvReader.get(ModuleName); if (moduleName.equals(module)){ moduleMarksheetFiles.add(file); } } } Let m be the number of files Let k be the average number of records per file. As each file is added only once because

What's the difference between these two java variable declarations?

谁说胖子不能爱 提交于 2019-12-29 09:11:54
问题 public class SomeClass { private HashSet<SomeObject> contents = new HashSet<SomeObject>(); private Set<SomeObject> contents2 = new HashSet<SomeObject>(); } What's the difference? In the end they are both a HashSet isn't it? The second one looks just wrong to me, but I have seen it frequently used, accepted and working. 回答1: Set is an interface, and HashSet is a class that implements the Set interface. Declaring the variable as type HashSet means that no other implementation of Set may be used

Internal implementation of java.util.HashMap and HashSet

雨燕双飞 提交于 2019-12-28 05:10:42
问题 I have been trying to understand the internal implementation of java.util.HashMap and java.util.HashSet . Following are the doubts popping in my mind for a while: Whats is the importance of the @Override public int hashcode() in a HashMap/HashSet? Where is this hash code used internally? I have generally seen the key of the HashMap be a String like myMap<String,Object> . Can I map the values against someObject (instead of String) like myMap<someObject, Object> ? What all contracts do I need

How is this HashSet producing sorted output?

限于喜欢 提交于 2019-12-28 04:27:04
问题 The following code produces the out put [1,2] even though hashset is not sorted. Set set = new HashSet(); set.add(new Integer(2)); set.add(new Integer(1)); System.out.println(set); Why is that? 回答1: EDIT: As of Java 8 and later, the following is no longer applicable. This proves that you shouldn't rely on undocumented Java behaviours. This behaviour is caused by several separate reasons: Integers hash to themselves in Java, HashMap s and HashSet s are backed up by an array they also modify

Why doesn't my hashmap work? My object has the property that hashCode() equality implies equals(…) equality [closed]

江枫思渺然 提交于 2019-12-25 18:52:10
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 years ago . Why doesn't my Java HashMap work? My object has the property that .equals equality implies hashCode equality. You can assume that I'm modifying a field of the object after I add the object to the HashMap . 回答1: You can assume that I'm modifying a field of the object after I add the object to the

HashSet showing more values [closed]

牧云@^-^@ 提交于 2019-12-25 07:54:40
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . class KeyMaster { public int i; public KeyMaster(int i) { this.i = i; } public boolean equals(Object o) { return i == ((KeyMaster)o).i; } public int hashCode() { return i; } } public class MIt { public static void main(String[] args) { Set<KeyMaster> set = new HashSet<KeyMaster>(); KeyMaster k1 =

best way to take an intersection of more than two hashsets in c#, when we donot know before hand how many hashsets are there

时光怂恿深爱的人放手 提交于 2019-12-25 05:28:17
问题 I am making a boolean retrieval system for some large no. of documents, in which i have made a dictionary of hashsets, and the the entries into the dictionary are the terms, and the hashsets contains the documentids in which the term was found. Now when i want to search for a single word, i will simply enter the word and i will index the dictionary using the entered word in query and print out the corresponding hashset. But i also want to search for sentences, in this case i will split the

What is the runtime for quadratic probing in a HashTable?

[亡魂溺海] 提交于 2019-12-25 04:12:30
问题 This is a similar question to Linear Probing Runtime but it regards quadratic probing. It makes sense to me that "Theoretical worst case is O(n)" for linear probing because in the worst case, you may have to traverse through every bucket(n buckets) What would runtime be for quadratic probing? I know that quadratic probes in a quadratic fashion -1, 4, 9, 16, ..... My initial thought was that it's some variation of log n(exponential) but there isn't a consistent base. 回答1: If there are n - 1