hashset

Build HashSet from a vector in Rust

旧巷老猫 提交于 2020-08-22 02:36:31
问题 I want to build a HashSet<u8> from a Vec<u8> . I'd like to do this in one line of code, copying the data only once, using only 2n memory, but the only thing I can get to compile is this piece of .. junk, which I think copies the data twice and uses 3n memory. fn vec_to_set(vec: Vec<u8>) -> HashSet<u8> { let mut victim = vec.clone(); let x: HashSet<u8> = victim.drain(..).collect(); return x; } I was hoping to write something simple, like this: fn vec_to_set(vec: Vec<u8>) -> HashSet<u8> {

HashSet<T>.CreateSetComparer() cannot specify IEqualityComparer<T>, is there an alternative?

╄→гoц情女王★ 提交于 2020-08-08 04:06:10
问题 In the internal source there is such a constructor public HashSetEqualityComparer(IEqualityComparer<T> comparer) but it's internal so I can't use it. By default, HashSet<T>.CreateSetComparer() just uses the parameterless constructor which will apply EqualityComparer<T>.Default . Is there a way to get a HashSetEqualityComparer<T> with a IEqualityComparer<T> of choice, without copying out the code from the source? 回答1: I think best solution is using SetEquals . It does the job you need and

Is it possible that TreeSet equals HashSet but not HashSet equals TreeSet

和自甴很熟 提交于 2020-07-28 05:55:08
问题 The bounty expires in 2 days . Answers to this question are eligible for a +50 reputation bounty. Concurrent Bhai is looking for a more detailed answer to this question. I had a interview today and the person taking my interview puzzled me with his statement asking if it possible that TreeSet equals HashSet but not HashSet equals TreeSet . I said "no" but according to him the answer is "yes". How is it even possible? 回答1: Your interviewer is right, they do not hold equivalence relation for

Attemping to add a value to a HashSet doesn't change the amount of values in it

Deadly 提交于 2020-06-10 19:39:18
问题 I have a HashSet and when I use the Add method of the collection, nothing is added. The output is still 2, 3, 5, 7, 11, 13 and the output from .Count is 6. Is this a bug or am I doing something wrong here? namespace AllerDiz { class MainClass { public static void Main (string[] args) { HashSet<int> smallPrimeNumbers = new HashSet<int> { 2, 3, 5, 7, 11, 13 }; smallPrimeNumbers.Add (3); smallPrimeNumbers.Add (5); smallPrimeNumbers.Add (7); Console.WriteLine ("{0}", smallPrimeNumbers.Count);

Attemping to add a value to a HashSet doesn't change the amount of values in it

南笙酒味 提交于 2020-06-10 19:37:02
问题 I have a HashSet and when I use the Add method of the collection, nothing is added. The output is still 2, 3, 5, 7, 11, 13 and the output from .Count is 6. Is this a bug or am I doing something wrong here? namespace AllerDiz { class MainClass { public static void Main (string[] args) { HashSet<int> smallPrimeNumbers = new HashSet<int> { 2, 3, 5, 7, 11, 13 }; smallPrimeNumbers.Add (3); smallPrimeNumbers.Add (5); smallPrimeNumbers.Add (7); Console.WriteLine ("{0}", smallPrimeNumbers.Count);

JAVA HashSet Order [duplicate]

倾然丶 夕夏残阳落幕 提交于 2020-04-06 13:21:30
问题 This question already has answers here : Ordering of elements in Java HashSet (2 answers) Closed 5 years ago . this is my code on one exercise, public class RockTest { public static void main(String[] args){ HashSet<Rock> hashset = new HashSet(); Rock rock1 = new Rock("QingDanasty",89); Rock rock2 = new Rock("Modern",32); Rock rock3 = new Rock("MingDanasty",100); hashset.add(rock1); hashset.add(rock2); hashset.add(rock3); Iterator<Rock> iterator = hashset.iterator(); while(iterator.hasNext())

JAVA HashSet Order [duplicate]

扶醉桌前 提交于 2020-04-06 13:21:14
问题 This question already has answers here : Ordering of elements in Java HashSet (2 answers) Closed 5 years ago . this is my code on one exercise, public class RockTest { public static void main(String[] args){ HashSet<Rock> hashset = new HashSet(); Rock rock1 = new Rock("QingDanasty",89); Rock rock2 = new Rock("Modern",32); Rock rock3 = new Rock("MingDanasty",100); hashset.add(rock1); hashset.add(rock2); hashset.add(rock3); Iterator<Rock> iterator = hashset.iterator(); while(iterator.hasNext())

JAVA HashSet Order [duplicate]

柔情痞子 提交于 2020-04-06 13:21:00
问题 This question already has answers here : Ordering of elements in Java HashSet (2 answers) Closed 5 years ago . this is my code on one exercise, public class RockTest { public static void main(String[] args){ HashSet<Rock> hashset = new HashSet(); Rock rock1 = new Rock("QingDanasty",89); Rock rock2 = new Rock("Modern",32); Rock rock3 = new Rock("MingDanasty",100); hashset.add(rock1); hashset.add(rock2); hashset.add(rock3); Iterator<Rock> iterator = hashset.iterator(); while(iterator.hasNext())