I have a List<StructureA>.
Now this structure comprises of a Type: typeA & typeB.
Type here is an Enum.
StructureA comprises of fields : countryCode, Type, timeZone.
StructureB comprises of fields: countryCode, timeZone
I want to convert this List into a Map<Type, Set<StructureB>>. Is this possible using streams?
use grouping by collector
Map<Type, Set<StructureB>> collect = list.stream()
.collect(Collectors.groupingBy(StructureA::getType,
Collectors.mapping(a -> new StructureB(a), Collectors.toSet())));
Related
In Haskell I've defined a datatype "Person". Now I want to define a List which can store any number of Persons. The goal is to be able to add an arbitrary amount of people to yet another datatype called "Class".
These are my attempts:
data Date = DMY
Int -- day
Int -- month
Integer -- year
deriving Show
data Person = Person
String -- first name
String -- last name
Date -- birthday
deriving Show
data Room =
Room
String -- name of room
Int -- capacity of students
Bool -- computer access for students
deriving Show
data PersonList =
Empty | Person PersonList
deriving Show
data Class =
Lecture String Room Teacher PersonList Date
|Lab String Room Teacher PersonList Int
deriving Show
data Teacher = Teacher String deriving Show
The usual way of doing this is to use the built-in list type instead of defining a separate PersonList type. So, Class would be defined using the type [Person] in place of the type PersonList, like so:
data Class
= Lecture String Room Teacher [Person] Date
| Lab String Room Teacher [Person] Int
deriving Show
If you really want to define a dedicated PersonList type from scratch, it would be something like:
data PersonList =
Empty | Node Person PersonList
That is, a "person list" is either Empty or a Node consisting of the first "person" plus the rest of the "person list". This very similar to the way the built-in list type is defined, except that the built-in type works with any element type instead of only Person elements, and there are lots of built-in functions for working with the built-in list type that you'd have to rewrite yourself if you tried to use this PersonList type.
Just use an existing list type, like [Person] or Vector Person. There's no need to reinvent the wheel here. If you want to call it a PersonList, use a type alias:
type PersonList = [Person]
As an aside, you really should be using record syntax for complex datatypes, especially if you're going to annotate the members anyways. Your current implementation of Date, for example, requires somebody to look at the comments in the definition every time they need to understand what's happening. You can formalize those annotations in record syntax:
data Date = DMY
{ day :: Int
, month :: Int
, year :: Int -- it's unlikely you need years beyond 9223372036854775807 C.E.
}
I have a table Documents and it has a field named type and the type has two data's namely - type1 & type2
Now my requirements is - i have a bootstrap tabs type1 & type2, and i need to display data accordingly on the template
Which is efficient way to do this ?
Using two variables
data = Documents.objects.filter(id=pk)
type1 = data.filter(type="type1")
type2 = data.filter(type="type2")
and then pass it to context
context = { "type1":type1,"type2":type2 }
Is there any other best way to do this ?
Seems like they really have to be different querysets since they are different sets of data to be rendered. Also assuming id is your primary key (which looks like it is), note that the usage of Documents.objects.filter(id=pk).filter(type=...) would just consider that 1 object with that pk, you might want to change it to Documents.objects.filter(type=...). Then you can make it a bit more dynamic by listing the possible types and then passing the filtered data to the template where the key is the type.
doc_types = ("type1", "type2")
context = {
doc_type: Documents.objects.filter(type=doc_type)
for doc_type in doc_types
}
I am trying to serialize this data
{ dates : [2020-06-11, 2020-06-12, 2020-06-13 ...], sources : ['Facebook', 'Twitter'...], count: [{'date': 2020-06-11, source: 'Facebook', 'count': 4}, ....]
How am I able to serialize the dates, and sources? It comes in a list and is not a list of objects, but merely a list of values. I have only serialized lists of objects before using the many=True keyword, but have never tried serializing a list of values. Do let me know how I am supposed to approach this, thanks all!
To serialize a list of values you have to use serializers.ListField with child of your value type.
class MySerializer(serializers.Serializer):
sources = serializer.ListField(child=serializrs.CharField())
...
I have a database schema where attribute are unlimited, I can have this structure using two ways.
Using Entity attribute-value model
table 1
id
entity
table 2
entityid
attribute-name
attribute-value
2 . Way is to use JSON.
like
table1
id
entity
json-attribute {"name":"value-pair"}
I have a question which way will be best and effective .
I am not familiar with a DBMS that would let you efficiently find all entities where someAttribute = x, if the entities were stored in a non-deconstructed canonical JSON representation. (But I would be eager to know about any.)
The first approach using two tables (at least) can accomplish this task, and it is therefore the more capable and the more flexible approach; a JSON representation of the entity always be constructed from the database recordset:
// all entities having a particular attribute
select entityid, attributeName, attributeValue
from ENTITIES INNER JOIN ENTITYATTRIBUTES
on ENTITY.ID = ENTITYATTRIBUTES.entityid
where ENTITIES.id IN
(
select distinct entityid from ENTITYATTRIBUTES
where attributename = ? and attributeValue = ?
)
OR
// the attributes for a specified entity
select attributeName, attributeValue
from ENTITIES INNER JOIN ENTITYATTRIBUTES
on ENTITY.ID = ENTITYATTRIBUTES.entityid
where ENTITIES.id = ?
Complexity could enter, of course, if attributes could themselves contain entities. Nesting of objects is possible in the JSON representation but in the database it requires either a multi-table relational mapping or an OODBMS that supports nested tables.
I had chosen json solution.
why ?
It will avoid writing complex query to fetch data.
What about if I need to load any particular attribute ?
yes. In JSON solution I have to load all attribute from the database. and then filter for that particular attribute.
But in my case I will be loading all attribute every time.
If I have a condition of loading particular attribute I might have chosen attribute value schema.
I and trying to use djangoappengine, but I am unsure how can I write model to incorporate standard
ListProperty(db.Key)
I know that djangotoolbox provides this field types but I am unable to figure out the exact syntax.
db.ListProperty(db.Key) stores a list of any entity's keys.
models:
class Profile(db.Model):
data_list=db.ListProperty(db.Key)
class Data(db.Model):
name=db.StringProperty()
views:
prof=Profile()
data=Data.all()
for data in data:
prof.data_list.append(data)
/// Here data_list stores the keys of Data entity
Data.get(prof.data_list) will get all the Data entities whose key are in the data_list attribute