简体   繁体   English

将 3 个不同的值从数据库放入 Hashmaps

[英]put 3 different values from database to Hashmaps

In my database, I have 3 columns: Date(PrimaryKey, TEXT) , FlightNumber(PrimaryKey, INTEGER) , and LoadEstimate(INTEGER) .在我的数据库中,我有 3 列: Date(PrimaryKey, TEXT)FlightNumber(PrimaryKey, INTEGER)LoadEstimate(INTEGER) What I would like to do is put all the values from database into a hashmap.我想做的是将数据库中的所有值放入 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 .我必须确保所有数据类型都正确才能将它们加载到其中,并且希望通过用户输入(日期和航班号)过滤LoadEstimate数据,然后返回LoadEstimate的预测数字,如果没有返回-1 .

Here is my database:这是我的数据库:

在此处输入图像描述

Text文本

Combine your date and flight number together as a String.将您的日期和航班号组合成一个字符串。 Perhaps include a delimiter between them for easier reading by humans and parsing by machine.也许在它们之间包含一个分隔符,以便于人类阅读和机器解析。

That combined text is your key for the map.该组合文本是 map 的关键。 The integer number of load factor is your value for the map. integer 的负载因子数是您对 map 的值。

Map< String , Integer > map = new HashMap<>() ; 

Use JDBC to access the database.使用JDBC访问数据库。 Loop your incoming database data.循环传入的数据库数据。 For each database row, combine the two fields, and enter into the map with the third field.对于每个数据库行,将这两个字段组合起来,并使用第三个字段输入 map。

String k = localDate.toString() + "F" + flightNumber ;
Integer v = loadFactor ;
map.put( k , v ) ;

Retrieve.取回。 The getOrDefault method returns a default value if the map does not find a value.如果 map 未找到值, getOrDefault方法将返回默认值。 You said you want a -1 as default.你说你想要一个 -1 作为默认值。

Integer loadFactor = map.getOrDefault( k , Integer.valueOf( -1 ) ;

Object Object

Alternatively, you could create a class to hold the date and flight number.或者,您可以创建一个 class 来保存日期和航班号。

The new records feature in Java 16 is ideal for that. Java 16 中的新记录功能非常适合此功能。 The compiler implicitly creates the constructor, getters, equals & hashCode , and toString .编译器隐式创建构造函数、getter、 equals & hashCodetoString Note that you can define your record locally, within the code block where you use it.请注意,您可以在使用它的代码块中本地定义您的记录。

For older Java, write a conventional class instead of a record.对于较旧的 Java,写入常规的 class 而不是记录。

record DateFlight ( LocalDate localDate , Integer flightNumber ) {}

Define your map as:将您的 map 定义为:

Map< DateFlight , Integer > map = new HashMap<>() ; 

And call put :并调用put

DateFlight k = new DateFlight( localDate , flightNumber ) ;
Integer v = loadFactor ;
map.put( k , v ) ;

Choosing a Map选择Map

You have a variety of Map implementations to choose from, bundled with Java.您有多种Map实现可供选择,与 Java 捆绑在一起。 Third parties provide implementations as well.第三方也提供实现。

I would probably start with a HashMap to load data.我可能会从HashMap开始加载数据。 Then make from that a non-modifiable map of unspecified class using Map.copyOf in Java 9 and later.然后使用Map.copyOf在 ZD52387880E2138177A 和更高版本中使用 Map.copyOf 制作未指定 class 的不可修改的 map。

Map< DateFlight , Integer > dataLoad = new HashMap<>() ; 
… load data
Map< DateFlight , Integer > map = Map.copyOf( dataLoad ) ; // Make unmodifiable `Map`. 

Here is a graphic I made as a guide to the bundled maps.这是我制作的一张图表,作为捆绑地图的指南。

在此处输入图像描述

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:不会强迫您创建一种新的 Object 来存储行信息的最简单的解决方案是:

A Hashmap contains key-value pairs and maps each unique key to a value. Hashmap 包含键值对并将每个唯一键映射到一个值。

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).根据 SQL 数据库信息,您提供的表具有复合主键,换句话说,您的主键由两列(TEXT 类型的日期和 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.因此,您应该在 hashmap 中存储表的主键作为键。

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*.现在,由于您的主键由两列组成,并且它们的值的组合可以帮助您识别唯一性,因此您可以创建一个数组或列表并将日期和 * 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.然后,您将将此数组/列表作为键添加到 hashmap 中,并将所需字段的 rest(在我们的示例中为 INTEGER 类型的 loadEstimate)作为其值。

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

Notice that I create an array of generic objects of type Object in order to be able to store objects of different kind.请注意,我创建了一个类型为 Object 的通用对象数组,以便能够存储不同类型的对象。 This is possible, since they both subclasses of the class Object.这是可能的,因为它们都是 class Object 的子类。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM