I am getting a response in this list retailers object.
List<Retailers> retailers;
Say for eg: retailers.getId() returns 1,2,3,4,5,6,7,8 in response and this works fine.
Now I need to store this in another List and pass it in params. But only first value 1 is getting stored in all case.
Below is my code for this
List<RoutePlanDayRequests> routePlanDayRequests = new ArrayList<RoutePlanDayRequests>();
RoutePlanDayRequests requests = new RoutePlanDayRequests();
for(int i= 0; i<retailers.size();i++){ //retailers is already having datas.
Log.e("retailers",retailers.get(i).getId()); //here i am getting 1,2,3,4,5
requests.setPartnerId(retailers.get(i).getId());
routePlanDayRequests.add(requests); //but while storing i get only 1.
updateServiceList(AppUtils.getMsisdn(mSharedPreferences), routePlanDayRequests);
}
Output Response:
[{"Id":"1"},{"Id":"1"},{"Id:"1"},{"Id":"1},{"Id":"1"},{"Id":"1"},{"Id":"1"}]
Instead it should get all elements (1,2,3,4,5 from retailers.getId) and pass it in params.
Anybody has any idea why this is happening?
You must create RoutePlanDayRequests in the loop and send all collected data at the end (outside of the loop) to the updateService
List<RoutePlanDayRequests> routePlanDayRequests = new ArrayList<RoutePlanDayRequests>();
// you can use the simpler syntax of the for-loop
for(Retailer retailer : retailers) {
// create the requests object in the loop
RoutePlanDayRequests requests = new RoutePlanDayRequests();
Log.e("retailers",retailer.getId());
requests.setPartnerId(retailers.get(i).getId());
routePlanDayRequests.add(requests);
}
// call the service outside of the loop
updateServiceList(AppUtils.getMsisdn(mSharedPreferences), routePlanDayRequests);
}
Related
I'm facing a weird behavior in my Java code using List.
The code is very simple, I have a List of Object called AccessRequest which comes from a database and I'm using this first List to create a new one but with a filter to select only a few objects.
Here is the code :
private void updateCommentIfNeeded() {
List<AccessRequest> accessRequestList = getAllRequest();
List<AccessRequest> commentsList = getCommentsListProcessedManually(accessRequestList);
}
public List<AccessRequest> getCommentsListProcessedManually(List<AccessRequest> accessRequests) {
accessRequests.removeIf(ar -> !ar.getComment().equals("To be processed manually"));
if (accessRequests.size() != 0) {
SQLServerConnection sqlServerConnection = new SQLServerConnection(sqlServerUrl);
accessRequests.removeIf(ar -> !sqlServerConnection.emailExists(ar.getEmail()));
}
return accessRequests;
}
I'm supposed to get a second List only containing the objects that has their comments to To be processed manually, which I do. But the weird part is that the first List also takes the value of the second as if I wrote accessRequestList = commentsList but there is no such thing and I'm using local variable.
Ex :
I have 3 objects in my first List, but only one containing the required comment
Both list ends with containing the only objects containing the comment
I'm kind of lost here if anyone has an idea !
Your method getCommentsListProcessedManually modifies the list you're passing. I believe you're operating under the assumption that passing the list as a parameter somehow creates a copy of the list, whereas what is actually happening is that a reference to the list is passed by value.
There are several ways to solve this, but the easiest is to simply create a copy of your input list at the start of your method:
public List<AccessRequest> getCommentsListProcessedManually(List<AccessRequest> input) {
List<AccessRequest> accessRequests = new ArrayList<>(input);
accessRequests.removeIf(ar -> !ar.getComment().equals("To be processed manually"));
if (accessRequests.size() != 0) {
SQLServerConnection sqlServerConnection = new SQLServerConnection(sqlServerUrl);
accessRequests.removeIf(ar -> !sqlServerConnection.emailExists(ar.getEmail()));
}
return accessRequests;
}
You could also use the Stream API for this (using the filter operation), but that's quite a bit trickier in this situation.
You are passing a reference of the list to the method getCommentsListProcessedManually.
So accessRequestList and the one passed as a parameter are the same, hence any operation done to the list is done to the same list.
You can create a copy of the list before passing it as a parameter:
List<AccessRequest> newList = new ArrayList<AccessRequest>(accessRequestList);
Me new to spark , in our project we are using spark-structured streaming to write kafka consumer.
We have a use case where I need to modular the code so that multiple people can work on different pieces of spark-job simultaneously.
In first step we read different kafka topics now i have two datasets.
Lets say ds_input1 and ds_input2.
I need to pass these to next step where other person working on.
So i have done as below in java8
DriverClass{
Dataset<Row> ds_input1 = //populate it from kafka topic
Dataset<Row> ds_output1 = null;
SecondPersonClass.process(ds_input1 , ds_output1 );
//here outside I get ds_output1 as null
//Why it is not working as List<Objects> in java ?
//Is there anything wrong I am doing ? what is the correct way to do?
Dataset<Row> ds_output2 = null;
ThirdPersonClass.process(ds_output1 , ds_output2);
//here outside I get ds_output2 as null
//though ds_output2 populated inside function why it is still null outside?
}
SecondPersonClass{
static void process(ds_input1 , ds_output1 ){
//here have business logic to work on ds_input1 data.
//then i will update and assign it back to out put dataSets
//i.e. ds_output1
//for simplicity lets says as below
ds_output1 = ds_input1 ;
//here I see data in ds_output1 i.e ds_output1 is not null
}
}
ThirdPersonClass{
static void process2(ds_input2 , ds_output2 ){
//here have business logic to work on ds_input2 data.
// then i will update and assign it back to out put dataSets
//i.e. ds_output2
//for simplicity lets says as below
ds_output2 = ds_input2 ;
//here I see data in ds_output2 i.e ds_output2 is not null
}
}
Question :
Even though dataset is populated inside the function static method why those are not reflecting outside the function and still null?
Why java call by reference to objects not working here ?
How to handle this ?
Can we return multiple Datasets from a function if so how to do it ?
I'm trying to create a queue for outgoing bluetooth messages in Android. The UI can generate a bunch of messages to be sent out via bluetooth. Sometimes these are of the same message type, but with different data. If I have 10 of the same message type waiting to go out, I don't want to send all 10, I just want to send the last one. This is mainly to save bandwidth as BLE is fairly limited.
The queue contains message objects that have message_type and message_data Strings. The behaviour I'm looking for, but can't seem to figure out is: When adding a new object to the queue, it should check the existing items in the queue to see if any are of the same message_type. If so, the new object would overwrite that object in the queue (or delete the existing object and add the new one to the end of the queue. either would work) If an object with a matching message_type isn't found, then the new object would just be added to the end of the queue.
I haven't found anything that does this. The closest I have found is a LinkedHashSet, but this would just not add the new element instead of replacing the existing element with the new one. Maybe this behaviour can be modified?
The Bluetooth Message Obj:
public class BluetoothMessageObj {
private String message_type;
private String message_data;
}
EDIT
Here's what I ended up going with:
private LinkedList<BluetoothMessageObj> outgoingMessageQueue = new LinkedList<>();
public void addMessageToOutgoingMessageQueue(BluetoothMessageObj newObj){
for (int i = 0; i < outgoingMessageQueue.size(); i++) {
BluetoothMessageObj existingObj = outgoingMessageQueue.get(i);
if( existingObj.getMessage_type().equals( newObj.getMessage_type() ) ){
outgoingMessageQueue.remove(i); // remove the existing message
break;
}
}
outgoingMessageQueue.add(newObj); // add the new message
}
For that you can create your own logic as following:
private void replaceValue(ArrayList<BluetoothMessageObj> list, BluetoothMessageObj newBluetoothMessage) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getMessage_type().equalsIgnoreCase(newBluetoothMessage.getMessage_type())) {
list.set(i, newBluetoothMessage);
break;
}
}
}
Now everytime you add new element pass existing list of elements and new element you want to add and this would do it for you.
private void displayGroupsInRanking() {
for (int i = 0; i < 4; i++)
{
RankingANames.setItems(FXCollections.observableArrayList(groupModel.getListA().get(i).getName()));
System.out.println(RankingANames);
}
RankingBNames.setItems(FXCollections.observableArrayList(groupModel.getListB()));
RankingCNames.setItems(FXCollections.observableArrayList(groupModel.getListC()));
RankingDNames.setItems(FXCollections.observableArrayList(groupModel.getListD()));
}
I´m trying to, to get a specific attribute from an arraylist into a new arraylist. This works fine, but the listview only shows one object?
[The output1
the Code
I'll not rewrite code from your screen to show you the right way to do this but I can tell you what is wrong here.
On every iteration you are creating new collection with exactly one item and then you are using it as items list for table.
That's clearly wrong.
To solve it, you have to first prepare full list of items and then pass it to setItems method.
I have excel sheet with Test case data. Each test case might one contain single or multiple steps. I wanted to pull all data(3 fields) for each steps of test case into Arraylist "TestStepsList" by iterating through excel (by using for loop for each row). Once I check if new test case has started in excel I put all step data (Arraylist "TestSTepsList") into another array TestCaseList. So TestCaseList is a list of list.
TestCaseID Input Keyword
TC_01 Main Window1 Menuitem_Verification
TC_02 Configuration Manager-2_1 SubMenuitem_Verification-1
TC_02 Configuration Manager-2_2 SubMenuitem_Verification-2
Code is as follows:
for(int j=0;j<Total_Row;j++){
int total_Steps_TestCase =(int) test_Case_Data.get(curr_TCID);
List<String> TestStepsList = new ArrayList<>();
if (flag_CreateNewTC == 1) // Checking if we are on first step of the test case and need to create new list(TestSTepList)
{
TC_index = 1;
l = 0;
TestStepsList.add(0, curr_TCID);
TestStepsList.add(1, Curr_TCInput);
TestStepsList.add(2, Curr_TCKword);
if(TC_index ==((int) test_Case_Data.get(curr_TCID))) //Checking if test case is only single line. In such case we will add that testcase object(list to
//TestCase List)
{
seq=seq-1;
TestCaseList.add(seq,TestStepsList);
}
}
else //If a test case is multi step we will add all steps into TestSteplist till new test case begins
{
l=l+1;
TestStepsList.add(curr_TCID);
TestStepsList.add(Curr_TCInput);
TestStepsList.add(Curr_TCKword);
TC_index++;
if(TC_index ==((int) test_Case_Data.get(curr_TCID)))//If this is last step of existing test case then we need to add entry to TestCaseList List
{
seq=seq-1;
TestCaseList.add(seq, TestStepsList);
}
}
}
}
The problem I am facing is that for single line test case entry is properly getting added in TestCaselist, but for testccase with multiple steps only last row is getting added. For e.g if Testcase consist of 3 rows only last step is getting added
I think best practice is to initialize the list object everytime you add any item to the list but in my case its not possible as I wanted to add multiple steps in the same list.
I'm thinking the seq=seq-1 might have something to do with it. You keep pushing the TestStepsList into the same element in your TestCastList, if I'm reading your code correctly.
If you're adding to the end of the List, then leave out the index argument. Otherwise, change your index argument to seq-1 and stop reassigning seq.
TestCaseList.add(seq - 1, TestStepsList);
I'm not sure if I'm missing something though.
List<String> TestStepsList = new ArrayList<>();
keep this outside of your for loop.
because every time you are looping through it you are creating a new ArrayList.