I did the code below to have a dropdown box with all the currencies code.
List<String> currencies = Currency.getAvailableCurrencies().stream() .map(currency -> currency.getCurrencyCode()) .collect(Collectors.toList());
However, my output is unsorted.
Someone could give me a hand to sort the ArrayList(Currencies) on the code below?
Appreciate any help =)
You can sort by adding .sorted() into your stream. This will sort the list on it's natural order
List<String> currencies = Currency.getAvailableCurrencies().stream().map(currency -> currency.getCurrencyCode()).sorted().collect(Collectors.toList());
When you want something more advanced than natural ordering, or you want to order an object, you can simply pass a Comperator as argument to the sorted method.
For example if you want the list to be in reverse order
List<String> currencies = Currency.getAvailableCurrencies().stream().map(currency -> currency.getCurrencyCode()).sorted(Comparator.reverseOrder()).collect(Collectors.toList());
Related
I have an alias called student, the data structure is like this (result of command describe),
studentIDInt:int,courses:bag{(courseId:int,testID:int,score:int)}
Then I am trying to filter students by score, but met with such Pig parse error, if anyone have any good ideas, it will be great. Thanks.
Confused about the additional tuple reported in the error message.
student = filter student by courses.score > 3;
incompatible types in GreaterThan Operator left hand side:bag :tuple(score:int) right hand score:int
regards,
Lin
You can't do it directly. Possible solution is first flatten, filter and than group again
flat_student = foreach student generate studentIDInt, flatten(courses);
filtered_student = filter flat_student by score > 3;
final_student = group filtered_student by studentIDInt;
Another way is writing custom FilterFunc, so it's up to you what to choose.
I am using TreeMap to as I want to store sorted keys. I have also passed the comparator to sort the order. Now, I want to retrieve the 2nd key form the map. How do I go about doing it. The TreeMap is as given below :
private TreeMap<Coupon, LineItem> couponVsDiscountLine = new TreeMap<>((c1, c2) -> c1.weight().compareTo(c2.weight()));
Getting the sorted keys from TreeMap :
TreeSet<Coupon> coupons = (TreeSet<Coupon>) couponVsDiscountLine.keySet();
There is no method in TreeSet to get(index) as the elements in TreeSet are not indexed.
Other question, which Set does keySet() method of TreeMap return? How does TreeMap store the keys internally?
I read in some post that the TreeMap or TreeSet does not maintain the order if any modifications is done on that. Does it mean that retrieval of element may not give the elements in the order specified in the comparator?
I have a simple question about programming in Ruby. I'm a newbie to Ruby, so if somebody can help me, I will really appreciate it.
Assume a system lets users have buyer and seller feedback ratings. I want to add/merge the buy and sell feedback ratings for a user into one consolidated rating, so only the rating needs to be added from the two Relation objects. The user id is only used as the key, but is not added.
buy_rating = user_object.group(buy_feedback_rating).select('buy_feedback_rating, COUNT(id) as count')
sell_rating = user_object.group(sell_feedback_rating).select('sell_feedback_rating, COUNT(id) as count')
buy_rating and sell_rating are histograms of the user's buy/sell rating, with 1=Terrible, 2=Poor, 3=Average, 4=Good, 5=Very Good.
The following is a sample array with (key,value) pairs where key=rating from 1 to 5, and value=number of ratings
buy rating = [(1,2),(2,5),(3,1),(4,7),(5,6)]
sell rating = [(1,3),(2,2),(3,7),(4,4),(5,7)]
Desired output = [(1,5),(2,7),(3,8),(4,11),(5,13)]
(obtained by adding only the second values from each array, not the first values).
The buy_rating and sell_rating arrays will only have the the key->value pair if the value>0. Meaning, if a buyer has no buyer rating=1, then the pair (1,0) will not be present in the buy_rating array. This means the arrays could be as follows:
buy_rating = [[2,5],[3,1],[4,7]]
sell_rating = [[1,3],[2,2],[5,7]]
Question is, how do I achieve the desired result? I want to add only the second column, not the first, from each array. Object returned should be of the same data type as buy_rating and sell_rating, i.e. buy_rating and sell_rating are both ActiveRecord::Relation objects, and the result should also be an ActiveRecord::Relation object.
You can make a map of values, sum based on the first, index, and then convert back to an array
buy_rating = [[1,2],[2,5],[3,1],[4,7],[5,6]]
sell_rating = [[1,3],[2,2],[3,7],[4,4],[5,7]]
merged_ratings = buy_rating + sell_rating
composite_ratings = Hash.new(0)
merged_ratings.each do |rating|
composite_ratings[rating[0]]+=rating[1]
end
composite_ratings.to_a
Check this fiddle: http://rubyfiddle.com/riddles/2d0f9/2
Lets say I have set of values in file.txt
a,b,c
a,b,d
k,l,m
k,l,n
k,l,o
And my code is:
file = LOAD 'file.txt' using PigStorage(',');
events = foreach file generate session_id, user_id, code, type;
gr = group events by (session_id, user_id);
and I have set of value:
((a,b),{(a,b,c),(a,b,d)})
((k,l),{(k,l,m),(k,l,n),(k,l,o)})
And I'd like to have:
(a,b,(c,d))
(k,l,(m,n,o))
Have you got any idea how to do it?
Regards
Pawel
Note: you are inconsistent in your question. You say session_id, user_id, code, type in the FOREACH line, but your have a PigStorage not providing values. Also, that FOREACH has 4 values, while your sample data only has 3. I'll assume that type doesn't exist in order to answer your question.
After your gr relation, you are left with the group by key (in this case (session_id, user_id)) in a automatically generated tuple called group.
So, first step: gr2 = FOREACH gr GENERATE FLATTEN(group);
This will give you the tuples (a,b) and (k,l). You need to use FLATTEN because group is a tuple and you are asking for session_id and user_id to be individual columns. FLATTEN does that for you.
Ok, so now modify the gr2 line to also use a projection to tease out the third value:
gr2 = FOREACH gr GENERATE FLATTEN(group), events.code;
events.code creates a bag out of all the code values. events is the name of the bag of grouped tuples (it's named after the original relation).
This should give you:
(a, b, {c, d})
(k, l, {m, n, o})
It's very important to note that the values in the list are in a bag not a tuple, like you asked for. Keeping it in a bag is the right idea because the bag is a variable list, while a tuple is not.
Additional advice: Understanding how GROUP BY outputs data is something I see a lot of people struggle with when first using Pig. If you think my answer doesn't make much sense, I'd recommend spending some time to really get to understand GROUP BY. Understanding versus thinking it is magic will pay off in the long run.
I have a string list (List) that contains delimited fields. An example would be:
List[0] = "7/1/2013,ABC,123456"
List[1] = "7/2/2013,DEF,234567"
I also have a DataTable where a record either will or will not contain the the values from the 2nd and 3rd column in the String List:
Example
Row[0][0]="ABC" <-----String
Row[0][1]=123456 <-----Int32
What I want to do is find any records (via Linq) in the DataTable that DO NOT have corresponding values in the String List.
I've been googling for a while, and can't quite find the right way to do this with Linq...can anyone help?
This code snippet should give you an enumeration of the indices that do not have the appropriate DataTable values:
var correspondingRecords =
from index in Enumerable.Range(0, List.Count)
let items = List[index].Split(',')
where !(item[1] == Row[index][0] && item[2] == Row[index][1])
select index;
The basic idea is to iterate over the indices in order to make sure that you're comparing the appropriate rows and list items to one another. Once you do that, it's simple enough to parse the list item and make the appropriate comparisons.