collections

How to copy Java Collections list

半城伤御伤魂 提交于 2019-12-17 04:40:35
问题 I have an ArrayList and I want to copy it exactly. I use utility classes when possible on the assumption that someone spent some time making it correct. So naturally, I end up with the Collections class which contains a copy method. Suppose I have the following: List<String> a = new ArrayList<String>(); a.add("a"); a.add("b"); a.add("c"); List<String> b = new ArrayList<String>(a.size()); Collections.copy(b,a); This fails because basically it thinks b isn't big enough to hold a . Yes I know b

How to copy Java Collections list

那年仲夏 提交于 2019-12-17 04:40:09
问题 I have an ArrayList and I want to copy it exactly. I use utility classes when possible on the assumption that someone spent some time making it correct. So naturally, I end up with the Collections class which contains a copy method. Suppose I have the following: List<String> a = new ArrayList<String>(); a.add("a"); a.add("b"); a.add("c"); List<String> b = new ArrayList<String>(a.size()); Collections.copy(b,a); This fails because basically it thinks b isn't big enough to hold a . Yes I know b

Why does WCF return myObject[] instead of List<T> like I was expecting?

你。 提交于 2019-12-17 04:21:41
问题 I am returning a List from my WCF method. In my client code, it's return type shows as MyObject[]. I have to either use MyObject[], or IList, or IEnumerable... WCFClient myClient = new WCFClient(); MyObject[] list = myClient.GetMyStuff(); or IList<MyObject> list = myClient.GetMyStuff(); or IEnumerable<MyObject> list = myClient.GetMyStuff(); All I am doing is taking this collection and binding it to a grid. What is the best object to assign my returned collection? 回答1: You can specify that you

Binding a list in @RequestParam

☆樱花仙子☆ 提交于 2019-12-17 04:17:40
问题 I'm sending some parameters from a form in this way: myparam[0] : 'myValue1' myparam[1] : 'myValue2' myparam[2] : 'myValue3' otherParam : 'otherValue' anotherParam : 'anotherValue' ... I know I can get all the params in the controller method by adding a parameter like public String controllerMethod(@RequestParam Map<String, String> params){ .... } I want to bind the parameters myParam[] (not the other ones) to a list or array (anything that keeps the index order), so I've tried with a syntax

The opposite of Intersect()

♀尐吖头ヾ 提交于 2019-12-17 04:14:49
问题 Intersect can be used to find matches between two collections, like so: // Assign two arrays. int[] array1 = { 1, 2, 3 }; int[] array2 = { 2, 3, 4 }; // Call Intersect extension method. var intersect = array1.Intersect(array2); // Write intersection to screen. foreach (int value in intersect) { Console.WriteLine(value); // Output: 2, 3 } However what I'd like to achieve is the opposite, I'd like to list items from one collection that are missing from the other : // Assign two arrays. int[]

Getting an element from a Set

扶醉桌前 提交于 2019-12-17 04:12:12
问题 Why doesn't Set provide an operation to get an element that equals another element? Set<Foo> set = ...; ... Foo foo = new Foo(1, 2, 3); Foo bar = set.get(foo); // get the Foo element from the Set that equals foo I can ask whether the Set contains an element equal to bar , so why can't I get that element? :( To clarify, the equals method is overridden, but it only checks one of the fields, not all. So two Foo objects that are considered equal can actually have different values, that's why I

What .NET collection provides the fastest search

泪湿孤枕 提交于 2019-12-17 03:49:29
问题 I have 60k items that need to be checked against a 20k lookup list. Is there a collection object (like List , HashTable ) that provides an exceptionly fast Contains() method? Or will I have to write my own? In otherwords, is the default Contains() method just scan each item or does it use a better search algorithm. foreach (Record item in LargeCollection) { if (LookupCollection.Contains(item.Key)) { // Do something } } Note . The lookup list is already sorted. 回答1: In the most general case,

Java - generate Random range of specific numbers without duplication of those numbers - how to?

爱⌒轻易说出口 提交于 2019-12-17 03:44:08
问题 Sounds simple enough...but I've been plugging away at this, trying to find the one and all solution. For a range of numbers, say 1-12 , I want to generate a random sequence within that range, and include 1 and 12 . I don't want duplicate numbers though . So I would want something like this - 3,1,8,6,5,4 ..and so on, every number from 1-12. Then I want to put these random numbers into an Array and use that array to 'randomly' select and display some items (like inventory pulled from database)

Iterating through a list in reverse order in java

余生长醉 提交于 2019-12-17 03:22:25
问题 I'm migrating a piece of code to make use of generics. One argument for doing so is that the for loop is much cleaner than keeping track of indexes, or using an explicit iterator. In about half the cases, the list (an ArrayList) is being iterated in reverse order by using an index today. Can someone suggest a cleaner way of doing this (since I dislike the indexed for loop when working with collections), though it does work? for (int i = nodes.size() - 1; i >= 0; i--) { final Node each = (Node

Immutable vs Unmodifiable collection

廉价感情. 提交于 2019-12-17 03:21:48
问题 From the Collections Framework Overview: Collections that do not support modification operations (such as add , remove and clear ) are referred to as unmodifiable . Collections that are not unmodifiable are modifiable . Collections that additionally guarantee that no change in the Collection object will be visible are referred to as immutable . Collections that are not immutable are mutable . I cannot understand the distinction. What is the difference between unmodifiable and immutable here?