collections

Is List<List<String>> an instance of Collection<Collection<T>>?

半腔热情 提交于 2020-01-01 09:13:18
问题 I wrote this handy, generic function for converting a collection of collections into a single set: public static <T> Set<T> makeSet(Collection<Collection<T>> a_collection) { Iterator<Collection<T>> it = a_collection.iterator(); Set<T> result = new HashSet<T>(); while (it.hasNext()) { result.addAll(it.next()); } return result; } Then I tried to call it: List<List<String>> resultLists = ... ; Set<String> labelsSet = CollectionsHelper.makeSet(resultLists); and I received the following error: <T

Is List<List<String>> an instance of Collection<Collection<T>>?

﹥>﹥吖頭↗ 提交于 2020-01-01 09:13:12
问题 I wrote this handy, generic function for converting a collection of collections into a single set: public static <T> Set<T> makeSet(Collection<Collection<T>> a_collection) { Iterator<Collection<T>> it = a_collection.iterator(); Set<T> result = new HashSet<T>(); while (it.hasNext()) { result.addAll(it.next()); } return result; } Then I tried to call it: List<List<String>> resultLists = ... ; Set<String> labelsSet = CollectionsHelper.makeSet(resultLists); and I received the following error: <T

whats the fastest string collection structure/algorithm for startswith and/or contains searches

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-01 08:57:25
问题 I have the following situation: I have a big collection of strings (lets say 250.000+) of average length of maybe 30. What I have to do is to do many searches within these .. mostly those will be of StartsWith and Contains kind. The collection is static at runtime. Which means the initial reading and filling of the collection of choice is done only once .. therefore the performance of building the datastructure is absolutely not important. Memory is also not a problem: which also means that I

What does a hashset do with memory when initializing a collection?

天涯浪子 提交于 2020-01-01 08:48:09
问题 I stumbled upon the following problem. I want a hashset with all numbers from 1 to 100.000.000. I tried the following code: var mySet = new HashSet<int>(); for (var k = 1; k <= 100000000; k++) mySet.Add(k); That code didn't make it since I got memory overflow somewhere around the 49mil. This was also pretty slow and memory grew excessively. Then I tried this. var mySet = Enumerable.Range(1, 100000000).ToHashSet(); where ToHashSet() is the following code: public static HashSet<T> ToHashSet<T>

Array list algorithm - Interview

雨燕双飞 提交于 2020-01-01 08:45:17
问题 I was asked this question in an interview today. I have tried a solution but would like to know if there's a better way to solve this: Question : I have an arraylist say of 500,000 elements such that the value of each element of the arraylist is same as the index. For ex: list.get(0) = 0; list.get(1) = 1 ...etc. But only one element is out of sync with this ordering [i.e list.get(i) != i]. How do you find that element. My Answer : Iterate over the list using multiple threads each thread

get only the first 20 items in a backbone collection

这一生的挚爱 提交于 2020-01-01 07:51:47
问题 I'm trying to render only the first 20 items in a backbonejs collection. If the collection was an array I would do _.first(collection, 20). But since it's not it doesn't work. How do I limit it to 20? Thanks. 回答1: Backbone collections have underscore methods builtin: collection.first(20) 来源: https://stackoverflow.com/questions/8240382/get-only-the-first-20-items-in-a-backbone-collection

Why doesn't CopyOnWriteArraySet implement the Cloneable interface, while CopyOnWriteArrayList does?

寵の児 提交于 2020-01-01 07:11:08
问题 In this bug report, Doug Lea writes (referring to a pre-release version of JDK 5.0): While CopyOnWriteArraySet is declared Cloneable , it fails to define public clone method. But it eventually ends up that CopyOnWriteArraySet doesn't implement the Cloneable interface at all! (This is true in Java SE 6, 7, and 8.) How is CopyOnWriteArraySet different from CopyOnWriteArrayList with respect to cloning? There is a sound reason nobody ever wants to clone it? P.S. I understand that clone() is not

Find a special number in an array

十年热恋 提交于 2020-01-01 06:36:06
问题 There are many numbers in an array and each number appears three times excepting for one special number appearing once. Here is the question: how can I find the special number in the array? Now I can only put forward some methods with radix sorting and rapid sorting which cannot takes advantage the property of the question. So I need some other algorithms. Thanks for your help. 回答1: Add the numbers bitwise mod 3, e.g. def special(lst): ones = 0 twos = 0 for x in lst: twos |= ones & x ones ^=

Sort and group objects alphabetically by first letter Javascript

陌路散爱 提交于 2020-01-01 05:27:06
问题 Im trying to create a collection like this in order to use in a react component: let data = [ { group : 'A', children : [ { name : 'Animals', id : 22 }, ... ] }, { group : 'B', children : [ { name : 'Batteries', id : 7}, { name : 'Baggage', id : 12 }, ... ] }, { group : 'C', children : [ { name : 'Cake', id : 7}, ... ] }, ] I've already sort my data like this : let rawData = [ { name : 'Animals', id : 10}, { name : 'Batteries', id : 7}, { name : 'Baggage', id : 12 }, { name : 'Cake', id : 7},

How to group elements of a List by elements of another in Java 8

[亡魂溺海] 提交于 2020-01-01 05:19:12
问题 I have the following problem: Given these classes, class Person { private String zip; ... public String getZip(){ return zip; } } class Region { private List<String> zipCodes; ... public List<String> getZipCodes() { return zipCodes; } } using the Java 8 Stream API, how do I obtain a Map<Person, List<Region>> based on whether the Region contains that Person 's zip code? In other words how do I group the regions by the people whose zip codes belong to those regions? I've done it in Java 7 the