collections

How to split an array to 2 arrays with odd and even indices respectively? [duplicate]

末鹿安然 提交于 2019-12-24 01:18:02
问题 This question already has answers here : Getting odd/even part of a sequence with LINQ (7 answers) Closed 3 years ago . How to split an array to 2 arrays with odd and even indices respectively? For example int[] a = new int[]{1, 3, 7, 8}; then get two arrays a1: {1, 7} a2: {3, 8} 回答1: Simple using the overload of Where than contains the index which: Filters a sequence of values based on a predicate. Each element's index is used in the logic of the predicate function. int[] a = new int[] { 1,

How to show tree-view replies to message? Java & Hibernate

泄露秘密 提交于 2019-12-24 01:17:52
问题 I have message, and need to show ten (for example) first 'root' replies with all the replies to itselfs, and it should look like a tree. (Standard messages and tree replies view, you know). So, the question is - how to get it from DB - i'm using hibernate, and afaik it will get a lot of time - to retrieve the WHOLE collection itself, with all subtrees, recursively. (And, maybe, its good only for small-sized collections, otherwise recursion will cause a stack overflow (Ha-ha. Here we are :) )

Why List.contains() takes Object as an argument in Collections Java [duplicate]

情到浓时终转凉″ 提交于 2019-12-24 01:16:08
问题 This question already has answers here : Why aren't Java Collections remove methods generic? (10 answers) Closed 5 years ago . The java.util.List.contains(Object o) method takes Object as an argument and internally uses Object.equals(Object o) as described here. If I do the following code in Netbeans: List<String> listStr = new ArrayList<>(); listStr.contains(34); //warning it gives the obvious warning, that is: Given object can not contain instances of int (expected String) Since it visible

Why List.contains() takes Object as an argument in Collections Java [duplicate]

廉价感情. 提交于 2019-12-24 01:14:03
问题 This question already has answers here : Why aren't Java Collections remove methods generic? (10 answers) Closed 5 years ago . The java.util.List.contains(Object o) method takes Object as an argument and internally uses Object.equals(Object o) as described here. If I do the following code in Netbeans: List<String> listStr = new ArrayList<>(); listStr.contains(34); //warning it gives the obvious warning, that is: Given object can not contain instances of int (expected String) Since it visible

Different occurrences of a specific key in an array of hashes

我怕爱的太早我们不能终老 提交于 2019-12-24 01:08:12
问题 I have an array like that: array = [{"id"=>"id1", "email"=>"name@organization.com", "sess"=>"sess1"}, {"id"=>"id2", "email"=>"name@organization.com", "sess"=>"sess2"}, {"id"=>"id3", "email"=>"name@organization.com", "sess"=>"sess2"}, {"id"=>"id4", "email"=>"name@organization.com", "sess"=>"sess3"}, {"id"=>"id5", "email"=>"name@organization.com", "sess"=>"sess2"}, {"id"=>"id6", "email"=>"name@organization.com", "sess"=>"sess3"}, {"id"=>"id7", "email"=>"name@organization.com", "sess"=>"sess2"},

Java ArrayList and LinkedList - adding element at end implementation details

时间秒杀一切 提交于 2019-12-24 01:00:01
问题 My understanding of why arraylist is faster than a linkedlist is that with an arraylist you basically only need one action - update the reference at the end array element, whereas with a linked list you have to do much more e.g. create a new node, update 2 references, go through linked list and update the last node to point to the new one etc. However I am not sure how java implement these. How does the arraylist know where the "last" element is, does it store a value of the last element or

How to clear out the contents of a map when clear() method call throws UnsupportedOperationException?

二次信任 提交于 2019-12-24 01:00:00
问题 How can I empty a map contents if I keep getting an UnsupportedOperationException when I call clear() method on the map instance ? 回答1: You shouldn't. There is something wrong in the contracts of the collaborating objects and their methods. You expect a modifiable map, but get an unmodifiable view. You should redefine the contracts and/or adopt any of the implementations accordingly. If you are not in control of the one, who is passing you that map, then there is probably a reason for it

Retrieve all entries from Map<Integer, String> with keys in certain range

你离开我真会死。 提交于 2019-12-24 00:52:45
问题 I need to have a HashMap< Integer, String> which can serve fast operations for retrieving a list of all entries whose keys are in a certain integer range besides, getting values from map based on keys. What Map implementation is suitable for these needs ? 回答1: Use a TreeMap , which implements NavigableMap supplying a subMap method returning a view of the map with only keys in your range. To get the values, of course you call values() on the result. If you have an existing Map whose keys

AutoMapping Array to a List

≯℡__Kan透↙ 提交于 2019-12-24 00:47:19
问题 class A { public List<string> list; } class B { public string[] array; } How would you map this? I've tried CreateMap<A,B>(); That doesn't work 回答1: Your first issue is going to be that the class members don't match. If they did, I'd imagine that this would work. If not, you just have to specify your mapping rather than letting Automapper infer it: CreateMap<A,B>() .ForMember(d => d.array, opts => opts.MapFrom(s => s.list.ToArray()); 回答2: For a shortcut, a vb.net version CreateMap(Of A, B)()

How to accept a collection of a base type in WCF

只愿长相守 提交于 2019-12-24 00:43:35
问题 The Setup I have a WCF service that exposes a base type (e.g. Animal) as well as a few derived types (e.g. Lion, Tiger, and Bear). Another type (e.g. Zoo) includes a property that is a collection of the base type. The base type is concrete, not abstract, so it is perfectly acceptable for the collection to contain instances of the base type and/or the derived types (in any combination). For example: [DataContract, KnownType(typeof(Lion)), KnownType(typeof(Tiger)), KnownType(typeof(Bear))]