hashset

Converting from HashSet<String> to String[]

痴心易碎 提交于 2019-12-02 20:07:19
What's the best way to convert HashSet<String> to String[] ? set.toArray(new String[set.size()]); Answer of JB Nizet is correct, but in case you did this to transform to a CSV like string, with Java 8 you can now do: Set<String> mySet = new HashSet<>(Arrays.asList("a", "b", "c")); System.out.println(String.join(", ", mySet)); Output is: a, b, c This allows to bypass arrays. 来源: https://stackoverflow.com/questions/5474656/converting-from-hashsetstring-to-string

Why is HashMap faster than HashSet?

怎甘沉沦 提交于 2019-12-02 19:33:57
I have been reading/researching the reason why HashMap is faster than HashSet . I am not quite understanding the following statements: HashMap is faster than HashSet because the values are associated to a unique key. In HashSet , member object is used for calculating hashcode value which can be same for two objects so equals() method is used to check for equality. If it returns false , that means the two objects are different. In HashMap , the hashcode value is calculated using the key object. The HashMap hashcode value is calculated using the key object. Here, the member object is used to

.NET: How to efficiently check for uniqueness in a List<string> of 50,000 items?

旧巷老猫 提交于 2019-12-02 18:52:08
In some library code, I have a List that can contain 50,000 items or more. Callers of the library can invoke methods that result in strings being added to the list. How do I efficiently check for uniqueness of the strings being added? Currently, just before adding a string, I scan the entire list and compare each string to the to-be-added string. This starts showing scale problems above 10,000 items. I will benchmark this, but interested in insight. if I replace the List<> with a Dictionary<> , will ContainsKey() be appreciably faster as the list grows to 10,000 items and beyond? if I defer

Is there an AddRange equivalent for a HashSet in C#

痴心易碎 提交于 2019-12-02 18:17:38
With a list you can do: list.AddRange(otherCollection); There is no add range method in a HashSet . What is the best way to add another collection to a HashSet? quetzalcoatl For HashSet<T> , the name is UnionWith . This is to indicate the distinct way the HashSet works. You cannot safely Add a set of random elements to it like in Collections , some elements may naturally evaporate. I think that UnionWith takes its name after "merging with another HashSet ", however, there's an overload for IEnumerable<T> too. RoadieRich This is one way: public static class Extensions { public static bool

Hashset handling to avoid stuck in loop during iteration

折月煮酒 提交于 2019-12-02 15:08:45
问题 I'm working on image mining project, and I used Hashset instead of array to avoid adding duplicate urls while gathering urls, I reached to the point of code to iterate the Hashset that contains the main urls and within the iteration I go and download the the page of the main URL and add them to the Hashet, and go on , and during iteration I should exclude every scanned url, and also exclude ( remove ) every url that end with jpg, this until the Hashet of url count reaches 0, the question is

Need help understanding unexpected behavior using LINQ Join with HashSet<T>

拥有回忆 提交于 2019-12-02 15:05:26
问题 I encountered some odd behavior using C# HastSet with LINQ's Join method that I don't understand. I've simplified what I am doing to help focus on the behavior I am seeing. I have the following: private HashSet<MyClass> _mySet; // module level IEnumerable<ISearchKey> searchKeys; // parameter. // Partial key searches are allowed. private IEqualityComparer<ICoreKey> _coreKeyComparer; // Module level. // Compares instances of MyClass and ISearchKey to determine // if they match. Given that There

Issue with “contains” hashset method (Java)

…衆ロ難τιáo~ 提交于 2019-12-02 13:32:02
问题 The following code is not giving me the result I'm expecting: public static void main (String[] args) { Set<Pair> objPair = new LinkedHashSet<Pair>(); objPair.add(new Pair(1, 0)); System.out.println("Does the pair (1, 0) exists already? "+objPair.contains(new Pair(1, 0))); } private static class Pair { private int source; private int target; public Pair(int source, int target) { this.source = source; this.target = target; } } The result will be: Does the pair (1, 0) exists already? false I

Algorithm Complexity Time

徘徊边缘 提交于 2019-12-02 12:14:42
问题 I am currently having trouble identifying and understanding the complexity time of the following algorithm. Background: There is a list of files, each containing a list of candidate Ids. Both, number of files and number of candidates within them are not fixed. How would you calculate the time complexity for an algorithm which is responsible for: Reading each file and adding all the unique candidate Ids into a Hashset? Thanks. 回答1: i'm just repeating what amit said, so please give him the

Order of items in HashSet not correct

喜夏-厌秋 提交于 2019-12-02 11:40:49
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("Checks", "Saving note with title: " + title); String content = (Library.notes.get(i).content == null)

Checking equality with a HashSet of objects

流过昼夜 提交于 2019-12-02 11:40:30
问题 I am trying to compare two hashsets of Definition type as EqualityComparer<T>.Default.Equals(value, oldValue) . Definition is defined as follows public class Definition { public string Variable { get; set; } public HashSet<Location> LocationList { get; set; } public override bool Equals(object obj) { Definition other = obj as Definition; return other.Variable.Equals(this.Variable) && other.LocationList!= null &&this.LocationList != null && other.LocationList.Count == this.LocationList.Count &