collections

Priority queue is not maintaining sorting order

非 Y 不嫁゛ 提交于 2019-12-24 18:30:50
问题 Priority queue is not maintaining sorting order Am i implementing Comparable not properly? Wrong sorting order is coming as output? import java.util.PriorityQueue; class A implements Comparable { int i; A(int i) { this.i = i; } public int compareTo(Object obj) { return i - ((A)obj).i; } public String toString() { return Integer.toString(i); } } class Manager11 { public static void main(String[] args) { PriorityQueue pq = new PriorityQueue(); pq.add(new A(9)); pq.add(new A(5)); pq.add(new A(8)

C# get all property conditionally from dynamic collection (which looks like tree)

青春壹個敷衍的年華 提交于 2019-12-24 18:26:55
问题 let's say I have a dynamic collection, which consists set of key-values. for example: [ { key: test1, value: [ { key:c1level1, value: [ { key:settings-key, value: c1level2Val }, { key: types, value: typesVal } ] } ] }, { key: test2, value: [ { key:c2level1, value: [ { key:settings-key, value: c2level2Val }, { key: types, value: typesVal } ] } ] } ] and I want to get all the objects which have a key named "settings-key" and "types" and save them together in a List for example. How can I

Java extended collection filtering

谁都会走 提交于 2019-12-24 16:43:11
问题 I have to filter and to sort a ArrayList wíth objects. - Every object has 2 integer pairs => 4 ints per Object . - Every value of Column_1 < Column_2 and - Every value of Column_3 < Column_4 . ... so each pair represents a distance. 1.) Distance in 1st(Column_1,Column_2) pair and 2nd(Column_3, Column_4,) pair have to be equal. 2.) if there exists in the list a Obj_1 , whose Column_2 value is equal to Column_1 value+1 of Obj_2 and 3.) if there exists in the list a Obj_1 , whose Column_4 value

Dealing 7 random non-repeating “cards” out of a deck of 52

感情迁移 提交于 2019-12-24 16:27:05
问题 With the help of this forum I've designed a program to simulate a deck of cards. The final class in the program is meant to give two options: Option 1 will display the 52 cards, and option 2 will deal out 7 random (nonrepeating) cards. I'm using Collections.shuffle for this, but I'm getting Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 52 when I select option 2 (the program works and compiles fine otherwise). Here is the class---let me know if you need any information

Hash Collisions in HashMap

你说的曾经没有我的故事 提交于 2019-12-24 15:43:57
问题 I understand that there are two ways that a hash collision can occur in Java's HashMap , 1. hashCode() for Key Object produces same hash value as already produced one ( even if hash bucket is not full yet ) 2.Hash Bucket is already full so new Entry has to go at existing index. In case of Java's HashMap , situation#2 would really be rare due to so large number of allowed entries and automatic resizing ( See My other question ) Am I correct in my understanding? But for the sake of theoretical

Threadsafe and generic arraylist?

心已入冬 提交于 2019-12-24 15:26:06
问题 I want to have a generic thread safe collection and I saw that the Arraylist can easily be used thread safe by its static Synchronized method but what bugs me is that this ArrayList is not generic so when I want to use my objects I always have to cast them. Is there an easier way to do this? Also other list types would be possible. 回答1: A little knowledge is a dangerous thing ;-) Yes, you could use Meta-Knight's suggestion and use SyncRoot , but you need to be careful - it's not a panacea.

how to create a collection from list of strings that represents a directory structure in C# or VB

ε祈祈猫儿з 提交于 2019-12-24 15:13:45
问题 First, I did check this post but it is in Python, first, and second it appears to be actually making the directories, which I cannot do in this scenario. Second, these are not directories that exist, nor can I create them. I have an input in C# like this: List<string> filePaths = new List<string>(); filePaths.Add(@"ProgramDir\InstallDir\Module1\mod1pack1.exe"); filePaths.Add(@"ProgramDir\InstallDir\Module1\mod1pack2.exe"); filePaths.Add(@"ProgramDir\InstallDir\Module2\mod2pack1.exe");

Security question: how to secure Hibernate collections coming back from client to server?

走远了吗. 提交于 2019-12-24 15:13:33
问题 I've got a simple pojo named "Parent" which contains a collection of object "Child". In hibernate/jpa, it's simply a one-to-many association, children do not know their parent: these Child objects can have different type of Parent so it easier to not know the parent (think of Child which represents Tags and parents can be different object types which have tags). Now, I send my Parent object to the client view of my web site to allow user to modify it. For it, I use Hibernate/GWT/Gilead. My

Generic LazyCollection type

送分小仙女□ 提交于 2019-12-24 14:01:04
问题 I need a function that returns a lazy generator of various composed generator functions such as filter and map. For example, if I want to apply lazy.filter().map() the code looks like: // Simplified typealias MyComplexType = Int typealias MyComplexCollection = [MyComplexType] func selection() -> LazyMapCollection<LazyFilterCollection<MyComplexCollection>, Int> { let objects:MyComplexCollection = [1, 2, 3, 4, 5, 6] let result = objects.lazy.filter({$0 < 4}).map({$0 * 10}) return result } for

Evenly filter a list down by a certain percentage - Kotlin/Java

喜夏-厌秋 提交于 2019-12-24 13:03:29
问题 I'm looking for an most efficient way in Kotlin/Java to filter a List down by a certain percentage and with the removal of filtered elements will be applied across the collection in a uniformed fashion (i.e. - the elements to be removed span across the entire collection evenly); For Example filter the following by 50% [0,1,2,3,4,5,6,7,8,9] = [0,2,4,6,8] filter the following by 10% [1,100,1000,10000] = [1,100,10000] I came up with the following Kotlin extension function & it works great when