collections

Synchronized List/Map in Java if only one thread is writing to it

最后都变了- 提交于 2019-12-24 04:06:47
问题 The first thread is filling a collection continuously with objects. A second thread needs to iterate over these objects, but it will not change the collection. Currently I use Collection.synchronized for making it thread-safe, but is there a fast way to doing it? Update It's simple: The first thread (ui) continuously writes the mouse position to the ArrayList, as long as the mousebutton is pressed down. The second thread (render) draws a line based on the list. 回答1: Even if you synchronize

“Collection was modified…” Issue

痞子三分冷 提交于 2019-12-24 03:35:09
问题 I've got a function that checks a list of objects to see if they've been clicked and fires the OnClick events accordingly. I believe the function is working correctly, however I'm having an issue: When I hook onto one of the OnClick events and remove and insert the element into a different position in the list (typical functionality for this program), I get the "Collection was modified..." error. I believe I understand what is going on: The function cycles through each object firing OnClick

Spring Security method rules: returned value contains a Collection

£可爱£侵袭症+ 提交于 2019-12-24 03:31:01
问题 In Spring Security I want to secure a method incorporating returned values and using @PostAuthorize . I want to add a constraing that will not allow one user to access to resources they are not owners. The problem I face is that I want to check principal id against one collection of values. Scenario: Domain objects: public class Car implements Serializable { private Integer id; private Collection<Driver> drivers; ... } public class Driver implements Serializable { private Integer id; ... }

Instantiating a Generic Class of Type <?>

烈酒焚心 提交于 2019-12-24 03:29:46
问题 I'm studying for the SCJP/OCPJP and I came across a sample question that seams strange to me. The sample code instantiated two generic collections: List<?> list = new ArrayList<?>(); List<? extends Object> list2 = new ArrayList<? extends Object>(); The "correct" answer to the question was that this code would compile but adding to either collection would produce a runtime error. When I try to compile code like this I just get errors. The Java tutorial does not even show this type of code, it

Java Object reference

风流意气都作罢 提交于 2019-12-24 03:23:38
问题 This is regarding the usage of ArrayList returned by another class instance variable. Class A { //assigned list of string to it. private List < String > newAl; //returns the list public List < String > getList() { return newA1; } } Class Test { public void go() { List < String > list = a.getList(); list.add(""); } } In the Test class when i retreive list and manipulate the list.Because of the reference ,class A list also got manipulated.If A is part of third party code.How do I correct my

How to add values in swift dictionary dynamically

北战南征 提交于 2019-12-24 02:43:16
问题 I have declared a dictionary in my swift class as below : var profileDetail = Dictionary<String, String>() But I'm not able to add values in it. IF I try like following, It only stores the last value. profileDetail = [profileItem[1]:values.valueForKey("middle_name") as String] profileDetail = [profileItem[2]:values.valueForKey("last_name") as String] profileDetail = [profileItem[3]:values.valueForKey("gender") as String] profileDetail = [profileItem[4]:values.valueForKey("birth_date") as

Fill immutable map with for loop upon creation

六月ゝ 毕业季﹏ 提交于 2019-12-24 02:21:35
问题 I have this map that looks like this: val fields: Map[(Int, Int), Field] and I thought about doing something like: val fields: Map[(Int, Int), Field] = Map( for(a <- 0 to 10) { (0, a) -> new Field(this, 0, a) } ) instead of a long copy/paste list like: (0, 0) -> new Field(this, 0, 0), (1, 0) -> new Field(this, 1, 0), (2, 0) -> new Field(this, 2, 0), (3, 0) -> new Field(this, 3, 0), (4, 0) -> new Field(this, 4, 0), (5, 0) -> new Field(this, 5, 0), (6, 0) -> new Field(this, 6, 0), (7, 0) -> new

Java 8 - filter collection using many filters [duplicate]

一笑奈何 提交于 2019-12-24 01:44:30
问题 This question already has answers here : How to apply multiple predicates to a java.util.Stream? (4 answers) Closed 3 years ago . I would like to filter out my collection using multiple filters. Let's assume I have a list of Strings and a function filter() to filter out empty Strings. List<String> myList = ....... Typically, I would use streams like this: myList.stream() .filter(elem -> filterOut(elem)) .collect(Collectors.toList()); How to apply multiple filters from a collection ( List or

Check if an item exist in the dictionary and remove it from the dictionary in C# [duplicate]

一曲冷凌霜 提交于 2019-12-24 01:44:13
问题 This question already has answers here : Remove Item in Dictionary based on Value (6 answers) Closed 6 years ago . The question should be clear from the title itself. I need to check if an item exist in the dictionary and remove it from the dictionary in C#. The only catch is that i have to do this using only the value item and not the key. The declaration is as below: IDictionary<string, myCustomClassObject> clients = new IDictionary<string, myCustomClassObject>(); Now i fill in the

mongodb recovery removed records

微笑、不失礼 提交于 2019-12-24 01:24:25
问题 I have a two-member replica set, I accidentally removed all documents in an collection, I am not sure how I did this, but it's gone. Is it possible to get all the data back? 回答1: Unless you have a backup (always recommended for just this type of thing), or one of the replicas is using slavedelay, then I am afraid the removal of the records is final. You might have been able to force a shutdown in time to save the data on-disk if you killed the process before the next fsync to disk (similarly