lion_bash
lion_bash

Reputation: 1409

Java object destructuring

In javascript there is object destructuring so we can break down objects and just use the end key if the intermidiate objects are resused multiple times. e.g)

const person = {
  firstName: "Bob",
  lastName: "Marley",
  city: "Space"
}

So instead of calling person.<> to get each value we can destructure it like this

console.log(person.firstName) 
console.log(person.lastName) 
console.log(person.city) 

Destructured:

const { firstName, lastName, city } = person;

And call like this:

console.log(firstName)
console.log(lastName)
console.log(city)

Is there something similar in Java? I have this Java Object that I need to get the value from and have to call long intermediate object names like this:

myOuterObject.getIntermediateObject().getThisSuperImportantGetter()
myOuterObject.getIntermediateObject().getThisSecondImportantGetter()
...

I would like this destructure it somehow and just call the last method getThisSuperImportantGetter(), getThisSecondImportantGetter() for cleaner code.

Upvotes: 41

Views: 50757

Answers (3)

user5132301
user5132301

Reputation:

As far as I know, Java doesn't support this.

Another JVM language called Kotlin does support this

Kotlin | Destructuring Declarations

Upvotes: 24

chubbsondubs
chubbsondubs

Reputation: 38857

As noted Java does not support destructured assignments like Javascript. But, Groovy, a jvm language, also supports it like so:

def (a,b,c) = [10, 20, 'foo']

Or with data types like so:

def (int i, String j) = [10, 'foo']

Upvotes: 1

Stefan Haberl
Stefan Haberl

Reputation: 10549

Java Language Architect Brian Goetz has recently talked about adding destructuring to an upcoming version of Java. Look for the Sidebar: pattern matching chapter in his paper:

Towards Better Serialization

I strongly dislike the current proposal of the syntax, but according to Brian your use case will look like the following (please note, that at this point this is a proposal only and will not work with any current version of Java):

public class Person {
    private final String firstName, lastName, city;

    // Constructor
    public Person(String firstName, String lastName, String city) { 
        this.firstName = firstName;
        this.lastName = lastName;
        this.city = city;
    }

    // Deconstruction pattern
    public pattern Person(String firstName, String lastName, String city) { 
        firstName = this.firstName;
        lastName = this.lastName;
        city = this.city;
    }
}

You should than be able to use that deconstruction pattern for instance in an instanceof check like so:

if (o instanceof Person(var firstName, lastName, city)) {
   System.out.println(firstName);
   System.out.println(lastName);
   System.out.println(city);
}

Sorry, Brian does not mention any direct destructuring assignment in his examples, and I'm not sure if and how these will be supported.

On a side note: I do see the intended similarity to the constructor, but I personally do not like the current proposal that much, because the arguments of the "deconstructor" feel like out-parameters (Brian says as much in his paper). For me this is rather counter-intuitiv in a world where everybody is talking about immutability and making your method parameters final.

I would rather like to see Java jump over the fence and support multi-value return types instead. Something along the lines of:

    public (String firstName, String lastName, String city) deconstruct() { 
        return (this.firstName, this.lastName, this.city);
    }

Upvotes: 29

Related Questions