Converting from HashSet to String[]
set.toArray(new String[set.size()]);
set.toArray(new String[set.size()]);
If you really wanted the most efficient way to clone a HashSet<T>, you’d do the following (but possibly at the cost of maintainability) Use reflector or the debugger to figure out exactly what fields in HashSet<T> need to be copied. You may need to do this recursively for each field. Use Reflection.Emit or use expression … Read more
Anthony Pegram has said it the best. Use the right tool for the job. I say this because a Distinct or HashSet isn’t that big different when it comes to performance. Use a HashSet when the collection should always hold only distinct stuffs. It also tells the programmer that you cant add duplicates to it. … Read more
My experiment shows that HashSet is faster than an ArrayList starting at collections of 3 elements inclusively. A complete results table | Boost | Collection Size | | 2x | 3 elements | | 3x | 10 elements | | 6x | 50 elements | | 12x | 200 elements | <= proportion 532-12 vs … Read more
You need to override the Object#hashCode() method in the Move class to let it return the same hashCode() value for the state of the Move instance. Don’t forget to override Object#equals() as well. See also: Overriding equals and hashCode in Java Hint: if you’re using an IDE like Eclipse, you can also just autogenerate them. … Read more
Using the Stream API (Java 8+) boolean allEqual = list.stream().distinct().limit(2).count() <= 1 or boolean allEqual = list.isEmpty() || list.stream().allMatch(list.get(0)::equals); Using a Set: boolean allEqual = new HashSet<String>(tempList).size() <= 1; Using a loop: boolean allEqual = true; for (String s : list) { if(!s.equals(list.get(0))) allEqual = false; } Issues with OP’s code Two issues with your … Read more
You can create an int[] from any Collection<Integer> (including a HashSet<Integer>) using Java 8 streams: int[] array = coll.stream().mapToInt(Number::intValue).toArray(); The library is still iterating over the collection (or other stream source) on your behalf, of course. In addition to being concise and having no external library dependencies, streams also let you go parallel if you … Read more
Java’s Set and Map interfaces specify two very different collection types. A Set is just what it sounds like: a collection of distinct (non-equal) objects, with no other structure. A Map is, conceptually, also just what it sounds like: a mapping from a set of objects (the distinct keys) to a collection of objects (the … Read more
I’m guessing that you are creating a new Quotes with the same values. In this case they are not equal. If they should be considered equal, override the Equals and GetHashCode methods. public class Quotes{ public string symbol; public string extension public override bool Equals(object obj) { Quotes q = obj as Quotes; return q … Read more
Actually, it’s not just HashSet. All implementations of the Set interface in Java 6 are based on an underlying Map. This is not a requirement; it’s just the way the implementation is. You can see for yourself by checking out the documentation for the various implementations of Set. Your main questions are But, why is … Read more