so here's problem. have 3 classes below:
class objectclassa { private final long id; private final string name; private final boolean isreadonly; //<some more fields> long getid() { return this.id; } string getname() { return this.name; } boolean isreadonly() { return this.isreadonly; } } class objectclassb { private final long id; private final string location; private final boolean isreadonly; //<some more fields> long getid() { return this.id; } string getlocation() { return this.location; } boolean isreadonly() { return this.isreadonly; } }
and
class objectclassc { private final string location; private final boolean isreadonly; private final string location; string getname() { return this.name; } boolean isreadonly() { return this.isreadonly; } string getlocation() { return this.location; } }
i have 2 maps -
map<id,objectclassa> mapobjectclassa
and
map<id,objectclassb> mapobjectclassb
both these maps of same size. id keys common both maps. aim iterate on either maps create sorted (by objectclassc.name
) list<objectclassc>
objects such that:
objectclassc.name = objectclassa.name objectclassc.isreadonly = objectclassa.isreadonly || objectclassb.isreadonly objectclassc.location = objectclassb.location
this logic have right follows:
final list<objectclassc> list = mapobjectclassa.values() .stream() .map(a -> { new objectclassc(a.getname(), a.isreadonly() || mapobjectclassb.get(a.getid).isreadonly(), mapobjectclassb.get(a.getid).getlocation()) }) .sorted(comparator.comparing(objectclassc::getname)) .collect(collectors.<objectclassc> tolist());
my mapping , collection working fine, don't sorted collection of objectclassc
objects. please point out i'm going wrong?
not nice, works.
public class mapmerge_38837365 { public static void main(string[] args) { map<integer, a> = new hashmap(); as.put(1, new a("anna")); as.put(2, new a("maria")); as.put(3, new a("eva")); map<integer, b> bs = new hashmap(); bs.put(1, new b("adam")); bs.put(2, new b("edward")); bs.put(3, new b("jon")); stream.concat( as.entryset().stream(), bs.entryset().stream() ).collect(collectors.groupingby(map.entry::getkey)) .entryset().stream() .map(e -> e.getvalue()) .map(e -> { if (e.get(0).getvalue() instanceof a) return new ab((a) e.get(0).getvalue(), (b) e.get(1).getvalue()); return new ab((a) e.get(1).getvalue(), (b) e.get(0).getvalue()); }) .map(ab -> new c(ab.a.name+" " +ab.b.name)) .foreach(system.out::println); } }
"the high magic" happens in stream.concat
groupingby
. in 2 lines create stream of entries both maps. group them key map. elements same key (id) go 1 list<object>
. next step create new stream grouping map entryset().stream()
.
then extract value - entries same id
> map(e -> e.getvalue())
.
next step build ab
objects , omitted want extract type check , cast function. have pseudo pair ab
, nice candidate create c
class map(ab -> new c(ab.a.name+" " +ab.b.name))
.
classes a
, b
, c
, ab
:
class { final string name; a(string name) { this.name = name; } @override public string tostring() { return "a{" + "name='" + name + '\'' + '}'; } } class b { final string name; b(string name) { this.name = name; } @override public string tostring() { return "b{" + "name='" + name + '\'' + '}'; } } class c { final string name; c(string name) { this.name = name; } @override public string tostring() { return "c{" + "name='" + name + '\'' + '}'; } } class ab { final a; final b b; ab(a a, b b) { this.a = a; this.b = b; } @override public string tostring() { return "ab{" + "a=" + + ", b=" + b + '}'; } }
Comments
Post a Comment