/* package whatever; // don't place package name! */
import java.util.*;
import java.util.stream.Collectors;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
List < CountryCount
> listOfCountryCount
= List.
of( new CountryCount( "Peru" , 17L ) ,
new CountryCount( "Xanadu" , 128L ) ,
new CountryCount( null , 42L ) ,
new CountryCount( "Andorra" , null )
);
listOfCountryCount
.stream()
.collect(
Collectors.toMap(
countryCount -> Objects.isNull( countryCount.getCountry() ) ? "unknown" : countryCount.getCountry() ,
countryCount
-> Objects.
isNull( countryCount.
getCount() ) ? Long.
valueOf( 0 ) : countryCount.
getCount() ,
( existing , replacement ) -> existing , // In case of duplicate key conflict, first one wins.
)
);
System.
out.
println( "listOfCountryCount = " + listOfCountryCount
); System.
out.
println( "map = " + map
); }
}
final class CountryCount
{
private final Long count
;
{
this.country = country;
this.count = count;
}
public String getCountry
( ) { return country
; }
public Long getCount
( ) { return count
; }
@Override
public boolean equals
( Object obj
) {
if ( obj == this ) return true;
if ( obj == null || obj.getClass() != this.getClass() ) return false;
var that = ( CountryCount ) obj;
return Objects.equals( this.country , that.country ) &&
Objects.equals( this.count , that.count );
}
@Override
public int hashCode ( )
{
return Objects.hash( country , count );
}
@Override
{
return "CountryCount[" +
"country=" + country + ", " +
"count=" + count + ']';
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS51dGlsLnN0cmVhbS5Db2xsZWN0b3JzOwoKLyogTmFtZSBvZiB0aGUgY2xhc3MgaGFzIHRvIGJlICJNYWluIiBvbmx5IGlmIHRoZSBjbGFzcyBpcyBwdWJsaWMuICovCmNsYXNzIElkZW9uZQp7CiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbiAoIFN0cmluZ1tdIGFyZ3MgKSB0aHJvd3MgamF2YS5sYW5nLkV4Y2VwdGlvbgogICAgewogICAgICAgIExpc3QgPCBDb3VudHJ5Q291bnQgPiBsaXN0T2ZDb3VudHJ5Q291bnQgPSBMaXN0Lm9mKAogICAgICAgICAgICAgICAgbmV3IENvdW50cnlDb3VudCggIlBlcnUiICwgMTdMICkgLAogICAgICAgICAgICAgICAgbmV3IENvdW50cnlDb3VudCggIlhhbmFkdSIgLCAxMjhMICkgLAogICAgICAgICAgICAgICAgbmV3IENvdW50cnlDb3VudCggbnVsbCAsIDQyTCApICwKICAgICAgICAgICAgICAgIG5ldyBDb3VudHJ5Q291bnQoICJBbmRvcnJhIiAsIG51bGwgKQogICAgICAgICk7CgoJCU5hdmlnYWJsZU1hcCA8IFN0cmluZywgTG9uZyA+IG1hcCA9CgkJICAgICAgICBsaXN0T2ZDb3VudHJ5Q291bnQKCQkgICAgICAgICAgICAgICAgLnN0cmVhbSgpCgkJICAgICAgICAgICAgICAgIC5jb2xsZWN0KAoJCSAgICAgICAgICAgICAgICAgICAgICAgIENvbGxlY3RvcnMudG9NYXAoCgkJICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBjb3VudHJ5Q291bnQgLT4gT2JqZWN0cy5pc051bGwoIGNvdW50cnlDb3VudC5nZXRDb3VudHJ5KCkgKSA/ICJ1bmtub3duIiA6IGNvdW50cnlDb3VudC5nZXRDb3VudHJ5KCkgLAoJCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgY291bnRyeUNvdW50IC0+IE9iamVjdHMuaXNOdWxsKCBjb3VudHJ5Q291bnQuZ2V0Q291bnQoKSApID8gTG9uZy52YWx1ZU9mKCAwICkgOiBjb3VudHJ5Q291bnQuZ2V0Q291bnQoKSAsIAoJCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgKCBleGlzdGluZyAsIHJlcGxhY2VtZW50ICkgLT4gZXhpc3RpbmcgLCAgLy8gSW4gY2FzZSBvZiBkdXBsaWNhdGUga2V5IGNvbmZsaWN0LCBmaXJzdCBvbmUgd2lucy4KCQkgICAgICAgICAgICAgICAgICAgICAgICAJCVRyZWVNYXAgOjogbmV3IAoJCSAgICAgICAgICAgICAgICAgICAgICAgICkKCQkgICAgICAgICAgICAgICAgKTsKCQkgICAgICAgICAgICAgICAgCiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCAibGlzdE9mQ291bnRyeUNvdW50ID0gIiArIGxpc3RPZkNvdW50cnlDb3VudCApOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbiggIm1hcCA9ICIgKyBtYXAgKTsKICAgIH0KfQoKZmluYWwgY2xhc3MgQ291bnRyeUNvdW50CnsKICAgIHByaXZhdGUgZmluYWwgU3RyaW5nIGNvdW50cnk7CiAgICBwcml2YXRlIGZpbmFsIExvbmcgY291bnQ7CgogICAgQ291bnRyeUNvdW50ICggU3RyaW5nIGNvdW50cnkgLCBMb25nIGNvdW50ICkKICAgIHsKICAgICAgICB0aGlzLmNvdW50cnkgPSBjb3VudHJ5OwogICAgICAgIHRoaXMuY291bnQgPSBjb3VudDsKICAgIH0KCiAgICBwdWJsaWMgU3RyaW5nIGdldENvdW50cnkgKCApIHsgcmV0dXJuIGNvdW50cnk7IH0KCiAgICBwdWJsaWMgTG9uZyBnZXRDb3VudCAoICkgeyByZXR1cm4gY291bnQ7IH0KCiAgICBAT3ZlcnJpZGUKICAgIHB1YmxpYyBib29sZWFuIGVxdWFscyAoIE9iamVjdCBvYmogKQogICAgewogICAgICAgIGlmICggb2JqID09IHRoaXMgKSByZXR1cm4gdHJ1ZTsKICAgICAgICBpZiAoIG9iaiA9PSBudWxsIHx8IG9iai5nZXRDbGFzcygpICE9IHRoaXMuZ2V0Q2xhc3MoKSApIHJldHVybiBmYWxzZTsKICAgICAgICB2YXIgdGhhdCA9ICggQ291bnRyeUNvdW50ICkgb2JqOwogICAgICAgIHJldHVybiBPYmplY3RzLmVxdWFscyggdGhpcy5jb3VudHJ5ICwgdGhhdC5jb3VudHJ5ICkgJiYKICAgICAgICAgICAgICAgIE9iamVjdHMuZXF1YWxzKCB0aGlzLmNvdW50ICwgdGhhdC5jb3VudCApOwogICAgfQoKICAgIEBPdmVycmlkZQogICAgcHVibGljIGludCBoYXNoQ29kZSAoICkKICAgIHsKICAgICAgICByZXR1cm4gT2JqZWN0cy5oYXNoKCBjb3VudHJ5ICwgY291bnQgKTsKICAgIH0KCiAgICBAT3ZlcnJpZGUKICAgIHB1YmxpYyBTdHJpbmcgdG9TdHJpbmcgKCApCiAgICB7CiAgICAgICAgcmV0dXJuICJDb3VudHJ5Q291bnRbIiArCiAgICAgICAgICAgICAgICAiY291bnRyeT0iICsgY291bnRyeSArICIsICIgKwogICAgICAgICAgICAgICAgImNvdW50PSIgKyBjb3VudCArICddJzsKICAgIH0KfQ==
listOfCountryCount = [CountryCount[country=Peru, count=17], CountryCount[country=Xanadu, count=128], CountryCount[country=null, count=42], CountryCount[country=Andorra, count=null]]
map = {Andorra=0, Peru=17, Xanadu=128, unknown=42}