Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to reduce this? (or destructuring assignment in Java)

Tags:

java

I have a getSource method that return a Pair<String,String> and I want to call the method and associate the key and the value to two different variables.

I do it like this:

entryMode = getSource(encounterAdmitSource).getKey();
admitSource = getSource(encounterAdmitSource).getValue();

My question is: is there a way in java to do something like that:

getSource(encounterAdmitSource){ //Something 
    entryMode = getKey();
    admitSource=getValue();
}

I want to initialize entryMode and admitSource by call only once the method and without create a Pair<String,String> object. Does it exist? Thank you.

like image 872
Couldosh Avatar asked Apr 15 '26 10:04

Couldosh


1 Answers

Without using Optional, this is the best you could do (safely) for the given example

final Pair<String, String> source = getSource(encounterAdmitSource);
if (source!=null) { 
    String entryMode = source.getKey();
    String admitSource=source.getValue();
} 
like image 86
OneCricketeer Avatar answered Apr 17 '26 00:04

OneCricketeer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!