I'm running into issues with a Hashmap in my program overwriting the value of previous Hashmap entries despeinte the two entries having different keys.
public void drawTheNumbers() {
final int NO_WIN = 0;
int playerOneTicketWinning = 0;
Tuple numberTransporter = new Tuple (0,0);
for(Journal entry : theJournal) {
playerOneTicketWinning = entry.getWinning();
if (winningsPerPlayer.containsKey(entry.getPerson())) {
numberTransporter.playerTotalWinnings = numberTransporter.playerTotalWinnings + playerOneTicketWinning;
numberTransporter.playerSimilarities = numberTransporter.playerSimilarities + entry.getSimilarities();
winningsPerPlayer.put(entry.getPerson(), numberTransporter );
}else {
numberTransporter.playerTotalWinnings = playerOneTicketWinning;
numberTransporter.playerSimilarities = entry.getSimilarities();
winningsPerPlayer.put(entry.getPerson(), numberTransporter);
}
}
for (Map.Entry<String, Tuple> player: winningsPerPlayer.entrySet()) {
int temp1 = numberTransporter.getplayerTotalWinnings();
int temp2 = NO_WIN;
if (temp1 == temp2) {
noWinMessage (player.getKey());
}else {
playerWinningsMessage(player.getKey(), numberTransporter);
}
}
}
When this loop is run the second time around it will overwrite the values of playerTotalWinnings and playerSimilarities in the method above, but also in the previously entry in the Hashmap created by winningsPerPlayer.put
The key is the name of the player entered by the user when running the program. Multiple users can run the program within the same session.
The key might be different. But the value is not -_-°
You need to create a new Tuple inside the loop, instead of constantly modifying the one and only you created before the loop.
Generally something like a Tuple should not need to be modified after its creation, it should be created with the two values it will contain and never changed from that. So it should be made immutable, with final values, so that you avoid that kind of silly mistakes.
Related
I am trying to return a number returned by a query, which is running inside a list loop,which again runs inside another list loop. May be I am missing something out which is giving me a wrong data. The code as below.
for (CfCust cust : custList) {
int custCount = 0;
int billCustCount = 0;
List<CfBillCust> billCustList = projectMasterDAO.getCfBillCust(bu,cust);
System.out.println("BillCust List size "+billCustList.size());
for (CfBillCust cfBillCust : billCustList) {
int i = (int) projectMasterDAO.getProjectByCfBillCust(bu, cfBillCust);
billCustCount = i + billCustCount;
}
custCount = billCustCount;
cust.setNoOfProjects(custCount);
custLst.add(cust);
}
return custLst;
The data above is expected to be custCount=14, which is returned from the database calculated separately. The lists are declared globally and few in the snapshot.
Currently in RObocode I have a hashtable that has names as the keys and point2D objects as the values. One of the properties of these objects is the double lastSeen which is the time since the robot has been seen. Everytime I scan a robot, I would set this value to 0, this value also helps my radar become the oldest scanned radar.
public void onScannedRobot(ScannedRobotEvent e) {
String name;
EnemyInfo enemy = (EnemyInfo) enemies.get(name = e.getName());
// if the enemy is not already on the hashtable, puts it on
if (enemy == null) {
enemies.put(name, enemy = new EnemyInfo());
}
enemy.bearing = e.getBearing();
enemy.velocity = e.getVelocity();
enemy.heading = e.getHeading();
enemy.energy = e.getEnergy();
enemy.lastSeen = 0;
above is the code that upon scanning a robot, shoves it into the hashtable as an object and sets the object's lastseen property to 0;
I have made a method that increases the value (by 1) of every object's lastSeen variable by returning an enumeration of all object's lastSeen variables and adding one to each of them.
Method is below:
public static void advanceTime(EnemyInfo e) {
if (!enemies.isEmpty()) {
int i = 0;
Enumeration enum3 = enemies.elements();
do {
(e = (EnemyInfo) enum3.nextElement()).lastSeen = (e = (EnemyInfo) enum3
.nextElement()).lastSeen + 1;
i++;
System.out.println("Added one to.." + i);
} while (enum3.hasMoreElements());
}
}
However, I cannot call this method if there is nothing in the hashtable, which is why I put an if to stop the method from executing if there is nothing in the hashtable. Don't know the reason for this..
Any other method for doing this efficiently and effectively??
Storing Calculated Values
In a lot of cases, it is better to store the information to calculate a value rather than a calculated value itself. For instance, instead of storing a person's age (eg 20 years old), one would store the person's birthday (eg 5-5-95). If you store someone's age, you have to update it every year. If you store the birthday, you can simply calculate the age whenever you want.
Robocode
Back to the problem at hand. Instead of storing how old a piece of information is (age), store when the information was created (birthday). ScannedRobotEvent has a getTime() method that you can use to get the "birthday" of the scan event. Store this number. Then if you need to know how old the stored ScannedRobotEvent is, subtract this stored time from current time. This will bypass the need for an advanceTime method. To implement this update enemy.lastSeen to this:
public void onScannedRobot(ScannedRobotEvent e) {
String name;
EnemyInfo enemy = (EnemyInfo) enemies.get(name = e.getName());
// if the enemy is not already on the hashtable, puts it on
if (enemy == null) {
enemies.put(name, enemy = new EnemyInfo());
}
enemy.bearing = e.getBearing();
enemy.velocity = e.getVelocity();
enemy.heading = e.getHeading();
enemy.energy = e.getEnergy();
enemy.lastSeen = e.getTime();
Exception
There are some cases where you want to store a calculated value. GPA is a good example. GPA is a calculated value, but to calculate it you need to access every course someone has taken. In this case, if could be faster to store GPA and remember to update it whenever someone completes a new course.
I'm stuck on this one question I can't get my head around. I need to write a method to increase the number of "votes" of a specific "act" by one and then print out the updated vote count for that specific act. I'm working with ArrayLists here as well to point out.
Here is the logic you want to follow:
1: Iterate through ArrayList of 'acts'
2: Check for specified 'act'
3: If 'act' equals specified 'act', add one to your counter variable (votes++)
This is as much information as I'll give out without code to show what you've tried!
You could use a Map:
Class VoteCounter {
Map<Integer, Integer> actToCounterMap = new HashMap<Integer, Integer>();
public void raiseVoteForAct(int actId) {
if (actToCounterMap.contains(actId) {
int curVote = actToCounterMap.get(actId);
curVote++;
actToCounterMap.put(actId, curVote);
} else {
// init to 1
actToCounterMap.put(actId, 1);
}
}
}
You can print entire objects out in java, such as
System.out.println("Array list contains: " + arrayListName);
which will print the contents of the array without iterating through each value, although it may have odd syntax. As for the "acts", which I assume you mean objects, if you want to iterate the number of votes by one, you can have a class like this:
public class Act{
int votes = 0;
public void increaseVote(){
votes ++;
//You can also do votes = votes + 1, or votes += 1, but this is the fastest.
}
//While were at it, let's add a print method!
pubic void printValue(){
System.out.println("Votes for class " + this.getClass().getName() + " = " + votes + ".");
}
}
Finally, for a class with the arrayList:
class classWithTheArrayList {
private ArrayList<Act> list = new ArrayList<Act>();
public static void main(String[] args){
Act example1 = new Act();
list.add(example1);
//ArrayLists store a value but can't be changed
//when in the arraylist, so, after updating the value like this:
Act example2 = new Act();
example2.increaseVote();
//we need to replace the object with the updated one
replaceObject(example1, example2);
}
public void replaceObject(Object objToBeRemoved, Object objToReplaceWith){
list.add(objToReplaceWith, list.indexOf(objToBeRemoved); //Add object to the same position old object is at
list.remove(objToBeRemoved); //Remove old object
}
}
A slightly more efficient vote counter.
class VoteCounter<T> {
final Map<T, AtomicInteger> actToCounterMap = new HashMap<>();
public void raiseVoteForAct(T id) {
AtomicInteger ai = actToCounterMap.get(id);
if (ai == null)
actToCounterMap.put(id, ai = new AtmoicInteger());
ai.incrementAndGet();
}
}
Instead of AtomicInteger you can use new int[1] but it's relatively ugly. ;)
I explain what I am trying to do in comments above the parts in the method:
public int addPatron(String name) throws PatronException {
int i = 0;
//1. Iterate through a hashmap, and confirm the new name I am trying to add to the record doesn't already exist in the hashmap
for (Map.Entry<Integer, Patron> entry : patrons.entrySet()) {
Patron nameTest = entry.getValue();
//2. If the name I am trying to add already exists, we want to throw an exception saying as much.
if (nameTest.getName() == name) {
throw new PatronException ("This patron already exists");
//3. If the name is unique, we want to get the largest key value (customer number) already in the hash, an increment by one.
} else if (nameTest.getName() != name) {
Map.Entry<Integer,Patron> maxEntry = null;
for(Map.Entry<Integer, Patron> entryCheck : patrons.entrySet()) {
if (maxEntry == null || entryCheck.getKey() > maxEntry.getKey()) {
maxEntry = entryCheck;
i = maxEntry.getKey();
i++;
}
}
} else {
throw new PatronException("Something's not working!");
}
//4. If everything is ok up to this point, we want to us the name and the new customer id number, and use those to create a new Patron object, which then gets added to a hashmap for this class which contains all the patrons.
Patron newPatron = new Patron(name, i);
patrons.put(i, newPatron);
}
return i;
}
When I try and run a simple unit test that will fail if I successfully add the same name for addPatron twice in a row, the test fails.
try {
testLibrary.addPatron("Dude");
testLibrary.addPatron("Dude");
fail("This shouldn't have worked");
The test fails, telling me the addPatron method is able to use the same name twice.
#Jon Skeet:
My Patron class looks like this:
public class Patron {
//attributes
private String name = null;
private int cardNumber = 0;
//operations
public Patron (String name, int cardNumber){
this.name = name;
this.cardNumber = cardNumber;
}
public String getName(){
return name;
}
public int getCardNumber(){
return cardNumber;
}
}
As others have said, the use of == for comparing strings is almost certainly inappropriate. However, it shouldn't actually have caused a problem in your test case, as you're using the same constant string twice, so == should have worked. Of course, you should still fix the code to use equals.
It's also not clear what the Patron constructor or getName methods do - either of those could cause a problem (e.g. if they create a new copy of the string - that would cause your test to fail, but would also be unnecessary usually).
What's slightly more worrying to me is this comment:
// 3. If the name is unique, we want to get the largest key value (customer number)
// already in the hash, an increment by one.
This comment is within the main loop. So by that point we don't know that the name is unique - we only know that it doesn't match the name of the patron in this iteration.
Even more worrying - and I've only just noticed this - you perform the add within the iteration block too. It seems to me that you should have something more like this:
public int addPatron(String name) throws PatronException {
int maxKey = -1;
for (Map.Entry<Integer, Patron> entry : patrons.entrySet()) {
if (entry.getValue().getName().equals(name)) {
// TODO: Consider using IllegalArgumentException
throw new PatronException("This patron already exists");
}
maxKey = Math.max(maxKey, entry.getKey());
}
int newKey = maxKey + 1;
Patron newPatron = new Patron(name, newKey);
patrons.put(newKey, newPatron);
return newKey;
}
Additionally, it sounds like really you want a map from name to patron, possibly as well as the id to patron map.
You need to use equals to compare String objects in java, not ==. So replace:
if (nameTest.getName() == name) {
with:
if (nameTest.getName().equals(name)) {
Try to use
nameTest.getName().equals(name)
instead of
nameTest.getName() == name
because now you're comparing references and not the value of the String.
it's explained here
Took another look on your code
Well i took another look on your code and the problem is, that your HashMap is empty at the start of the Test. So the loop will never be runned ==> there will never bee a Patron added or an Exception thrown.
The cause of the problem is how you have used the compare operator ==.
When you use this operator against two objects, what you test is that variable point to the same reference.
To test two objects for value equality, you should use equals() method or compareTo if available.
For String class, invoke of equals is sufficient the check that the store same characters more.
What is equals method ?
To compare the values of Object
The problem is how you compare names.
I started down this path of implementing a simple search in an array for a hw assignment without knowing we could use ArrayList. I realized it had some bugs in it and figured I'd still try to know what my bug is before using ArrayList. I basically have a class where I can add, remove, or search from an array.
public class AcmeLoanManager
{
public void addLoan(Loan h)
{
int loanId = h.getLoanId();
loanArray[loanId - 1] = h;
}
public Loan[] getAllLoans()
{
return loanArray;
}
public Loan[] findLoans(Person p)
{
//Loan[] searchedLoanArray = new Loan[10]; // create new array to hold searched values
searchedLoanArray = this.getAllLoans(); // fill new array with all values
// Looks through only valid array values, and if Person p does not match using Person.equals()
// sets that value to null.
for (int i = 0; i < searchedLoanArray.length; i++) {
if (searchedLoanArray[i] != null) {
if (!(searchedLoanArray[i].getClient().equals(p))) {
searchedLoanArray[i] = null;
}
}
}
return searchedLoanArray;
}
public void removeLoan(int loanId)
{
loanArray[loanId - 1] = null;
}
private Loan[] loanArray = new Loan[10];
private Loan[] searchedLoanArray = new Loan[10]; // separate array to hold values returned from search
}
When testing this, I thought it worked, but I think I am overwriting my member variable after I do a search. I initially thought that I could create a new Loan[] in the method and return that, but that didn't seem to work. Then I thought I could have two arrays. One that would not change, and the other just for the searched values. But I think I am not understanding something, like shallow vs deep copying???....
The return value from getAllLoans is overwriting the searchedLoanArray reference, which means that both loanArray and searchedLoanArray are pointing at the same underlying array. Try making searchedLoanArray a local variable, and then use Arrays.copyOf. If you're trying not to use standard functions for your homework, manually create a new Loan array of the same size as loanArray, and then loop and copy the values over.
your searchloanarray and loanarray point to the same array. doing this
private Loan[] searchedLoanArray = new Loan[10]
does nothing as you never use that new Loan[10]
this is the key to your problem
searchedLoanArray = this.getAllLoans()
that just points searchedLoanArray at loanArray
You could rewrite it like this:
public Loan[] findLoans(Person p)
{
Loan[] allLoans = this.getAllLoans();
System.arraycopy(allLoans, searchedLoanArray, 0, 0, allLoans.length); // fill new array with all values
// remainder of method the same
}
But as it stands, the code still has some problems:
The maximum number of loans is fixed to the size of the array. You will avoid this problem when you switch to List<Loan>.
Using the id as an index means that your ids must be carefully generated. If IDs come from a database, you may find that the list tries to allocate a huge amount of memory to size itself to match the Id. You would be better using a Map, then the size of the map is based on the number of loans, rather than their IDs.
As the number of people and loans increase, the search time will also increase. You can reduce search time to a constant (irrespective of how many People) by using a Map>, which allows quick lookup of the loans associated just with that person.
Here's a version with these changes:
class AcmeLoanManager
{
public void addLoan(Loan l)
{
Person client = l.getClient();
List<Loan> loans = clientLoans.get(l);
if (loans==null)
{
loans = new ArrayList();
clientLoans.put(client, loans);
}
loans.add(l);
allLoans.put(l.getLoanId(), l);
}
public void removeLoan(int loanId)
{
Loan l = loans.remove(loanId);
clientLoans.remove(loan);
}
public Collection<Loan> getAllLoans()
{
return loans.values();
}
public List<Loan> findLoans(Person p)
{
List<Loan> loans = clientLoans.get(p);
if (loans==null)
loans = Collections.emptyList();
return loans;
}
private Map<Integer,Loan> allLoans = new HashMap<Integer,Loan>();
private Map<Person, List<Loan>> clientLoans = new HashMap<Person,List<Loan>>();
}
I hope this helps!
What I would do is loop through the values and reassign each value to the new variable. Alternatively, you could use "deep copy" technique as described here in Javaworld: http://www.javaworld.com/javaworld/javatips/jw-javatip76.html