简体   繁体   中英

put 3 different values from database to Hashmaps

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 .

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. The integer number of load factor is your value for the map.

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

Use JDBC to access the database. Loop your incoming database data. For each database row, combine the two fields, and enter into the map with the third field.

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. You said you want a -1 as default.

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

Object

Alternatively, you could create a class to hold the date and flight number.

The new records feature in Java 16 is ideal for that. The compiler implicitly creates the constructor, getters, equals & hashCode , and toString . 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.

record DateFlight ( LocalDate localDate , Integer flightNumber ) {}

Define your map as:

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

And call put :

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

Choosing a Map

You have a variety of Map implementations to choose from, bundled with Java. Third parties provide implementations as well.

I would probably start with a HashMap to load data. Then make from that a non-modifiable map of unspecified class using Map.copyOf in Java 9 and later.

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:

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

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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