collections

Element order in BlockingCollection<>

冷暖自知 提交于 2020-01-02 00:57:09
问题 I have a Download Queue implemented with BlockingCollection<> . Now I want to prioritize some Download once in a while. I thought it might be great to move some elements 'up' the Collection, like in a list, but there is no method like Remove()/AddFirst() or Move(). What's the preferred way of arranging items in a BlockingCollection<> ? 回答1: BlockingCollection<T> works by wrapping an internal IProducerConsumerCollection<T>. The default is to use a ConcurrentQueue<T> internally, but you can

linq remove item from object array where property equals value

非 Y 不嫁゛ 提交于 2020-01-02 00:56:08
问题 If i have IEnumberable<Car> list and i want to remove an item from this list based on a property of the car i want something like: list.RemoveWhere(r=>r.Year > 2000) does something like this exist ? i am doing this over and over so i want to avoid copying the list each time to just remove one item 回答1: IEnumberable is immutable, but you can do something like this: list = list.Where(r=>r.Year<=2000) or write an extension method: public static IEnumerable<T> RemoveWhere<T>(this IEnumerable<T>

Sort a list with SQL or as a collection?

房东的猫 提交于 2020-01-02 00:55:12
问题 I have some entries with dates in my database. What is best?: Fetch them with a sql statement and also apply order by . Get the list with sql, and order them within the application with collection.sort or so? Thanks 回答1: To some extent, it depends on how many values are in the complete collection. If it is, say, 20-30 values then you can sort anywhere — even a relatively poor sorting algorithm can do that quickly (avoid Stooge Sort though; that's terrible) — as that is the sort of size of

Why does the Collections class contain standalone (static) methods, instead of them being added to the List interface?

烂漫一生 提交于 2020-01-02 00:52:08
问题 For all the methods in Collections that take a List as their first argument, why aren't those methods simply part of the List interface? My intuition is: given a List object, that object itself should "know" how to perform on itself operations such as rotate(), shuffle(), or reverse(). But instead, as a Java programmer, I have to review both the methods in the List interface, as well as the static methods "over there" in the Collections class, to ensure I'm using a canonical solution. Why

HashMap returned by Maps.newHashMap vs new HashMap

霸气de小男生 提交于 2020-01-02 00:43:08
问题 I am trying Guava for the first time and I find it really awesome. I am executing few parameterized retrieve queries on a Spring jdbc template. The method in the DAO ( AbstractDataAccessObject ) goes like this. No problem here. public Map<String,Object> getResultAsMap(String sql, Map<String,Object> parameters) { try { return jdbcTemplate.queryForMap(sql, parameters); } catch (EmptyResultDataAccessException e) { //Ignore if no data found for this query logger.error(e.getMessage(), e); } return

Backbone: Create collection from JSON

随声附和 提交于 2020-01-02 00:39:06
问题 I'm attempting to load JSON (from php's json_encode) into a Backbone JS collection. I've simplified the problem to: var myJSON = '[{ "id":"1","name":"some name","description":"hmmm"}]'; var myCollection = new MyCollection(myJSON, { view: this }); And: MyObject = Backbone.Model.extend({ id: null, name: null, description: null }); MyCollection = Backbone.Collection.extend({ model: MyObject, initialize: function (models,options) { } }); Error: Uncaught TypeError: Cannot use 'in' operator to

C Language Standard Collections Where Are They?

狂风中的少年 提交于 2020-01-02 00:32:12
问题 I have committed to learning C now, I'm good with Python/PHP/Bash but I've decided I'm limited by not being fluent in C. However I cannot imagine working in a language without lists and hashes, maybe I'm just jumping a gun, but surely there are 'standard' collection libraries. I do not see any in the GNU standard lib though, any suggestions? 回答1: There is no "standard" set of collection classes for C. Many people simply roll their own as needed. But of course, there are some libraries filling

Convert java.util.stream.Stream to Scala Stream

非 Y 不嫁゛ 提交于 2020-01-01 14:23:21
问题 I know how I can use the Java libraries, and I can write some loops that do the stuff I need for me, but the question is more, why is there nothing in scala.collection.JavaConverters or scala.collection.JavaConverstions to convert a java.util.stream.Stream to a scala.collection.immutable.Stream ? I would like to do something like this: def streamFiles(path: Path): Stream[Path] = { Files.newDirectoryStream(path).asScala } But instead I have to write something like this: def streamFiles(path:

Convert java.util.stream.Stream to Scala Stream

别等时光非礼了梦想. 提交于 2020-01-01 14:20:20
问题 I know how I can use the Java libraries, and I can write some loops that do the stuff I need for me, but the question is more, why is there nothing in scala.collection.JavaConverters or scala.collection.JavaConverstions to convert a java.util.stream.Stream to a scala.collection.immutable.Stream ? I would like to do something like this: def streamFiles(path: Path): Stream[Path] = { Files.newDirectoryStream(path).asScala } But instead I have to write something like this: def streamFiles(path:

refactoring Java arrays and primitives (double[][]) to Collections and Generics (List<List<Double>>)

为君一笑 提交于 2020-01-01 10:56:10
问题 I have been refactoring throwaway code which I wrote some years ago in a FORTRAN-like style. Most of the code is now much more organized and readable. However the heart of the algorithm (which is performance-critical) uses 1- and 2-dimensional Java arrays and is typified by: for (int j = 1; j < len[1]+1; j++) { int jj = (cont == BY_TYPE) ? seq[1][j-1] : j-1; for (int i = 1; i < len[0]+1; i++) { matrix[i][j] = matrix[i-1][j] + gap; double m = matrix[i][j-1] + gap; if (m > matrix[i][j]) {