hashset

Joining both hashsets together

假装没事ソ 提交于 2019-12-13 08:32:46
问题 My aim is to create a database like code where i add planets and the year they are found, and once the user inputs planets it shows all the planets and the year they are found, so far i have created this. import java.util.HashSet; import java.util.Scanner; import java.lang.*; public class sky { public static void main(String args[]) { Scanner in = new Scanner(System.in); // HashSet declaration HashSet < String > planet = new HashSet < String > (); // Adding elements to the HashSet planet.add(

Are there problems of implementing HashSet using HashMap?

本小妞迷上赌 提交于 2019-12-13 05:54:14
问题 In java HashSet is implemented using a HashMap. So when we add an item to the set the following code is executed. public boolean add(E e) { return map.put(e, PRESENT)==null; } what happens when two objects that are different but having equal hash is added to the HashSet; will it (HashSet) contain both the objects or what happens then? 回答1: hashmap uses .equals() as well as .hash() . two things are not the same unless .equals() returns true. the same will be true of hashset. so if two objects

Speed of enumerating Hashset

两盒软妹~` 提交于 2019-12-13 00:07:48
问题 Is there any performance difference between these two enumerations of a Hashset<string> in C#? foreach(string value1 in Hashset1) { } and for(int i = 0; i < Hashset1.Count; i++) { string _value1 = Hashset1.ElementAt(i); } Is there any other fast enumeration (performance wise) of a Hashset ? 回答1: If it matters, you should benchmark and see for yourself what is the faster solution in your scenario. In this case the second solution is pretty much guaranteed to be slower because ElementAt is an

Javafx tableview items prevent duplicates

六眼飞鱼酱① 提交于 2019-12-12 18:30:09
问题 I'm using javafx tableview with observableList list, I tried to prevent list from holding duplicate items. After doing some search, i figure out that an observableSet can do this job by overidding thoes methodes:equals() and hashcode(). But the problem that javaFX tableview can't hold an observable set: tableView.setItems(FXCollections.observableSet(new hashSet<T>()); I also planned to calculate the some for a columns in my tableview so, i need // After change in element T the total will

Text file into Java Set<String> using Commons or Guava

陌路散爱 提交于 2019-12-12 13:25:54
问题 I would like to load each line in a file into HashSet collection. Is there a simple way to do this? 回答1: How about: Sets.newHashSet(Files.readLines(file, charSet)); (using Guava). References: Files.readLines() Sets.newHashSet() 回答2: You can do Set<String> lines = new HashSet<String>(FileUtils.readLines(new File("foo.txt"))); Using the Apache Commons FileUtils class and the readlines method. 回答3: Multiset can store duplicated strings, if your text contains duplicated lines. (add ordering)

Improving speed and memory consumption when handling ArrayList with 100 million elements

 ̄綄美尐妖づ 提交于 2019-12-12 13:14:01
问题 I work with text files with short strings in it (10 digits). The size of file is approx 1.5Gb, so the number of rows is reaching 100 millions. Every day I get another file and need to extract new elements (tens of thousands a day). What's the best approach to solve my problem? I tried to load data in ArrayList - it takes around 20 seconds for each file, but substraction of arrays takes forever. I use this code: dataNew.removeAll(dataOld); Tried to load data in HashSets - creation of HashSets

Unique values of custom class in Java Set

ⅰ亾dé卋堺 提交于 2019-12-12 10:49:29
问题 I expect to have only 2 elements in my Set but I receive 3 elements while printing! How can I define uniqueness? public class test { public static void main(String[] args) { class bin { int a; int b; bin (int a, int b){ this.a=a; this.b=b; } public boolean Equals(bin me) { if(this.a==me.a && this.b==me.b) return true; else return false; } @Override public String toString() { return a+" "+b; } } Set<bin> q= new HashSet<bin>(); q.add(new bin(11,23)); q.add(new bin(11,23)); q.add(new bin(44,25))

Subtract HashSets (and return a copy)?

故事扮演 提交于 2019-12-12 09:31:38
问题 I've got a HashSet, var universe = new HashSet<int>(); And a bunch of subsets, var sets = new List<HashSet<int>>(numSets); I want to subtract a chunk, which I can do like this: var remaining = universe.ExceptWith(sets[0]); But ExceptWith works in-place. I don't want to modify the universe . Should I clone it first, or is there a better way? 回答1: I guess I should clone it first? How do I do that? var universe = new HashSet<int>(); var subset = new HashSet<int>(); ... // clone the universe var

Should I dump java.util.HashSet in favor of CompactHashSet? [closed]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 08:41:25
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . I found that there is an implementation of a Set that uses hashes (with all the useful consequences, like O(1) for contains() etc) that is claimed to be more efficient than java.util.HashSet in every aspect: http://ontopia.wordpress.com/2009/09/23/a-faster-and-more-compact

Thread-safe HashSet with Guava Collections

无人久伴 提交于 2019-12-12 08:20:11
问题 Like the title says, i would like to get a thread-safe HashSet using Guava Collections. Can you help me? Thanks! 回答1: Set<K> set = Collections.newSetFromMap(new ConcurrentHashMap<K, Boolean>()); 回答2: This would be the right answer, Using the Sets class from Guava. Anyway the answer from @crhis was good intended. Sets.newSetFromMap(new ConcurrentHashMap<V, Boolean>()); 回答3: Google Collections had a factory method named Sets.newConcurrentHashSet() for a while. Its implementation was similar to