在迭代中创建对象 - 更好的方法

发布于 2024-12-08 19:35:54 字数 1594 浏览 2 评论 0原文

当我开发某些东西时,我想知道在迭代某些东西时创建一堆对象可能更好/更快的方法。

假设我们有这个包装类:

public class Photo{
    private String url;
    private String creator;

    public Photo(String url, String creator){
        this.url = url;
        this.creator = creator;
    }
    // Getter methods down here...
}

我们有一个 JSONArray 照片,我们只需要其中的 url - 和 creator - 字符串:

JSONArray photos = json.getJSONArray("photos");
Photo[] photo_arr = new Photo[photos.length()];
// Iterate:
for (int i = 0; i < photos.length(); i++){
    // Create the objects here.
}

现在,我看到三种可能的解决方案:

创建临时变量

创建一些临时变量,从当前对象获取所需的值,然后构造新的 Photo 对象:

// Iterate:
String url = "";
String creator = "";
for (int i = 0; i < photos.length(); i++){
    url = photos[i].getString("url");
    creator = photos[i].getString("creator");
    photo_arr[i] = new Photo(url, creator);
}

直接在构造函数中使用返回值

不要创建临时变量,而是使用从构造函数调用中的 getString() 方法:

// Iterate:
for (int i = 0; i < photos.length(); i++){
    photo_arr[i] = new Photo(
        photos[i].getString("url"),
        photos[i].getString("creator")
    );
}

使用 setter 方法

urlcreator 到包装类并使用它们来填充对象:

// Iterate:
for (int i = 0; i < photos.length(); i++){
    photo_arr[i] = new Photo();
    photo_arr[i].setUrl( photos[i].getString("url") );
    photo_arr[i].setCreator( photos[i].getString("creator") );
}

这里哪种方法更好/更快/更干净?

As I was developing something, I wondered what might be the better/faster approach to create a bunch of objects while iterating over something.

Lets say we have this wrapper-class:

public class Photo{
    private String url;
    private String creator;

    public Photo(String url, String creator){
        this.url = url;
        this.creator = creator;
    }
    // Getter methods down here...
}

And we have a JSONArray of photos from which we want only the url- and the creator-strings:

JSONArray photos = json.getJSONArray("photos");
Photo[] photo_arr = new Photo[photos.length()];
// Iterate:
for (int i = 0; i < photos.length(); i++){
    // Create the objects here.
}

Now, I see three possible solutions:

Creating temporary variables

Creating some temporary variables which get the desired values from the current object and then construct the new Photo-object:

// Iterate:
String url = "";
String creator = "";
for (int i = 0; i < photos.length(); i++){
    url = photos[i].getString("url");
    creator = photos[i].getString("creator");
    photo_arr[i] = new Photo(url, creator);
}

Use the returned-values directly in the constructor

Don't create temporary variables but use the returned values from the getString()-method in the constructor-call:

// Iterate:
for (int i = 0; i < photos.length(); i++){
    photo_arr[i] = new Photo(
        photos[i].getString("url"),
        photos[i].getString("creator")
    );
}

Using setter-methods

Adding a no-parameter constructor and setter-methods for the url and creator to the wrapper-class and use them to populate the object:

// Iterate:
for (int i = 0; i < photos.length(); i++){
    photo_arr[i] = new Photo();
    photo_arr[i].setUrl( photos[i].getString("url") );
    photo_arr[i].setCreator( photos[i].getString("creator") );
}

Which one is the better/faster/cleaner approach here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在深巷 2024-12-15 19:35:54

前两种方法类似。如果您觉得变量更具可读性和可维护性,请引入变量。最后一个使 Photo 类可变,而在前两个方法中它是不可变的(尽管不是并发中使用的最严格的含义)。

如果可能的话,您应该始终支持不变性,主要是因为它使程序更加健壮:在前两种情况下,Photo 始终处于可用的、完全构造的状态。不在第三个。

请注意,性能在这里无关紧要:这三种方法肯定会导致可比较的时间,并且并不是那种会使程序变慢的代码。不要过早优化。这是万恶之源。

The two first methods are similar. Introduce variables if you feel it's more readable and maintainable. The last one makes the Photo class mutable whereas it was immutable in the first two methods (not in the strictest meaning used in concurrency, though).

You should always favor immutability when possible, mainly because it makes programs more robust: a Photo is always in a usable, fully constructed state in the first two cases. Not in the third one.

Note that performance is irrelevant here: the three ways will certainly lead to comparable times, and it's not that kind of code that will make a program slow. Don't optimize prematurely. It's the root of all evil.

灼疼热情 2024-12-15 19:35:54

您为此处查看的代码提供的解决方案之间的速度差异可以忽略不计。可读性实际上比人们想象的更重要。您的最终用户体验到 1% 的速度提升对您来说更重要吗?或者您将能够在未来的版本中维护此代码?答案通常是后者。

因此,我会选择创建临时变量使用 setter-methods(在我看来)可读性较差,但可能会更慢,因为您必须输入每个 setter 方法并分配这些资源。 直接在构造函数中使用返回值(在我看来)可读性较差,因此在这种情况下应该避免;但如果返回的对象尺寸较大,则分配资源以将其存储在临时值中实际上可能是不可忽略的。

The difference in speed is negligible between the solutions you've provided for the code you're looking at here. Readability is actually more important than one might think. Is it more important to you that your end-users experience a 1% speed increase? Or that you will be able to maintain this code in future versions? The answer is usually the latter.

For that reason, I'd go with Creating temporary variables. Using setter-methods is (in my opinion) less readable, but potentially slower since you will have to enter each of those setter methods and allocate those resources. Us[ing] the returned-values directly in the constructor is (again, in my opinion) less readable and should therefore be avoided in cases like this; but if the returned object was something of a larger size, then allocating the resources to store it in a temporary value may actually be non-negligible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文