ConcurrentModificationException when invoking putAll - java

I have difficulties in understanding the following error.
Suppose you have a class A in which I implement the following method:
Map<Double,Integer> get_friends(double user){
Map<Double,Integer> friends = user_to_user.row(user);
//friends.putAll(user_to_user.column(user));
return friends;}
Then in the main I do the following:
A obj = new A();
Map<Double,Integer> temp = obj.get_friends(6);
Well this works fine. However when I uncomment the follwing line in class A:
friends.putAll(user_to_user.column(user));
and I run again the program, it crashes and throws me the concurrentModificationException.
It is to be noted, that I am creating the Table user_to_user as follows:
private HashBasedTable<Double,Double,Integer> user_to_user;//
user_to_user = HashBasedTable.create();
What is further surprising is that when I interchange the way I am filling friends, I mean in that way:
Map<Double,Integer> friends = user_to_user.column(user);
friends.putAll(user_to_user.row(user));
Then everyting will work fine.
Any idea ?

The issue is that HashBasedTable is internally implemented as a Map<Double, Map<Double, Integer>>, and that the implementation of user_to_user.column(user) is iterating over the rows at the same time you're modifying the row associated with user.
One workable alternative would be to copy user_to_user.column(user) into a separate Map before putting it into the row.

Related

How do I get the value of a dictionary that contains a list of dictionaries in java?

I am working on a programm in Java which contains a part where I get a couple of information from my Python Server. It is a Hashmap that looks like this:
{hid=null, art=CWE, produktgruppe=23, objekt=Küche, betrag=714.989990234375, iban=DE0212, id=2812, varten=[{art=2, lz_min=24, lz_max=36, faktor=null, zinssatz=0.5, kondnr=84}], status=979}
How can I get the value of the key "zinssatz".
I tried it with
mymap.get("zinssatz");
But that always returns zero. Which is probably because it can't find the key inside my hashmap because it is build like a "Dictionary with a list of dictionaries" inside it.
Thanks for your help.
It seems to be a nested map. Have you tried the following:
((HashMap)mymap.get("varten")).get("zinssatz");
Or, if you're not sure if varten is really a HashMap, you can try the following to get the type:
System.out.println(mymap.get("varten").getClass());
Update
If varten is a HashMap stored inside an ArrayList, try the following:
((HashMap)((ArrayList)mymap.get("varten")).get(0)).get("zinssatz");
...which is the same as:
List list = (ArrayList) mymap.get("varten");
Map map = (HashMap) list.get(0);
Object zinssatz = map.get("zinssatz");

How to store values from Trident/Storm in a List (using the Java API)

I'm trying to create a few Unit Tests to verify that certain parts of my Trident topology are doing what they are supposed to.
I'd like to be able to retrieve all the values resulting after running the topology and put them in a List so I can "see" them and check conditions on them.
FeederBatchSpout feederSpout = new FeederBatchSpout("some_time_field", "foo_id");
TridentTopology topology = new TridentTopology();
topology.newStream("spout1", feederSpout)
.groupBy(new Fields("some_time_field", "foo_id"))
.aggregate(new Fields("foo_id"), new FooAggregator(),
new Fields("aggregated_foos"))
// Soo... how do I retrieve the "aggregated_foos" from here?
I am running the topology as a TrackedTopology (got the code from another S.O. question, thank you #brianghig for asking it and #Thomas Kielbus for the reply)
This is how I "launch" the topology and how I feed sample values into it:
TrackedTopology tracked = Testing.mkTrackedTopology(cluster, topology.build());
cluster.submitTopology("unit_tests", config, tracked.getTopology());
feederSpout.feed(new Values(MyUtils.makeSampleFoo(1));
feederSpout.feed(new Values(MyUtils.makeSampleFoo(2));
When I do this, I can see in the log messages that the topology is running correctly, and that the values are calculated properly, but I'd like to "fish" the results out into a List (or any structure, at this point) so I can actually put some Asserts in my tests.
I've been trying [a s**ton] of different approaches, but none of them work.
The latest idea was adding a bolt after the aggregation so it would "persist" my values into a list:
Below you'll see the class that tries to go through all the tuples emitted by the aggregate and would put them in a list that I had previously initialized:
class FieldFetcherStateUpdater extends BaseStateUpdater<FieldFetcherState> {
final List<AggregatedFoo> results;
public FieldFetcherStateUpdater(List<AggregatedFoo> results) {
this.results = results;
}
#Override
public void updateState(FieldFetcherState state, List<TridentTuple> tuples,
TridentCollector collector) {
for (TridentTuple tuple : tuples) {
results.add((AggregatedFoo) tuple.getValue(0));
}
}
}
So now the code would look like:
// ...
List<AggregatedFoo> results = new ArrayList();
topology.newStream("spout1", feederSpout)
.groupBy(new Fields("some_time_field", "foo_id"))
.aggregate(new Fields("foo_id"), new FooAggregator(),
new Fields("aggregated_foos"))
.partitionPersist(new FieldFetcherFactory(),
new Fields("aggregated_foos"),
new FieldFetcherStateUpdater(results));
LOGGER.info("Done. Checkpoint results={}", results);
But nothing... The logs show Done. Checkpoint results=[] (empty list)
Is there a way to get that? I imagine it must be doable, but I haven't been able to figure out a way...
Any hint or link to pages or anything of the like will be appreciated. Thank you in advance.
You need to use a static member variable result. If you have multiple parallel tasks running (ie, parallelism_hint > 1) you also need to synchronize the write access to result.
In your case, result will be empty, because Storm internally, creates a new instance of your bolt (including a new instance of ArrayList). Using a static variable ensures, that you get access to the correct object (as there will be only one over all instances of your bolt).

How to get voters lists from all linked issues

I am trying to create a JIRA plugin that does the following:
For each issue, takes all linked issues which are linked by "duplicates" or "is duplicated by" (or other predefined link types).
For each such issue, get a list (not necessarily a List object) of the voters on that issue.
My problem is that the javadoc has little to no information. Following a tutorial, I currently have:
public class VotersCount extends AbstractJiraContextProvider {
#Override
public Map<String, Integer> getContextMap(User user, JiraHelper jiraHelper) {
Map<String, Integer> contextMap = new HashMap<>();
Issue currentIssue = (Issue) jiraHelper.getContextParams().get("issue");
// Issue[] linkedIssues = currentIssue.getLinkedIssuesBy(...); //Step 1 mock code
// Voter[] voters = linkedissues[3].getVoters(); //Step 2 mock code
int count = voters.length; //Pretend there is some calculation here
contextMap.put("votersCount", count);
return contextMap;
}
}
(and I use votersCount in the .vm file.)
However, I see no explanation in the javadocs for AbstractJiraContextProvider and getContextMap so I'm not even sure if it's the right approach.
In my own research I found the class ViewVoters which has the method Collection<UserBean> getVoters(), which is something I can work with, but I don't know how to obtain or construct such an object in a way which will interact with a given issue.
I am looking for a working code to replace my 2 lines of mock code.
1) Use one of the methods from IssueLinkService. Maybe getIssueLinks
2) issueVoterAccessor.getVoterUserkeys
Instances of IssueLinkService and IssueVoterAccessor should be injected as parameters to constructor of your VotersCount.
I solved it by using the following:
To get issues linked to Issue issue by specified link types:
LinkCollection linkCollection = ComponentAccessor.getIssueLinkManager().getLinkCollectionOverrideSecurity(issue);
Set<IssueLinkType> linkTypes = linkCollection.getLinkTypes();
// Perform operations on the set to get the issues you want.
for (IssueLinkType linkType : linkTypes) {
List<Issue> l1 = linkCollection.getOutwardIssues(linkType.getName());
List<Issue> l2 = linkCollection.getInwardIssues(linkType.getName());
}
To get all the voters on Issue issue:
ComponentAccessor.getVoteManager().getVoterUserkeys(issue);
I was later shown that one can extend CalculatedCFType and override getValueFromIssue which hands you the current issue as a parameter instead of the using
Issue currentIssue = (Issue) jiraHelper.getContextParams().get("issue");

java.lang.NullPointerException in mapreduce job

I have created a class as detect harm which I tested to work in normal environment. The class is such that you have to provide problem statement & device type and the result is a code. This code logic is defined in a function with private variable. This variable is initialized everytime the object is created an a file containing values is passed.
I have used the same idea in mapreduce program where in for every input split an object would be created of the class. The rule file would be passed and the value would be returned. I see that the intermediate functions are returning perfect values only problem is the mapping of the function which returns the hashmap code.
public HashMap<String,String> fdpCodes(LinkedList<String> str)
{
HashMap<String,String> displayCodes = new HashMap<String,String>();
for(String word:str)
{
if(mapHarms.containsKey(word))
displayCodes.put(mapHarms.get(word),word);
}
if(displayCodes.isEmpty()) displayCodes.put("C50675", "No Harm");
return displayCodes;
}
I have to declare the same logic in hadoop to make it work. Any suggession is appreciated. Thanks.

how to read a list of objects from the configuration file in play framework

How can i read a list of users from the configuration file in play framework?
i have tried doing something like this:
users=[{uid:123,pwd:xyz},{uid:321,pwd:abc}]
from the play application
List<Object> uids = Play.application().configuration().getList("users");
will give me this a list of objects, if I iterate through the list i get each object as
{uid=123,pwd=xyz} and {uid=321,pwd=abc}
at this point i don't know how i can elegantly get the value of the uid, i can do some hacky job as omit the first and last bracket and parse for the before after equal sign, but it would be too ugly! any idea? (the application is written in java)
thanks
A Scala implementation that avoids the deprecated getConfigList method would rely on retrieving a Seq[Configuration] as follows:
case class UserConfig(uid: Int, pwd: String)
val userConfigs: Seq[UserConfig] = configuration.get[Seq[Configuration]]("users").map { userConfig =>
UserConfig(userConfig.get[Int]("uid"), userConfig.get[String]("pwd"))
}
Since I had recently the same problem and this is still unanswered,
here is my suggestion:
List<User> users = getConfig().getConfigList("users").stream().map(
config -> new User(config.getString("uid"), config.getBoolean("pwd"))
).collect(Collectors.toList());
As far as I know there are no tuples or anything in Java, you need to use either an object or a list with two elements. I decided to go for an object here, you can also return a list.
A list of uid's sounds to me like:
# List of UID's
users=[123,456,789] // every number represents a UID
Then you can get this list as:
List<Object> uids = Play.application().configuration().getList("users");
And then do what you want with this:
for (Iterator<Object> iterator = uids.iterator(); iterator.hasNext();) {
Object object = (Object) iterator.next();
System.out.println(object);
}
Is this what you are looking for?
BTW, you can read more about Play Framework configuration options: http://www.playframework.com/documentation/2.1.0/Configuration

Categories