#include<iostream>
class A
{
public:
A(int n=2): m_i(n)
{
// std::cout<<"Base Constructed with m_i "<<m_i<<std::endl;
}
~A()
{
// std::cout<<"Base Destructed with m_i"<<m_i<<std::endl;
std::cout<<m_i;
}
protected:
int m_i;
};
class B: public A
{
public:
B(int n ): m_a1(m_i + 1), m_a2(n)
{
//std::cout<<"Derived Constructed with m_i "<<m_i<<std::endl;
}
~B()
{
// std::cout<<"Derived Destructed with m_i"<<m_i<<std::endl;
std::cout<<m_i;//2
--m_i;
}
private:
A m_a1;//3
A m_a2;//5
};
int main()
{
{ B b(5);}
std::cout <<std::endl;
return 0;
}
Ответ в этом случае равен 2531. Как здесь вызывается конструктор:
Вызывается тот же самый способ деструктора:
В этом примере построение m_A1 & amp; m_A2 не имеет отношения к порядку порядка списка инициализации, но их порядок объявления.
Это можно сделать в несколько этапов. Пусть для простоты все значения равны String
. Также предполагается, что у вас есть конструкторы и методы equals
/ hashcode
.
Map<IdName, Map<Another, List<String[]>>> map = Arrays.stream(dbResult)
.collect(
groupingBy(s -> new IdName(s[0], s[1], null),
groupingBy(s -> new Another(s[2], s[3], null))));
Тогда мы можем создать Format
объектов и собрать все вместе.
for (Map.Entry<IdName, Map<Another, List<String[]>>> entry : map.entrySet()) {
IdName idName = entry.getKey(); // main object
Set<Another> anothers = entry.getValue().keySet();
for (Another another : anothers) { // create list<Format> for each Another
List<Format> formatList = entry.getValue().get(another).stream()
.map(format -> new Format(format[4], format[5], format[6]))
.collect(Collectors.toList());
another.setFormatList(formatList);
}
idName.setAnotherNameList(anothers);
}
Теперь мы можем получить все собранные объекты
Set<IdName> idNames = map.keySet();
Похоже, вы должны использовать Collectors.collectingAndThen
.
Сначала создайте экстракторы (при условии, что у ваших классов есть конструкторы и геттеры):
// The cast types are just an example. You can Cast/convert the array values to any type you want
IdName extractIdName(Object[] row) {
return new IdName((String) row[0], (String) row[1], null);
}
Another extractAnother(Object[] row) {
return new Another((String) row[2], (String) row[3], null);
}
Format extractFormat(Object[] row) {
return new Format((String) row[4], (boolean) row[5], (boolean) row[6]);
}
Затем вам понадобятся функции слияния:
List<Another> setFormats(Map<Another, List<Format>> map) {
return map.entrySet()
.stream()
.map(e -> {
e.getKey().setFormatList(e.getValue());
return e.getKey();
})
.collect(toList());
}
List<IdName> setAnothers(Map<IdName, List<Another>> map) {
return map.entrySet()
.stream()
.map(entry -> {
entry.getKey().setAnotherNameList(entry.getValue());
return entry.getKey();
})
.collect(toList());
}
Наконец, это поможет. :
// Converting Object[][] to List<IdName>
List<IdName> list =
Arrays.stream(dbResult)
.collect(
collectingAndThen(
groupingBy(this::extractIdName,
collectingAndThen(
groupingBy(this::extractAnother,
mapping(this::extractFormat, toList())),
this::setFormats
)),
this::setAnothers));