hashset

Understanding contains method of Java HashSet

荒凉一梦 提交于 2020-01-19 09:49:12
问题 Newbie question about java HashSet Set<User> s = new HashSet<User>(); User u = new User(); u.setName("name1"); s.add(u); u.setName("name3"); System.out.println(s.contains(u)); Can someone explain why this code output false ? Moreover this code does not even call equals method of User. But according to the sources of HashSet and HashMap it have to call it. Method equals of User simply calls equals on user's name. Method hashCode return hashCode of user's name 回答1: If the hash code method is

Understanding contains method of Java HashSet

和自甴很熟 提交于 2020-01-19 09:48:07
问题 Newbie question about java HashSet Set<User> s = new HashSet<User>(); User u = new User(); u.setName("name1"); s.add(u); u.setName("name3"); System.out.println(s.contains(u)); Can someone explain why this code output false ? Moreover this code does not even call equals method of User. But according to the sources of HashSet and HashMap it have to call it. Method equals of User simply calls equals on user's name. Method hashCode return hashCode of user's name 回答1: If the hash code method is

How to sort a HashSet?

混江龙づ霸主 提交于 2020-01-19 04:42:06
问题 For lists, we use the Collections.sort(List) method. What if we want to sort a HashSet ? 回答1: A HashSet does not guarantee any order of its elements. If you need this guarantee, consider using a TreeSet to hold your elements. However if you just need your elements sorted for this one occurrence, then just temporarily create a List and sort that: Set<?> yourHashSet = new HashSet<>(); ... List<?> sortedList = new ArrayList<>(yourHashSet); Collections.sort(sortedList); 回答2: Add all your objects

IndexOutOfRangeException when adding to Hashset<T>

ぃ、小莉子 提交于 2020-01-14 07:13:31
问题 I have a simple application that adds about 7 million short strings to a HashSet <string> . Occasionally I get an exception during a call to Hashset.Add(): System.Collections.Generic.HashSet`1.IncreaseCapacity(): Index was outside the bounds of the array. It's an intermittent problem and seems related to memory, but this is on a win2k8 R2 server with 16 GB, not much else going on, most of that physical memory is available. Any ideas? 回答1: The HashSet<T> is not thread-safe. Especially when

Hashcode() Vs Equals()

给你一囗甜甜゛ 提交于 2020-01-13 05:16:13
问题 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

Order of items in HashSet not correct

戏子无情 提交于 2020-01-11 13:45:09
问题 As an exercise I am developing a simple notes application. Obviously the notes have to be saved persistently so I have the following method: public static void saveNotesPersistently() { SharedPreferences.Editor editor = sharedPreferences.edit(); HashSet<String> titleSet = new HashSet<String>(); HashSet<String> contentSet = new HashSet<String>(); for (int i = 0; i < Library.notes.size(); i++) { String title = (Library.notes.get(i).title == null) ? "No title" : Library.notes.get(i).title; Log.d

Re-ordering arbitrary an array of integers

穿精又带淫゛_ 提交于 2020-01-11 07:58:06
问题 I have a method that accepts as argument an array of integers and I'd just change arbitrary the order of its values public static int[] _game_number = new int[4]; public static int[] _current_number = new int[4]; public static void GetRandomTwentyFour() { HashSet<int> nums = new HashSet<int>(); for (int i = 0; i < 4; i++) { Random r = new Random(); nums.Add(r.Next(0, 4)); } List<int> liste = nums.ToList<int>(); _current_number = new int[] { _game_number[liste[0]], _game_number[liste[1]],

Bind to specific item in collection without indexer

会有一股神秘感。 提交于 2020-01-06 15:17:21
问题 I am using an EF5 datamodel, in which the related entities are collected into a HashSet. I want to bind an element to the first related entity in the HashSet. I've seen answers on SO (here and here) which suggest using the following: <DataGridComboBoxColumn SelectedValueBinding="{Binding LeadSteps[0].NewZoneID}"/> but this relies on the collection having an indexer, which HashSet doesn't have. (If this cannot be done, I can create a property in a partial class to the entity, and bind to that.

Write hashset to txt using filewriter

[亡魂溺海] 提交于 2020-01-05 20:29:09
问题 I am trying to write a hashset to a text file. Normally, I have no issues with writing to txt as it is simple for me. However, in this case I am absolutely clueless. A bit of background about the class: it is a hashset of ComputerScientist objects with 2 fields; name, and field of research (and yes, I know what I put to fill up the hashset does not count, I was only trying to test to see if I could get this to work). I know the basic setup to use filewriter to save strings to hashset which is

Write hashset to txt using filewriter

邮差的信 提交于 2020-01-05 20:28:10
问题 I am trying to write a hashset to a text file. Normally, I have no issues with writing to txt as it is simple for me. However, in this case I am absolutely clueless. A bit of background about the class: it is a hashset of ComputerScientist objects with 2 fields; name, and field of research (and yes, I know what I put to fill up the hashset does not count, I was only trying to test to see if I could get this to work). I know the basic setup to use filewriter to save strings to hashset which is