Does .asSet(…) exist in any API?
Now with Java 8 you can do this without need of third-party framework: Set<String> set = Stream.of(“a”,”b”,”c”).collect(Collectors.toSet()); See Collectors. Enjoy!
Now with Java 8 you can do this without need of third-party framework: Set<String> set = Stream.of(“a”,”b”,”c”).collect(Collectors.toSet()); See Collectors. Enjoy!
Although JS implementations might keep track of such a value internally, there’s no standard way to get it. In the past, Mozilla’s Javascript variant exposed the non-standard __count__, but it has been removed with version 1.8.5. For cross-browser scripting you’re stuck with explicitly iterating over the properties and checking hasOwnProperty(): function countProperties(obj) { var count … Read more
List<String> strings = Arrays.asList(new String[]{“one”, “two”, “three”}); This is a list view of the array, the list is partly unmodifiable, you can’t add or delete elements. But the time complexity is O(1). If you want a modifiable a List: List<String> strings = new ArrayList<String>(Arrays.asList(new String[]{“one”, “two”, “three”})); This will copy all elements from the source … Read more
It was a Java design decision, and one that some consider a mistake. Containers want Objects and primitives don’t derive from Object. This is one place that .NET designers learned from the JVM and implemented value types and generics such that boxing is eliminated in many cases. In CLR, generic containers can store value types … Read more
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); … Read more
Where x is the collection: Foo[] foos = x.toArray(new Foo[x.size()]);
In Xcode 7, Apple has introduced ‘Lightweight Generics’ to Objective-C. In Objective-C, they will generate compiler warnings if there is a type mismatch. NSArray<NSString*>* arr = @[@”str”]; NSString* string = [arr objectAtIndex:0]; NSNumber* number = [arr objectAtIndex:0]; // Warning: Incompatible pointer types initializing ‘NSNumber *’ with an expression of type ‘NSString *’ And in Swift … Read more
List<int> list = …; string.Join(“,”, list.Select(n => n.ToString()).ToArray())
Since I couldn’t find a similar flowchart I decided to make one myself. This flow chart does not try and cover things like synchronized access, thread safety etc or the legacy collections, but it does cover the 3 standard Sets, 3 standard Maps and 2 standard Lists. This image was created for this answer and … Read more
With Java 8 it is so simple so it doesn’t even need separate method anymore: List<Integer> range = IntStream.rangeClosed(start, end) .boxed().collect(Collectors.toList()); And in Java 16 or later: List<Integer> range = IntStream.rangeClosed(start, end) .boxed().toList();