Grouping list of objects based on variable attributes of object in java - guava

I need to do group by on list of objects based multiple attributes in the object.
like lets say there is a student object
with attributes name,number,state,city,marks
now i need to do group by on it based on multiple keys,
like
it cab be state,city or state or name,state,city or marks,count(*)
Please let me know the best option to do it.
the possible options i have are.
1) using of Nested maps
2) using multimap with composite key
3) using ArrayTable.

Seems like just do a list per criteria. Otherwise I might create an Enum that implements Predicate and create a Multimap with the enum as the key.

Related

Index on Realm Object is compound index of properties or distinct indexes for each property

I use 'RealmSwift' to create a Realm Database in my app. Consider a subclass of 'Object' that I use to be 'ClassA'. The 'ClassA' and its properties can be simply described as below(avoiding actual code for brevity):
ClassA: Object
-prop1
-prop2
-prop3
-prop4
-prop5
-prop6
I want to have to two indexes on the 'ClassA'. To be specific I want each index to be a compound index. Lets call the two Indexes as 'Index1' & 'Index2'. And by compound I mean an Index can have multiple properties. So lets define the two Indexes as below:
Index1: prop1, prop2, prop6
Index2: prop3, prop4, prop5
I need these compound indexes, in order to have faster fetches, in case I have the values for those particular properties. This is of critical importance for my application.
Providing indexes on Real Objects using 'RealmSwift' is done by overriding Object.indexedProperties() by returning an array of String containing the names of properties we want to index.
I want to know if this will create a separate index for each property or a single compound index for all the properties.
AS I understand the documentation Object.indexedProperties() will create an index for each property that you return, but no compound index.
What you can do to get around this is to create new properties on your objects which combine the properties you want to use for your compound index.
Example:
Create a new property indexProp126 by concatenating prop1, prop2 and prop6 to a String (just as an example, there are probably more efficient ways like hashes etc. depending on what the type and content of those properties really is) and use this as the index then.

JPA Criteria API checking if entity collection contains certain elements or one of them

I am wondering how to create predicate which will filter entities which collection property contains elements from collection given as parameter using JPA specification.
In this example I am building query for UserEntity to return users which belong to groups contained in parameter List.
List<String> groups = query.getGroups();
predicates.add(cb.isTrue(r.get("groups").in(groups)));
But if I understand it correctly it will return true if all or less groups are in this list.
I am looking for solution where a) all elements from list are in entity collection b) at least one (or like way)
Is there some easy way to achieve this? In fact I don't really need anything from GroupEntity so I don't think joining is necessary
Thanks for help

Is there a way of allowing an end-user to modify enumeration values once application is released?

I am just starting out with CodeFluent and beginning to really like it. My question is: I set a property of an entity to enumeration. How can I allow an end-user to add extra values (that are stored afterwards as additional choices) to an enumeration? Or should I use another entity to store those values/choices instead?
For instance: let's say I have a product and a producttype. My producttype is an enumeration (frozen, fresh, seasonal), and down the road, the user wants additional types (i.e.: organic, stationary). Should those be enumeration values or a separate entity?
If a separate entity.....I'm not really sure how I define the relationship (1 to 1, 1 to many - i.e. 1 producttype can have many products)?
You can't add values to an enum at runtime, that's impossible in .NET, so it's also impossible with CodeFluent.
So, you want to create another entity that will store the list of enums. That would be a 1:M relation. This is how you would layout that relation:
Each enumeration value would be a row in the ProductType table. With CodeFluent, you can declare "instances" for an entity that will become rows in the final table, so here, you can declare your initial enum values using instance, so use the instance grid on the ProductType entity, and add instances:
Note in this case, maybe you want to create the ProductType's Id property as an int without identity (if you don't want those enum int values generated by the database).

Access the property used in mapping entity to a table in EFv4

When we have two entities in EFv4 EDM diagram and only one table for both in the database (for instance, having table Documents and entities Invoice and Qoute), table Documents having documentTypeId column as a discriminator and set this column as a discriminator in the EDM (in Table mappings), how do we read the value of this property in our code?
We cannot assign values to it because EF does it for us under the hood (based on what we entered in Table mappings for condition) but somehow I don't get it why we are also not allowed to read it.
Imo this property is already mapped so you can't map it again. It is used to determine type of materialized entity. Why do you need such column. Usually it is enough to use is operator like:
var document = context.Documents.GetById(id);
if (document is Invoice)
{
...
}
If you only need to select subtypes you can use OfType extension method like:
var invoices = context.Documents.OfType<Invoice>().ToList();
You also don't need to set this value when adding new entity because you are adding subtype - Invoice or Quote.
Edit:
As I understand from your comment you don't need this information in query. In such case you don't need to map it. Simply use partial class of your entity and add custom property which will return your string. Sound like stupid solution but actually it would be the easiest one.
Discriminator column should be part of mapping metadata so in case of T4 template generating your entities, it could be possible to update the template so it generate such property for you.
You may want to use a single-table inheritance hierarchy, as described here.
That way, you could have an abstract Document class that includes a DocumentTypeId column. Invoices and Quotes would extend this class, but specify certain DocumentTypeId filters. However, because the original class has a DocumentTypeId column, they would each have that column as well.
Another advantage to this approach is that you could create utility methods that can act on any Document, and you could pass any Invoice or Quote to these methods.

Sort order in Core Data with a multi-multi relationship

Say I'm modeling a school, so I have 2 Entities: Student and Class. For whatever reason, I want each class roster to have a custom sort order. In a simple relationship, this would mean giving Student a sortOrder attribute and just sorting the list by this number. Issue is, a Student might be order 3 in one Class and order 6 in another. How would I store these orderings in Core Data in a way that I can easily access them and sort my lists properly?
Student Class
classes <<--------->> students
^ ^
| |
unordered ordered
This diagram might help explain what I'm trying to do. The students "roster" I would want to be fetched in a specific order stored somewhere, which could be any ordering. Storing this ordering is what I'm not sure how to do in a way that's the most efficient. Creating a bunch of Order objects and trying to manage the links sounds like a lot of overhead, and it feels like there must be a better way.
If the ordering of students can be described by one or more NSSortDescriptors, you could create a fetched property on the Class entity that fetches the students and applies the sort descriptor. Alternatively, it may be easier (depending on your use case) to apply the sort descriptor(s) to the NSFetchedResultsController that you're using to deal with the class' students collection.
If you can't use an NSSortDescriptor, then you'll need an index attribute (or name of your choice) on the Student entity if there's only one ordering or a collection of Order entities describing the index in each ordering for each Student. You'll be responsible for maintaing these index values. Unfortunately, there's no easy way to do this in Core Data. It's just a lot of work.
Student <<->> StudentClass <<->> Class
StudentClass
----
studentID
order
classID
Then you can select as necessary.
For example, you have a student. Fetch all StudentClass where StudentID is student.studentID. You then have the order, as well as access to the Class.
You'll likely want to add some business logic to make your life easier. Also, if you're not already using it, take a peek at MOGenerator: https://github.com/rentzsch/mogenerator
EDIT: I'd really like to know why this is getting voted down. Comments would be much appreciated.