在这种情况下如何使用 HashMap?
Posted
技术标签:
【中文标题】在这种情况下如何使用 HashMap?【英文标题】:How to use HashMap for this scenario? 【发布时间】:2019-11-29 02:05:46 【问题描述】:给定客户要求的任务。
• 创建 Vehicle 类,其中包括车辆 Id(Integer) 作为车辆 number 和 lot(Integer) 作为停车场号码。实现 getId() 和 getLot() 返回车辆 Id 和 Lot。
创建包含 parkedVehicle、vehicle_lot_data 并实现以下内容的 ParkingLot 类
HashMap<Integer, Vehicle> parkedVehicle = new HashMap<>();
HashMap<Integer, Integer> vehicle_lot_data = new HashMap<>();
• parkVehicle(Vehicle, lot) 将车辆停在停车场,检查是否有效 停车,还要检查同一辆车不允许停在 多个批次。如果批次已被预订,则返回“批次已被占用” 如果我们要预订,则某人和“车辆已经存在” 同一辆车的多个批次。 • isLotBooked(int lot) 返回 boolean - 它检查已经预订的批次。返回假 对于空批次,对于预订批次为 True。 • isVehicleExists(int vehicleId) return boolean - 它检查已经存在的车辆。 如果车辆存在则返回 True 否则为不存在则返回 False 车辆。
第 3 步:(Database.java)
创建类数据库来存储停车场数据。
创建本地 SQLite 数据库 (parking_database.db) • 创建 DATABASE_NAME 变量并存储parking_database.db。 • 实施 onCreate, onUpgrade 用于数据库创建和数据库删除。 • ParkVehicle(Vehicle) - 将停放的车辆保存到数据库。 • getParkedVehicle()、getParkedVehicle(int vehicleId) - 全部停放 车辆返回(光标) • getAllBookedLots() - 获取所有预订 停车场返回(光标)
具有以下架构的设计表
CREATE TABLE parking_lots (id integer PRIMARY KEY,
lot_number INTEGER,
vehicle_number INTEGER UNIQUE);
• 实现 ParkVehicleNow(),它将从视图中获取用户输入数据, 并将数据存储在这些变量 vehicle_id_field_txt 和 将调用 park(int) 的 vehicle_lot_number_field_txt vehicle_id_field_txt, int vehicle_lot_number_field_txt) 函数 内部。 • park(int vehicle_id_field_txt, int vehicle_lot_number_field_txt) - 将使用 ParkingLot 类和 使用 ParkingLot 实例 pl 停放车辆并将该车辆保存到 使用 Databaseclass 实例 db 的数据库。 • 如果您无法停车 然后祝酒“车辆已停放选择不同的地段或 车辆”,否则“车辆停放”消息将被烘烤。 • 另外,不要 忘记对“Lot already taken”和“Vehicle”进行验证 已经存在”然后不要停放车辆。
但我没有得到第 2 步如何在 ParkingLot 中实现哈希图。
我累的是,这里基于场景,我无法在任何地方使用hashmap。
public class ParkingLot
HashMap<Integer, Vehicle> parkedVehicle = new HashMap<>();
HashMap<Integer, Integer> vehicle_lot_data = new HashMap<>();
public boolean isLotBooked(int lot, Context context)
Database db = new Database(context);
return db.isAlreadyLotsExists(String.valueOf(lot));
public boolean isVehicleExists(int vehicleId, Context context)
Database db = new Database(context);
return db.isAlreadyVehicleExists(String.valueOf(vehicleId));
public Vehicle()
public Vehicle(int primaryId,int id, int lot)
this.id = id;
this.lot = lot;
this.primaryId = primaryId;
public Vehicle(int id, int lot)
this.id = id;
this.lot = lot;
public int id;
public int getPrimaryId()
return primaryId;
public void setPrimaryId(int primaryId)
this.primaryId = primaryId;
public int primaryId;
public int getId()
return id;
public void setId(int id)
this.id = id;
public int getLot()
return lot;
public void setLot(int lot)
this.lot = lot;
public int lot;
public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Database db = new Database(this);
db.ParkVehicle(new Vehicle(1000,1));
db.ParkVehicle(new Vehicle(2000,2));
// Reading all contacts
Log.e("Reading: ", "Reading all contacts..");
List<Vehicle> contacts = db.getParkedVehicle(); //all parked vehicle
List<String> bookedSlots = db.getAllBookedLots(); //all booked slots
for (Vehicle cn : contacts)
String log = "Id: " + cn.getPrimaryId() + " ,Vehicle: " + cn.getId() + " ,Lot: " +
cn.getLot();
// Writing Contacts to log
Log.e("ContactsAllVehicles: ", log);
for (String cn : bookedSlots)
// Writing Contacts to log
Log.e("ContactsSlots: ", cn);
Vehicle vehicle= db.getParkedVehicle(1);
Log.e("ContactsLots",""+vehicle.getId());
//checking if lots already exisitng..
boolean alreadyExist=db.isAlreadyLotsExists("0");
Log.e("contactsslotsTest",""+alreadyExist);
public class Database extends SQLiteOpenHelper
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "vehicles";
private static final String KEY_ID = "id";
private static final String LOT_NUMBER = "lot_number";
private static final String VEHICLE_NUMBER = "vehicle_number";
private static final String PARKING_LOTS = "parking_lots"; //table name
public Database(Context context)
super(context, DATABASE_NAME, null, DATABASE_VERSION);
//3rd argument to be passed is CursorFactory instance
@Override
public void onCreate(SQLiteDatabase db)
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + PARKING_LOTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + LOT_NUMBER + " INTEGER,"
+ VEHICLE_NUMBER + " INTEGER UNIQUE" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + PARKING_LOTS);
// Create tables again
onCreate(db);
void ParkVehicle(Vehicle contact)
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(LOT_NUMBER, contact.getLot()); // Lot number
values.put(VEHICLE_NUMBER, contact.getId()); // Vehicle number
// Inserting Row
db.insert(PARKING_LOTS, null, values);
//2nd argument is String containing nullColumnHack
db.close(); // Closing database connection
Vehicle getParkedVehicle(int id) //single vehicle
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(PARKING_LOTS, new String[]KEY_ID,
LOT_NUMBER, VEHICLE_NUMBER, KEY_ID + "=?",
new String[]String.valueOf(id), null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Vehicle contact = new Vehicle(Integer.parseInt(cursor.getString(0)),
Integer.parseInt(cursor.getString(1)), Integer.parseInt(cursor.getString(2)));
// return contact
return contact;
public List<Vehicle> getParkedVehicle()
List<Vehicle> contactList = new ArrayList<Vehicle>();
// Select All Query
String selectQuery = "SELECT * FROM " + PARKING_LOTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst())
do
Vehicle contact = new Vehicle();
contact.setPrimaryId(Integer.parseInt(cursor.getString(0)));
contact.setLot(Integer.parseInt(cursor.getString(1)));
contact.setId(Integer.parseInt(cursor.getString(2)));
// Adding contact to list
contactList.add(contact);
while (cursor.moveToNext());
// return contact list
return contactList;
public List<String> getAllBookedLots()
List<String> contactList = new ArrayList<>();
// Select All Query
String selectQuery = "SELECT * FROM " + PARKING_LOTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToFirst();
// looping through all rows and adding to list
if (cursor!=null && cursor.moveToFirst())
while(cursor.isAfterLast() == false)
contactList.add(cursor.getString(cursor.getColumnIndex(LOT_NUMBER)));
Log.d("contactslotnumber",contactList.toString());
cursor.moveToNext();
return contactList;
public boolean isAlreadyLotsExists(String name)
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
String query="SELECT * FROM "+PARKING_LOTS+" WHERE "+LOT_NUMBER+" = "+name;
Cursor cursor = sqLiteDatabase.rawQuery(query,null);
if(cursor.getCount()>0)
return true;
else
return false;
public boolean isAlreadyVehicleExists(String name)
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
String query="SELECT * FROM "+PARKING_LOTS+" WHERE "+VEHICLE_NUMBER+" = "+name;
Cursor cursor = sqLiteDatabase.rawQuery(query,null);
if(cursor.getCount()>0)
return true;
else
return false;
// return contact list
public int updateContact(Vehicle contact)
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(LOT_NUMBER, contact.getLot());
values.put(VEHICLE_NUMBER, contact.getId());
// updating row
return db.update(PARKING_LOTS, values, KEY_ID + " = ?",
new String[]String.valueOf(contact.getPrimaryId()));
// Deleting single contact
public void deleteContact(Vehicle contact)
SQLiteDatabase db = this.getWritableDatabase();
db.delete(PARKING_LOTS, KEY_ID + " = ?",
new String[]String.valueOf(contact.getPrimaryId()));
db.close();
// Getting contacts Count
public int getContactsCount()
String countQuery = "SELECT * FROM " + PARKING_LOTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
这是我所做的,但要求中缺少一些步骤。例如:第 2 步我不确定在哪里使用 hashmap 来使用 ParkingLot 类中的 parkVehicle 方法,同样在 MainActivity 中使用第 4 步。
【问题讨论】:
【参考方案1】:可能在缓存数据库数据的上下文中给出了哈希图。当您写入与车辆/批次相关的数据时,您使用 hashmaps 作为缓存(使用回写或直写策略,以符合您的要求或需要为准),当您读取数据时,您总是从 hashmaps 中读取它们,即缓存
回想一下,回写意味着您将新数据写入缓存而不是 DB(数据稍后写入 DB),而直写意味着无论何时写入新数据,它都会同时写入缓存和数据库
【讨论】:
以上是关于在这种情况下如何使用 HashMap?的主要内容,如果未能解决你的问题,请参考以下文章
在这种情况下,如何使用 barh() 在条形图上显示值? [复制]