If we’re putting all the transactions in hashmap in the first for loop, then in the 2nd for loop when we’re going through all the transactions, wouldn’t we see the same transaction at both places and time will be the same too and so it will be within the 60 minute window since you put
@akarsh18213 ай бұрын
Oh nvm, there’s another condition that the city should be different, if it could be the same then there would be some problems. Thank you, very helpful video, learnt alot!
@sleepycracker47323 ай бұрын
good comment. glad you get it
@ryanhossain67182 жыл бұрын
Can you explain what you’re doing in the first for loop that has map.putIfAbsent
@sleepycracker47322 жыл бұрын
Good question. Using putIfAbsent function is to check do I have my "tran.name(transaction name)" in the map or not. If there is a tran.name in my map, I do not overwrite my existed key and also I append every "tran(transaction data)" along with the tran.name. If I do not have this tran.name in the map, I will create it and also create an empty list for its map value. note: You gonna initialize your array before you append any data into the list. You can use map.containsKey to check it as well, but I will have to write out an if statement for it. This is just an alternative way for me to practice one of the vary solutions to check "map.containsKey" is this helpful?
@ryanhossain67182 жыл бұрын
@@sleepycracker4732 so you are using Tran.name as the key for the hashmap map and if that name doesn’t exist add an empty array for it Next line you get the trans by name as the key and the value of that would be the empty array which you populate with the trans data? So then what happens when the name already exists you just never add it to map?
@sleepycracker47322 жыл бұрын
Key is the train.name, and value is the trans data. Once again, I do check whether or not the key exists or not in the map first. If so, I don't overwrite my data. If the key doesn't exist in the map, I create the key and initialize the arraylist for this key. for the second for loop, I check with my map(a fully built transactions for the people) to see if I have invalid transactions. (>1000 or 60 minutes same person in different city) Resources regarding putIfAbsent: www.geeksforgeeks.org/hashmap-putifabsentkey-value-method-in-java-with-examples/ docs.oracle.com/javase/8/docs/api/java/util/Map.html
@ryanhossain67182 жыл бұрын
@@sleepycracker4732 sorry for bothering you but so doesn’t this assume the name will only show up max twice how would you compare [name 1 t30, name 1 t91, name 1 t92] (dif location) Name 1 : time 30 added to map Second for loop Name 1: time 91 so wouldn’t be invalid Name 1: time 92 would be invalid because the previous persons time was within 60 and assuming dif location Is it not comparing to the trans in the map which is time 30 making both valid
@sleepycracker47322 жыл бұрын
That's ok. Glad you asking question. Let's just use the same dataset. [(name1, 30, 600, cityA), (name1, 91, 700, cityB), (name1, 92, 800, cityC)] only one person in this dataset, but for every transaction, its city is different. First loop: *First Iteration* I use putIfAbsent function to check does "name1" in the map or not, and map said "No", so I create a key for this transaction using name1. In the meanwhile, I create an empty list for this key. Once, I created a key for it. I append the current transaction into the map. map data: {name1, [(name1, 30, 600, cityA)]} *Second Iteration* I used putIfAbsent function to see does "name1" in the map or not, and map said "Yes", so I don't do anything. the next line is "map.get(tran.name).add(tran)". So I push the current tran into map map data: {name1, [(name1, 30, 600, cityA), (name1, 91, 700, cityB)]} *Third Iteration* I used putIfAbsent function to see does "name1" in the map or not, and map said "Yes", so I don't do anything. the next line is "map.get(tran.name).add(tran)". So I push the current tran into map map data: {name1, [(name1, 30, 600, cityA), (name1, 91, 700, cityB), (name1, 92, 800, cityC)]} Second loop: please describe it to me what happens for the second loop. #YourTurn