collections

Retrieve fixed number of entries around an entry in a map sorted by values

笑着哭i 提交于 2019-12-22 08:30:25
问题 The POJO viz. Entry.java represents an entry in the leaderboard. Position is the position in the leaderboard, 1 being the user with the highest score public class Entry { private String uid; private int score; private int position; @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + score; result = prime * result + ((uid == null) ? 0 : uid.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if

Is it Possible to Return Two ArrayList values in One method in java?

不想你离开。 提交于 2019-12-22 08:27:02
问题 Is there any way to return two values from one method.... Example: public Class Sample { public List<Date> returnTwoArrayList() { List<Date> startDate=new ArrayList<Date>(); List<Date> endDate=new ArrayList<Date>(); //So I want to Return Two values startDate and endDate ...It is Possible???? } } Edit:1 i'm calling this method into My Service class and in that Storing StartDate and endDate into Database here these are Two different Columns **StartDate endDate** 2012-12-01 2012-12-05 2012-12-01

Best Java thread-safe locking mechanism for collections?

与世无争的帅哥 提交于 2019-12-22 08:25:45
问题 What would be the least-slow thread-safe mechanism for controlling multiple accesses to a collection in Java? I am adding objects to the top of a collection and i am very unsure what would be the best performing collection. Would it be a vector or a queue? I originally thought an ArrayList would be fast but i ran some experiments and it was very slow. EDIT: In my insertion testing a Vector delared using volatile seems to be the fastest? 回答1: The exact operations used should determine what is

Scala nested arrays flattening

会有一股神秘感。 提交于 2019-12-22 08:16:46
问题 How to flatten an array of nested arrays of any depth ? For instance val in = Array( 1, Array(2,3), 4, Array(Array(5)) ) would be flattened onto val out = Array(1,2,3,4,5) Thanks in Advance. 回答1: If you have mixed Int and Array[Int] , which is not a very good idea to begin with, you can do something like in.flatMap{ case i: Int => Array(i); case ai: Array[Int] => ai } (it will throw an exception if you've put something else in your array). You can thus use this as the basis of a recursive

Fastest way to find out whether two ICollection<T> collections contain the same objects

拈花ヽ惹草 提交于 2019-12-22 08:02:23
问题 What is the fastest way to find out whether two ICollection<T> collections contain precisely the same entries? Brute force is clear, I was wondering if there is a more elegant method. We are using C# 2.0, so no extension methods if possible, please! Edit: the answer would be interesting both for ordered and unordered collections, and would hopefully be different for each. 回答1: use C5 http://www.itu.dk/research/c5/ ContainsAll " Check if all items in a supplied collection is in this bag

Collection modify item

微笑、不失礼 提交于 2019-12-22 07:08:13
问题 I have read tons of articles on choosing the correct collection for a specific implementation, and I understand that in the end it will come down to benchmarking real data, but while I'm busy doing that: What sorted collection in c# allows the modification of an item contained?I can't seem to find any? Is this because a modify would probably be implemented as a removal then re-insertion, thus making an explicit 'Modify' function pointless? I am in need of a collection (custom or standard

VB6 Collection Remove Doesn't Fire Class_Terminate

血红的双手。 提交于 2019-12-22 07:02:34
问题 I apologize in advance; this is a long question. I've tried to simplify as much as I can but it's still a bit more long-winded than I'd care to see. In some legacy code, we've got a VB6 collection. This collection adds objects via the .Add method and removes them via the .Remove method. However, via tracing I can see that sometimes when the .Remove is called it appears that the class terminate for the object isn't called. But it's not consistent; it happens only rarely and I can't isolate the

Scala: Get sum of nth element from tuple array/RDD

一个人想着一个人 提交于 2019-12-22 06:41:28
问题 I have a array of tuple like this: val a = Array((1,2,3), (2,3,4)) I want to write a generic method for a method like below: def sum2nd(aa: Array[(Int, Int, Int)]) = { aa.map { a => a._2 }.sum } So what I am looking for a method like: def sumNth(aa: Array[(Int, Int, Int)], n: Int) 回答1: There are a few ways you can go about this. The simplest is to use productElement : def unsafeSumNth[P <: Product](xs: Seq[P], n: Int): Int = xs.map(_.productElement(n).asInstanceOf[Int]).sum And then (note

Collections.synchronizedlist() remove element while iterating from end [duplicate]

心已入冬 提交于 2019-12-22 06:39:30
问题 This question already has answers here : Collections.synchronizedList and synchronized (6 answers) Closed 3 years ago . I am using Collections.Synchronizedlist() to make my arraylist thread safe. What I want to ask is the following code thread-safe i.e. remove while iterating over list from the end:- pendingExecutionList = Collections.synchronizedList(new ArrayList<>(initialCapacity)); I am creating the list in main thread. and adding to this list from different threads. But, iteration and

Immutable collections in Doctrine 2?

北城余情 提交于 2019-12-22 06:03:17
问题 I'm looking for a way to return an immutable collection from a domain object in Doctrine 2. Let's start with this example from the doc: class User { // ... public function getGroups() { return $this->groups; } } // ... $user = new User(); $user->getGroups()->add($group); From a DDD point of view, if User is the aggregate root, then we'd prefer: $user = new User(); $user->addGroup($group); But still, if we do need the getGroups() method as well, then we ideally don't want to return the