简体   繁体   English

Bean class Vs Collection:我应该更喜欢哪一个保存数据

[英]Bean class Vs Collection : which one should i prefer to hold data

I have a TestDTO class which holds the 2 input data from user, next step is to fetch the several data from database, lets say i am fetching ten String type values from database which requires further to execute the business logic. 我有一个TestDTO类,它保存来自用户的2个输入数据,下一步是从数据库中获取几个数据,假设我从数据库中获取十个String类型值,这需要进一步执行业务逻辑。

I wanted to know the best way to hold the data (in terms of saving memory space and performance) 我想知道保存数据的最佳方法(在节省内存空间和性能方面)

  1. Add 10 more fields in the existing TestDTO class and set database values at run time 在现有TestDTO类中添加10个字段,并在运行时set数据库值
  2. Use java.util.collection (List/Map/..) 使用java.util.collection (List/Map/..)
  3. Create another DTO/Bean class for 10 String values 为10个String值创建另一个DTO / Bean类

If you want modularity of your code 3rd point is better, but for simplicity you should use a HashMap, like: 如果你想要代码的模块化第三点更好,但为了简单起见你应该使用HashMap,如:

HashMap map = new HashMap();

map.put("string1",value);
.....
and so on.

This post can be useful for you : https://forums.oracle.com/thread/1153857 这篇文章对您有用: https//forums.oracle.com/thread/1153857

If TestDTO and the new values fetched are coming from the same table in the database, then they should be in the same class. 如果TestDTO和获取的新值来自数据库中的同一个表,那么它们应该在同一个类中。 Else, the new values should ideally be in another DTO. 否则,理想情况下,新值应该在另一个DTO中。 I do not know the exact scenario that you have, so given these constraints, 2nd option goes out of the window. 我不知道你有什么确切的情况,所以考虑到这些限制,第二个选项就会消失。 And options 1 and 3 will depend on your scenario. 选项1和3将取决于您的方案。 Always hold values from a single table in one object(preferably). 始终将一个表中的值保存在一个对象中(最好)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该更喜欢ifPresent方法而不是isPresent方法吗? - Should I prefer the `ifPresent` method to the `isPresent` one? Spark 2.2.0 API:我应该更喜欢使用Groupby结合Aggregate的Dataset或使用ReduceBykey结合RDD的数据集 - Spark 2.2.0 API: Which one should i prefer Dataset with Groupby combined with aggregate or RDD with ReduceBykey 在编写单元测试时,我应该更喜欢哪个作为期望值? - When writing the unit test, which one should I prefer as the expected value? 对于给定类型的数据及其使用,我应该更喜欢哪种数据结构 - Which Data Structure I should prefer for the given kind of data and its use ReentrantLock和Stampedlock有什么区别? 哪一个更喜欢? - What is the difference between ReentrantLock vs stampedlock? Which one to prefer? 我应该选择一个大循环还是许多较小的循环? - Should I prefer one large loop or many smaller loops? Jetty VS Tomcat Maven插件-我应该使用哪一个? - Jetty VS Tomcat Maven Plugin - which one should I use? 我应该更喜欢 ThreadLocalRandom 还是 SecureRandom? - Should I prefer ThreadLocalRandom or SecureRandom? 我更喜欢哪种模式? - Which pattern would I prefer? 我不熟悉Java中的自动化测试。 我应该选择哪种工具? JUnit还是TestNG? - I am new to automated testing in Java. Which tool should I prefer? JUnit or TestNG?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM