将3个不同的值从数据库放入Hashmaps [英] put 3 different values from database to Hashmaps

查看:51
本文介绍了将3个不同的值从数据库放入Hashmaps的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据库中,我有3列: Date(PrimaryKey,TEXT) FlightNumber(PrimaryKey,INTEGER) LoadEstimate(INTEGER).我想做的是将数据库中的所有值都放入一个哈希图中.我必须确保所有数据类型都是正确的,以便将它们加载到其中,并想通过用户输入(日期和航班号)过滤 LoadEstimate 数据,然后返回预测的数字表示 LoadEstimate ,如果没有返回 -1 .

In my database, I have 3 columns: Date(PrimaryKey, TEXT), FlightNumber(PrimaryKey, INTEGER), and LoadEstimate(INTEGER). What I would like to do is put all the values from database into a hashmap. I have to make sure that all the datatypes are correct in order to load them into it and would like to filter the LoadEstimate data by user input (a date and the flight number) and then return the predicted number for LoadEstimate and if none return -1.

这是我的数据库:

推荐答案

不强迫您创建一种新的对象来存储行信息的最简单的解决方案是:

The most simple solution that doesn't force you to create a new kind of Object to store a row's info is this one:

Hashmap包含键值对,并将每个唯一键映射到一个值.

A Hashmap contains key-value pairs and maps each unique key to a value.

根据您提供的表具有组合主键的SQL数据库信息,换句话说,您的主键由两列组成(日期为TEXT类型,而flightNumber为INTEGER类型).

According to the SQL database info you have provided your table has a composite primary key, in other words your primary key consists of two columns (a date of type TEXT and a flightNumber of type INTEGER).

您知道主键具有唯一值,以帮助您在查询表中的数据时进行区分.因此,您应该将哈希表的主键作为键存储在表中.

As you know a primary key has unique values in order to help you make distinctions when querying data in a table. So, you should store in your hashmap as a key the primary key of your table.

现在,由于主键由两列组成,并且它们的值组合有助于识别唯一性,因此您可以创建一个数组或一个列表,并在其中存储日期和* flightNumber *.然后,将这个数组/列表作为键添加到哈希映射中,并将所需的其余字段(在本例中为INTEGER类型的loadEstimate)添加为其值.

Now, since your primary key consists of two columns and it's the combination of their values that helps you identify uniqueness, you can create an array or a list and store there the date and the * flightNumber*. Then, you will add to your hashmap this array/list as a key and the rest of the fields you want (in our case the loadEstimate of type INTEGER) as its value.

上面的代码应该是这样的:

The above in code should be something like this:

HashMap<ArrayList<Object>, int> myMap = new HashMap<>(); //Create your hashmap

while (rs.next()) {
    LocalDate  date = LocalDate.parse(rs.getString("Date"));
    int  flightnumber = Integer.parseInt(rs.getString("FlightNumber"));
    int loadestimate = Integer.parseInt(rs.getString("LoadEstimate"));

    //Create array resembling the primary key
    ArrayList<Object> mapKey = new ArrayList<>();
    mapKey.add(date);
    mapKey.add(new Integer(flightnumber));

   //Store to Map the key and the value
   myMap.put(mapKey, loadestimate);
}
//then do sth with the hashmap

请注意,我创建了一个类型为Object的通用对象数组,以便能够存储不同种类的对象.这是可能的,因为它们都是Object类的子类.

Notice that I create an array of generic objects of type Object in order to be able to store objects of different kind. This is possible, since they both subclasses of the class Object.

这篇关于将3个不同的值从数据库放入Hashmaps的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆