collections

Which HTTP code to use for an empty subresource in a REST API?

自作多情 提交于 2019-12-18 12:59:10
问题 Let's say I've a resource articles at /articles . These articles may have related articles, so I fetch them by GETting /articles/{id}/related . What should I return is there is no related articles? I can think of: 404 Not Found , maybe with an empty collection 204 No Content 200 Found with an empty collection Any advices? (please give arguments) By the way, it may applies to pagination. If I request page 3 of 2, then the page 3 will return an empty set, should It be a 404 ? 回答1: 404 is not

Which one runs faster, ArrayList or LinkedList? [duplicate]

流过昼夜 提交于 2019-12-18 12:55:30
问题 This question already has answers here : When to use LinkedList over ArrayList in Java? (32 answers) Performance differences between ArrayList and LinkedList (11 answers) Closed 6 years ago . List li = new LinkedList(); for (int i = 0; i < 100; i++) { li.add(i); } long start1 = System.nanoTime(); li.get(57); long end1 = System.nanoTime(); long diff1 = end1-start1; System.out.println("Time taken by LinkedList = "+diff1); List al = new ArrayList(); for (int i = 0; i < 100; i++) { al.add(i); }

Return collection as read-only

时间秒杀一切 提交于 2019-12-18 12:49:31
问题 I have an object in a multi-threaded environment that maintains a collection of information, e.g.: public IList<string> Data { get { return data; } } I currently have return data; wrapped by a ReaderWriterLockSlim to protect the collection from sharing violations. However, to be doubly sure, I'd like to return the collection as read-only, so that the calling code is unable to make changes to the collection, only view what's already there. Is this at all possible? 回答1: If your underlying data

Copy or clone a collection in Julia

China☆狼群 提交于 2019-12-18 12:24:11
问题 I have created a one-dimensional array(vector) in Julia, namely, a=[1, 2, 3, 4, 5] . Then I want to create a new vector b , where b has exactly same elements in a , i.e b=[1, 2, 3, 4, 5] . It seems that directly use b = a just create a pointer for the original collection, which means if I modify b and a is mutable, the modification will also be reflected in a . For example, if I use !pop(b) , then b=[1, 2, 3, 4] and a=[1, 2, 3, 4] . I am wondering if there is a official function to merely

Custom Collection in Doctrine2

◇◆丶佛笑我妖孽 提交于 2019-12-18 12:22:25
问题 Just starting to work with Doctrine2, and am wondering how/if I can use a custom collection class. Searches point me to this part of the documentation: Collection-valued persistent fields and properties must be defined in terms of the Doctrine\Common\Collections\Collection interface. The collection implementation type may be used by the application to initialize fields or properties before the entity is made persistent. Once the entity becomes managed (or detached), subsequent access must be

ImmutableCollections SetN implementation detail

ⅰ亾dé卋堺 提交于 2019-12-18 12:19:05
问题 I have sort of a hard time understanding an implementation detail from java-9 ImmutableCollections.SetN ; specifically why is there a need to increase the inner array twice. Suppose you do this: Set.of(1,2,3,4) // 4 elements, but internal array is 8 More exactly I perfectly understand why this is done (a double expansion) in case of a HashMap - where you never (almost) want the load_factor to be one. A value of !=1 improves search time as entries are better dispersed to buckets for example.

Java - Collections.sort() performance

可紊 提交于 2019-12-18 12:16:50
问题 Im using Collections.sort() to sort a LinkedList whose elements implements Comparable interface, so they are sorted in a natural order. In the javadoc documentation its said this method uses mergesort algorithm wich has n*log(n) performance. My question is if there is a more efficient algorithm to sort my LinkedList? The size of that list could be very high and sort will be also very frequent. Thanks! 回答1: O(N log N) is very good asymptotically. That said, there are linear time O(N) non

Does the unmodifiable wrapper for java collections make them thread safe?

人走茶凉 提交于 2019-12-18 12:14:15
问题 I need to make an ArrayList of ArrayLists thread safe. I also cannot have the client making changes to the collection. Will the unmodifiable wrapper make it thread safe or do I need two wrappers on the collection? 回答1: It depends. The wrapper will only prevent changes to the collection it wraps, not to the objects in the collection. If you have an ArrayList of ArrayLists, the global List as well as each of its element Lists need to be wrapped separately, and you may also have to do something

Why is there no mutable TreeMap in Scala?

心已入冬 提交于 2019-12-18 12:05:31
问题 Is it lack of time, some technical problem or is there a reason why it should not exist? 回答1: It's just a missing case that will presumably eventually be filled in. There is no reason not to do it, and in certain cases it would be considerably faster than the immutable tree (since modifications require log(n) object creations with an immutable tree and only 1 with a mutable tree). Edit: and in fact it was filled in in 2.12. Mutable TreeMap. (There is a corresponding Set also.) 回答2: Meanwhile

Why does it.next() throw java.util.ConcurrentModificationException?

我的梦境 提交于 2019-12-18 11:56:12
问题 final Multimap<Term, BooleanClause> terms = getTerms(bq); for (Term t : terms.keySet()) { Collection<BooleanClause> C = new HashSet(terms.get(t)); if (!C.isEmpty()) { for (Iterator<BooleanClause> it = C.iterator(); it.hasNext();) { BooleanClause c = it.next(); if(c.isSomething()) C.remove(c); } } } Not a SSCCE, but can you pick up the smell? 回答1: The Iterator for the HashSet class is a fail-fast iterator. From the documentation of the HashSet class: The iterators returned by this class's