I am getting list of information along with the user details from an API. Based on a particular parameter i need to get last modified user who has acted upon it by comparing date.
For ex:
parameter Last updated user Date
param1 John 26-Sep-16
param1 Jane 12-Nov-17
Param1 James 12-April-18
The size of the data increases for the same param1 in the list. So how to accomplish this.
Thanks in advance
I am not sure I understand your question properly, but if what you need is to get the object with the latest modified date from the list of user information entries, then (if you use Java 8+) you may try using Java stream for that as following:
list.stream().max(Comparator.comparing(UserInformation::getLastModifiedDate));
I am assuming here you have a list of UserInfromation objects called list , and UserInfromation has a getter method with the signature Date getLastModifiedDate()
Related
Like firebase generating uid. Can firebase generate an unique ID as we wish? If it is possible? how to write a code for it in the following order?
For example PRO00013, PRO00014, PRO00015....
I am asking here, because I'm working in a project for online shopping. when a user adds a product to their inventory it needs to assign an id for every product. that is must be in the human readable format. If it is not possible, just tell me no, I can accept that answer.
You can just call .push() to get randomUUID and then ref.child(randomUUID).setValue(object)
where the object can contain field ID such as PRO00013.
If this doesn't fit your needs you can just place ref.child("PRO00013").setValue(object) and not include field id in this object.
Now you want to get id PRO00014 for the next pushed object. This ain't gonna happen with firebase.
But you can get the last ID added in this ref with query on
ref.orderByKey().limitToLast(1);
Now you have the last added object from this node and you can simply get the key which will be PRO00013.
1. Cast it to String
2. Remove "PRO" from this string
3. Cast the remaining to integer
4. Add 1 to this integer
5. Create new string variable and give it value "PRO" + the new integer
6. Now push the object in ref.(the new string variable).setValue(object).
Now I don't know if you gonna implement this logic in a cloud function accepting some sort of params but if this is the case - this should work just fine.
I also want to know WHY xD
I have a dataset, and I'm not sure which type of List or Map to use in Java.
Following is a sample dataset.
There are 2 columns, Users and Errors.
A user can be repeated. Errors are random and there won't be any duplicates.
Users Errors
User A Error 1
User B Error 2
User A Error 3
User C Error 4
and I should be able to use the list to get all errors per user at the end.
I tried a hashmap, but it only retrieves the last entry for user.
Thanks in advance.
You can use a HashMap<User,List<Error> (not sure if there are User and Error classes or if they are represented by Strings or some numeric type, but you get the idea).
This will make it easy to obtain the List of all errors for a given user.
You can use Map of String to List.
Depending on java version you might need to do manual checks if given key exists (in such case add the error) or doesn't (create list and add a single error)
I have a map(String, LinkedList) mp. This corresponds to a table like this:
Name Date Dept
Tony 4/1/2014 55125
Bob 3/2/2013 54112
Jill 7/2/2014 55265
(I just made these up for this example. The first row (Name, Date, Dept) are the headings and correspond to the key value in the map (so you do an mp.get("Name") for example). The linked list that is returned for Name is , for Date is <4/1/2014, 3/2/2013, 7/2/2014> and Dept is <55125, 54112, 55265> etc.
I need to get the list for a value and then sort it. For Name and Dept I believe I can just do a Collections.sort(), but this will not work for date. If date was yyyymmdd it would work, but date is mm/dd/yyyy which is not guaranteed to sort correctly, and usually won't. I suppose I could process each entry and change it to yyyymmdd, but some of the fields will be blank (or maybe null but I think blank).
I can easily determine when to sort normally and when by date. Just not sure how to use Collections.sort() or something else to sort the date list. Can anyone help?
The Collections.Sort method allows you to (optionally) specify your own Comparator. In your case, when you sort the date column you will need to provide your own comparator.
If you are using Java 8 then the code will look something like this:
Collections.sort(dataMap.get("Date"), (date1, date2) -> compareDates(date1, date2));
The compareDates method would then need to be able to handle nulls and would need to pull apart the date components and return number of days from date1 to date2. You could easily use the java.util.Date object to do that - just convert the dates (possibly using DataFormat) and then compare.
I created one birt report.It contains one report parameter with name "FromDate".User will insert it in a fromat like "dd/MM/yyyy". I want to get year(if date is 11/01/2013.i want to extract 2013 from this varaible) from this input parameter and want to pass this value to one stored procedure(ie data set).How can i do that.Can anyone pls share sample code
You could define a variable 'selectedYear' like this:
var selectedValue = params["FromDate"].value;
var selectedValueArray = selectedValue.split("/");
selectedValueArray[2]
This code creates an array by "cutting up" the parameter value on the /'s, and then selecting the year (the second value in the array, which is the third block since we count from zero).
You can then use vars["selectedYear"] on your data set.
Alternatively, if your data set contains a list of years, you can create a data set
select distinct YEAR
from [dataSource]
and have your parameter select from that list instead. This will also guarantee that the selected year is in range.
You can map dataset parameters to report parameter-derived values in the Parameters section of the Edit Dataset dialog:
Select the stored procedure year input parameter from the list of dataset parameters and click Edit... .
Click on the fx button next to the Default Value and select Javascript Syntax .
Enter the formula BirtDateTime.year(params["FromDate"].value) .
Click on OK to confirm these changes in each dialog. (The Linked To Report Parameter option needs to be left as None for that dataset parameter, since you don't want to set the procedure input parameter value to be exactly the same as any of the Report Parameters.)
I had a similar problem in the past, however i looked at from a different angle in driving the date selection from the dates stored in the datasource as a dynamic parameter. Normally then a date from the dataset is in a format which can be formatted/extracted by functions to give the year month day etc.
i tried this one..in corresponding stored procedure i will do a substring on input paramaeter(ie FromDate)..it is working
In sql query(Data Set), Use this
"split_part(?,'/',3)" , We will have date parameter in place of '?' . As, you must be aware of that, Split_part will divide the string using the delimiter '/' .
What I need is something like Hashtable which I will fill with prices that were actual at desired days.
For example: I will put two prices: January 1st: 100USD, March 5th: 89USD.
If I search my hashtable for price: hashtable.get(February 14th) I need it to give me back actual price which was entered at Jan. 1st because this is the last actual price. Normal hashtable implementation won't give me back anything, since there is nothing put on that dat.
I need to see if there is such implementation which can find quickly object based on range of dates.
Off the top of my head, there are a couple ways, but I would use a TreeMap<Date> (or Calendar, etc).
When you need to pull out a Date date, try the following:
Attempt to get(date)
If the result is null, then the result is in headMap(date).lastKey()
One of those will work. Of course, check the size of headMap(date) first because lastKey() will throw an Exception if it is empty.
You could use a DatePrice object that contains both and keep those in a list or array sorte by date, then use binary search (available in the Collections and Arrays classes) to find the nearest date.
This would be significantly more memory-effective than using TreeMap, and it doesn't look like you'll want to insert or remove data randomly (which would lead to bad performance with a array).
Create a Tree Map with Date,String. If some one calls for a date then convert the string to date and call map.get(date), if you find then take the previous key than the current element.
You have all your tools already at hand. Consider a TreeMap. Then you can create a headmap, that contains only the portion of the map that is strictly lower that a given value. Implementation example:
TreeMap<Date,Double> values = new TreeMap<Date,Double>();
...fill in stuff...
Date searchDate = ...anydate...
// Needed due to the strictly less contraint:
Date mapContraintDate = new Date(searchDate.getTime()+1);
Double searchedValue = values.get(values.headMap(mapContraintData).lastKey);
This is efficient, because the headMap is not create by copying the original map, but returns only a view.