hashset

Set is not working with overridden equals

纵饮孤独 提交于 2019-12-11 02:09:54
问题 I was trying to use set. so i tried this way HashSet set=new HashSet(); Points p1 = new Points(10, 20); Points p2 = new Points(10, 20); System.out.println(set.add(p1)); // output true System.out.println(set.add(p2)); // output false I know my first output will be true and second will be false as Set will not allow duplicate elements. And, i also know Set achieve this by using equals(Object o) method. Which comes from java Object class with following signature. public boolean equals(Object o)

Overriding HashSet's Contains Method

吃可爱长大的小学妹 提交于 2019-12-10 18:04:05
问题 Could anybody tell me how I can override HashSet's contains() method to use a regex match instead of just equals()? Or if not overriding, how I can add a method to use a regex pattern? Basically, I want to be able to run regex on a HashSet containing strings, and I need to match substrings using regex. If my method is not appropriate, please suggest others. Thank you. :) 回答1: You could extend a HashSet the following way: public class RegExHashSet extends HashSet<String > { public boolean

java容器源码分析(六)——HashSet

情到浓时终转凉″ 提交于 2019-12-10 15:43:55
本文内容 HashSet概述 HashSet源码分析 HashSet概述 HashSet是Set的一种实现,其底层是用HashMap实现的,整个HashSet看起来就像一个包装类! HashSet的继承图如下: HashSet继承了Set、Abstract类,实现了Cloneable 、Serializable 接口。 HashSet实现 看一下HashSet的属性 private transient HashMap<E,Object> map; // Dummy value to associate with an Object in the backing Map private static final Object PRESENT = new Object(); 底层直接用了HashMap.PRESENT就是用来填充map的value。为何不用null呢?? 默认构造函数: public HashSet() { map = new HashMap<>(); } public HashSet(Collection<? extends E> c) { map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16)); addAll(c); } public HashSet(int initialCapacity,

Java - Is the Point class's hashCode() method any good, or should I override it and write my own?

微笑、不失礼 提交于 2019-12-10 15:03:39
问题 Is there any way to actually see the source code of standard java classes by the way? I'm making a hash table of points ( HashSet<Point> ) and I want to make sure that it will hash well, but I can't see what Point's hashCode() method actually looks like, so I don't know how good it really is. Can anyone help me? Should I override it? And if so, is there an easy way to do this without creating a whole new java file/class? 回答1: If you're searching for the hashCode() of java.awt.Point , it is

Get a HashSet out of the keys of a HashMap?

时光怂恿深爱的人放手 提交于 2019-12-10 14:14:31
问题 I have a pretty big (100'000s of entries) HashMap . Now, I need a HashSet containing all the keys from this HashMap . Unfortunately, HashMap only has a keySet() method which returns a Set but not a HashSet . What would be an efficient way to generate such a HashSet using Java? 回答1: Why do you specifically need a HashSet? Any Set have the same interface, so typically can be used interchangeably, as good-practices requires that you use the Set interface for all of them. If you really need so,

What is unchecked and unsafe operation here?

南笙酒味 提交于 2019-12-10 13:50:16
问题 I have the following code: private static final Set<String> allowedParameters; static { Set<String> tmpSet = new HashSet(); tmpSet.add("aaa"); allowedParameters = Collections.unmodifiableSet(tmpSet); } And it cause: Note: mygame/Game.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. And when I recompile with the suggested option I see a pointer (^) pointing at "new" in front of HashSet(); . Does anybody know what is going on here? 回答1: Yes, you're

Can i have HashSets as the keys in a HashMap? Suggest an alternative if not

安稳与你 提交于 2019-12-10 13:45:24
问题 Edit: explained the problem properly now. I have a hashmap where i want to store sets of words seen together (key) and the lines in which they were seen together(value). This is the structure i came up with: HashMap<HashSet<String>, HashSet<Integer>> hm= ... for inputs: mango, banana, apple apple, banana peach, walrus walrus, peach As I read this, line by line, I make new temporary keys (hashsets not yet inserted into hashmap) from the combination of words in the line. Each temporary key is a

Java HashSet shows list in weird order, always starting with 3

不问归期 提交于 2019-12-10 10:48:50
问题 I have array of Strings which actually in nothing but list of integers coming from file. I converted it to HashSet so as to remove duplicates as follows: Set<String> intSet = new HashSet<String>(Arrays.asList(strArr)); I expected that it all the numbers to be in order but off course, since this is a string and not integer list, it may not come in order. But whenever I try to print this HashSet, I always get output as follows: [3, 2, 1, 4] [3, 2, 5, 4] Every time, if 3 is present it is

Java containsAll does not return true when given lists

妖精的绣舞 提交于 2019-12-10 10:06:46
问题 I want to check an array is subset of another array. The program prints false, but I expect true. Why isn't containsAll returning true? int[] subset; subset = new int[3]; subset[0]=10; subset[1]=20; subset[2]=30; int[] superset; superset = new int[5]; superset[0]=10; superset[1]=20; superset[2]=30; superset[3]=40; superset[4]=60; HashSet sublist = new HashSet(Arrays.asList(subset)); HashSet suplist = new HashSet(Arrays.asList(superset)); boolean isSubset = sublist.containsAll(Arrays.asList

What load factor should be used when you know maximum possible no of elements in HashSet

孤街醉人 提交于 2019-12-10 04:17:29
问题 What load factor should I use when I really know the maximum possible no of elements in a HashSet ? I had heard that the default load factor of 0.75 is recommended as it offers good performance trade-offs between speed & space. Is this correct ? However a larger size HashSet would also takes more time in creation and more space. I am using HashSet just inorder to remove duplicate integers from a list of integers. 回答1: I spent some time playing around with load factors once, and it is shocking