hashset

hashcode() and equals() method [duplicate]

限于喜欢 提交于 2019-12-04 18:20:55
问题 This question already has answers here : What issues should be considered when overriding equals and hashCode in Java? (11 answers) Closed 6 years ago . so i have a question on hashcode() and equals() method Let's say I just write a very basic program overridng both the methodes import java.util.*; class Employee { private String name; private int empid; public Employee(String name,int empid) { this.name=name; this.empid=empid; } public int getEmpid() { return empid; } public String getName()

HashSet vs ArrayList Speed? Insert vs Lookup ( Java )

半世苍凉 提交于 2019-12-04 17:07:12
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 the ArrayList, although the lookup is worse, the inserting makes up for it? It very much depends on the

Moving data from a HashSet to ArrayList in Java

心已入冬 提交于 2019-12-04 16:20:04
问题 I have the following Set in Java: Set< Set<String> > SetTemp = new HashSet< Set<String> >(); and I want to move its data to an ArrayList : ArrayList< ArrayList< String > > List = new ArrayList< ArrayList< String > >); Is it possible to do that ? 回答1: You simply need to loop: Set<Set<String>> setTemp = new HashSet<Set<String>> (); List<List<String>> list = new ArrayList<List<String>> (); for (Set<String> subset : setTemp) { list.add(new ArrayList<String> (subset)); } Note: you should start

How does HashSet not allow duplicates?

╄→尐↘猪︶ㄣ 提交于 2019-12-04 15:07:27
问题 I was going through the add method of HashSet . It is mentioned that If this set already contains the element, the call leaves the set unchanged and returns false. But the add method is internally saving the values in HashMap public boolean add(E e) { return map.put(e, PRESENT)==null; } The put method of HashMap states that Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced. So if the put method

Why can't I preallocate a hashset<T>

与世无争的帅哥 提交于 2019-12-04 14:57:14
问题 Why can't I preallocate a hashset<T> ? There are times when i might be adding a lot of elements to it and i want to eliminate resizing. 回答1: Answer below was written in 2011. It's now in .NET 4.7.2 and .NET Core 2.0; it will be in .NET Standard 2.1. There's no technical reason why this shouldn't be possible - Microsoft just hasn't chosen to expose a constructor with an initial capacity. If you can call a constructor which takes an IEnumerable<T> and use an implementation of ICollection<T> , I

Hashcode() Vs Equals()

删除回忆录丶 提交于 2019-12-04 12:43:56
I have below this two classes .. class Emp //implements Comparable { String name,job; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } int salary; public Emp(String n,String j,int sal) { name=n; job=j; salary=sal; } public void display() { System.out.println(name+"\t"+job+"\t"+salary); } public boolean equals(Object o) { Emp p=(Emp)o; return this.name.equals

How to find common elements in multiple lists?

让人想犯罪 __ 提交于 2019-12-04 12:05:57
I have a list of list (nest list). I need to find the common elements between those. Example would be [1,3,5], [1,6,7,9,3], [1,3,10,11] should result in [1,3] If not using the retainAll method of HashSet, how to iterate all the element to find? Thanks, What you can do: Set<Integer> intersection = new HashSet<>(lists.get(0)) for(List<Integer> list : lists) { Set<Integer> newIntersection = new HashSet<>(); for(Integer i : list) { if(intersection.contains(i)) { newIntersections.add(i); } } intersection = newIntersection; } Narendra Jaggi Do the sorting on individual list which will result in

Should I use a `HashSet` or a `TreeSet` for a very large dataset?

China☆狼群 提交于 2019-12-04 11:20:44
问题 I have a requirement to store 2 to 15 million Accounts (which are a String of length 15) in a data structure for lookup purpose and checking uniqueness. Initially I planned to store them in a HashSet , but I doubt the speed of the lookup will be slow because of hash collisions and will eventually be slower than a TreeMap (using Binary search). There is no requirement for Data to be sorted. I am using Java 7. I have 64G system with 48G dedicated for this application. This question is not a

Is HashSet<T> the fastest container to look up in?

北战南征 提交于 2019-12-04 08:29:56
问题 I need to check that specific string contains in the set of others: private bool Contains(string field) { return this.Fields.Contains(field); // HashSet<string> local property } What is the best type of container to use if only one task of it - to hold a number of strings and check does another one is into or does not? 回答1: Yes, HashSet is perfect for this since it contains one value to look up unlike a Dictionary which requires a key and a value. 回答2: Does HashSet work? Sure. But that's not

Java: Modify id that changes hashcode

天大地大妈咪最大 提交于 2019-12-04 07:10:05
I use HashSet and I need to modify the ID of an object, but it changes hashcode and breaks HashSet and rules of hashCode() method. What is best solution: to delete object from Set and add object with new ID, or to keep the hash code (generated in constructor, for example) in every object in Set, or is there other way to solve this problem? Thanks for help. UPDATE: I made mistake: keeping hash code in object is terrible, because in that case equal objects can have different hash codes. A HashSet as a container accesses its items (contains, remove) via the hash code of the items you put into it.