Is an action like a sort list without intercepting the returned data in a local list allowed in Java?

I know how Java passes object references, but this piece of code really confused me.

Introduction: when the application starts, the service retrieves JSON data, parses it, and compares it with data models. Then we need to load a specific list into ListView, but we also need to sort and cancel it first. The line of code looks like this:

Collections.sort(Datastorage.getInstance().getHistoryKeys());
Collections.reverse(Datastorage.getInstance().getHistoryKeys());
//...
//pass HistoryKey object to some other method after a few lines

where the called method getHistoryKeys()simply returns a list object populated by the service when the application starts.

Is this really allowed in Java?

I would call it that, and it seems like I'm wrong.

ArrayList<Key> keysList = Datastorage.getInstance().getHistoryKeys()
Collections.sort(keysList);
Collections.reverse(keysList);
//...
//pass keysList object to another method after a few lines

Am I the only one who considers my code more understandable and understandable for those who will support the project after me?

+4
2

: keysList , , Datastorage.getInstance().getHistoryKeys() . , "Datastorage.getInstance(). GetHistoryKeys()" , , , , .

ArrayList , :

List<Key> keysList = Datastorage.getInstance().getHistoryKeys();
Collections.sort(keysList);
Collections.reverse(keysList);

sort + reverse :

Collections.sort(Datastorage.getInstance().getHistoryKeys(), Collections.reverseOrder());

... keysList, Datastorage.getInstance().getHistoryKeys() , .

+1

Datastorage.getInstance().getHistoryKeys() , . , , , . API , , , , .

, , , , , , :

Collections.sort
    (Datastorage.getInstance().getHistoryKeys(), Collections.reverseOrder());

, :

List<Key> keysList = Datastorage.getInstance().getHistoryKeys()
Collections.sort(keysList, Collections.reverseOrder());
+2

Source: https://habr.com/ru/post/1611098/


All Articles