My issue with arraylists is simple; I have a class called Score, it consists of an integer called myScore and a Name class (not relevant) called myPlayerName. I have an arraylist of these Score(s) called scoreList, and i want to be able to print out the ten highest scores in that list (a top ten list if you will), but when i call a collections.sort() method on the arraylist, it seems to return an error. I think this may be because of the fact that my score class has two parameters. how can I order an arrayList based on a specific part of an element. Is it possible to sort them by the score integer?
Related
Suppose I have two fluxes Flux<Class1> and Flux<Class2> and both Class1 and Class2 have a common attribute, say "id".
The use case is to join the two fluxes based on the common attribute "id" and construct a single Flux<Tuple<Class1, Class2>>, similar to joining two sql tables.
-There will always be a 1 to 1 match, for the attribute id, between the two fluxes.
-The fluxes won't contain more than 100 objects.
-The fluxes are not ordered by id.
How do I achieve this in Project Reactor/Spring web flux?
Assuming that:
both collections aren't very big (you can hold them in memory without risking OOM issues)
they're not sorted by id
each element in a collection has its counterpart in the other
First, you should make those Class1, Class2 implement Comparable or at least prepare a comparator implementation that you can use to sort them by their id.
Then you can use the zip operator for that:
Flux<Class1> flux1 = ...
Flux<Class2> flux2 = ...
Flux<Tuple2<Class1,Class2>> zipped = Flux.zip(flux1.sort(comparator1), flux2.sort(comparator2));
Tuple2 is a Reactor core class that lets you access each element of the Tuple like this
Tuple2<Class1,Class2> tuple = ...
Class1 klass1 = tuple.getT1();
Class2 klass2 = tuple.getT2();
In this case, sort will buffer all elements and this might cause memory/latency issues if the collections are large. Depending on how the ordering is done in those collections (let's say the ordering is not guaranteed, but those were batch inserted), you could also buffer some of them (using window) and do the sorting on each window (with sort).
Of course, ideally, being able to fetch both already sorted would avoid buffering data and would improve backpressure support in your application.
I think this should work with the following constraints:
the 2nd Flux needs to emit the same elements to all subscribers since it gets subscribed to over and over again.
this is basically the equivalent of a nested loop join so highly inefficient for large fluxes.
every element of the first Flux has a matching element in the second one.
flux1.flatMap(
f1 -> flux2.filter(f2 -> f2.id.equals(f1.id)).take(1)) // take the first with matching id
.map(f2 -> Tuple.of(f1,f2))) // convert to tuple.
writen without IDE. Consider pseudo code.
In my code I working with different types of collections and often converting one to another. I do it easily calling toList, toVector, toSet, toArray functions.
Now I am interested in performance of this operations. I find information about length, head, tail, apply performance in documentation. What actually happens when I call functions(toList, toVector, toSet, toArray) on List, Set, Array and Vector implementation in scala?
P.S. Question is only about standard scala collections which is immutable.
Well my advice would be: look yourself into the source code ! For instance, method toSet is defined as follow in the TraversableOnce trait (annotated by myself) :
def to[Col[_]](implicit cbf: CanBuildFrom[Nothing, A, Col[A #uV]]): Col[A #uV] = {
val b = cbf() //generic way to build the collection, if it would be a List, it would create an empty List
b ++= seq // add all the elements
b.result() //transform the result to the target collection
}
So it means that the toSet method has a performance of O(N) since you traverse all the list once! I believe that all the collections inheriting this trait are using this implementation.
let's say i want to create a movie database software.
i need a class Actor that will have attribute as name, surname, dob, awards, and it should also have an array of "movie" object.
i also need the class Movie, that will have attribute as title, duration, year, but it should also have an array of "actor" object. to represent the cast of the movie.
Actor object need a list of all the movie done by that actor, just as the movie object need a list of all the actors that played in it.
that gets quite messy cause it's a class containing another one and viceversa, the compiler get stuck.
which would be the right way to represent this situation?
You can use references, Actor* in class Movieand Movie* in class Actor. And forward declarations of the classes:
class Actor;
public class Movie { ... Actor* ... }
This in the header.
In the class implementation, .cpp, you then might include the header of the other class, and use methods of the other class.
In most languages the concept is the same. As you've observed, you can't have "lists of Movie objects" embedded in Actors and vice versa, so instead keep a list of each and just "reference" the other objects in some way. This might be using some unique reference value (e.g. an incrementing number) that can be used to search the other list or associative container (e.g. use a key/value "std::map" in C++ rather than a linked list), or by keeping a reference or (weak) pointer directly to the logically linked objects....
I'm using SortedDictionaries to simulate a Queue (due to some requirements that I have), and I'm calling Last() over the sorted dictionary to get the item that I need to dequeue.
I was just wondering about the performance of using a custom comparer and calling First() or keep calling Last().
After decompiling the .NET 3.5 assemblies, I found that the SortedDictionary class does have a Count property, so I'm guessing the framework just returns the item at position 0 when First is called, and the item at position [count-1] when Last is called, am I right?
No.
Since SortedDictionary does not implement IList<TValue> (which has a this[int] indexer), Last() has no choice but to iterate through the whole thing.
The Last method is an extension method in the Enumerable class. The implementation of Last first tries to cast the IEnumerable (your SortedDictionary) to IList<T>. If it can, then it uses the Count property to directly access the last element. If it can't, then it has to iterate over all elements to get to the last one. SortedDictionary doesn't implement IList<T>, so Last will iterate over all elements.
We have an invoice model that bills clients in a few different ways. For the sake of brevity, I'm going to focus on two: cost per impression and cost per phone inquiry. My thought was to implement these (and the rest) as strategies and then dynamically mix them into the invoice class.
This seems appropriate because there are different sources of information used to determine the number of impressions/calls. This could be encapsulated in the strategy, while keeping the basic formula in the Invoice class.
The calculation for cost per impression is simple: num impressions X cost per impression.
The calculation for phone inquiries is a little more complicated: num calls X cost per call.
class Invoice
def self.strategy
self.class_eval <<-EOS
include #{billing_type}
EOS
end
def invoice_amount
# this will used the module mixed in above
self.rate * calculate_impressions
end
end
Then, the modules could be:
module PerImpressionCalculation
def calculate_impressions
# get the number of impessions from source a...
end
end
module PerInquiryCalcuation
def calculate_impressions
# get the number of impessions from source b...
end
end
However, whether a call counts or not is based on the length of the call and this varies from model to model. Thus, when I'm searching through the phone logs I need to have this value.
My question is where does this value get stored? I could create a strategy for invoices that are based on 10 second calls and a separate one for 30 second ones, but that seems wasteful. If a deal came in that wants the threshold to be 15 seconds, I need to write a new strategy. What is the best design choice to solve this issue?
Don't implement your strategies as module mixins. Implement them as full fledged classes with a public PerInquiryCalculation method and inject the right one into the Invoice class using its constructor.
This way each strategy class can have its own state variables set during construction. The constructor of PerInquiryStrategy can take a duration threshold that the PerInquiryCalculation method uses to calculate the fees.
You can retrieve all mixed modules and base class using the class method ancestors. So if you have an instance myInvoice, you can simply use myInvoice.class.ancestors. It returns an array of constants so you can check for inclusion.
BTW, in this context, I think that traditional composition/aggregation is more appropriate in this case: it is safer when several different strategy coexists at the same time. You do not want ending in changing all invoices strategy because you affected the base class...