Better to use TreeMap or order?

What would be better for programming practice?

Is it better to use the order by clause in sql or put the results in treemap vs hashmap in java for things like drop down?

+4
source share
4 answers

If you are thinking about performance, then large results are better sorted at the end of the database, especially when the columns you are sorting are indexed.

For smaller performance differences, the data sets may not be significant, but I think the SQL Order By clause will be simpler in most cases.

By the way, you still have to use LinkedHashMap instead of a simple HashMap, if you must save the already sorted data on the map before using it. Since LinkedHashMap will store data in input order, while HashMap will not.

If you want the data to be automatically sorted as it is placed on the map, you will need TreeMap or another sorted map implementation.

+2
source

Much depends on the implementation, the size of the list, etc. I would say if you can make an order in your sql query, which will lead to overhead in the database, and you will not need to pretend in the application. Also, make sure that the data structure that you use in the application keeps order, that is, does not insert the results into the hash map. But a lot depends on the specifics of what you are trying to achieve.

0
source

It also depends on the fields you are trying to β€œarrange”, or use them to sort as desired in Java code. But in most cases, I will depend on what DB makes the order.

0
source

At first...

Using HashMap will destroy any attempt to "order by" on your SQL, as you are not guaranteed to return the order that you put. Therefore, if you used "order" in your SQL, you would have to use LinkedHashMap for storage in order to preserve the order of the positions.

TreeMap will order for you if you install a valid comparator.

My preferences will be based on list size and usage. If many people access this list on a daily basis and update it, I would prefer their own user interfaces to handle the order. This part really depends.

0
source

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


All Articles