Issues deleting from multi-map, any ideas why its not working?
Hello everyone. I have a multi map and I'm trying to delete an element based on its Serial which is the KEY to the value, and the PacketID, which is part of the value.
For instance here are 2 entries in the MultiMap, having the same key, but different values.
Serial (key): 333
Values in Map:
[
Identifer: UPDATE 0
Serial: 333
PacketID: 1
,
Identifer: UPDATE 1
Serial: 333
PacketID: 2
]
Here's my Map class:
package tester;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.io.*;
//this class should store all the Messages or "Events" and
//you can access them based on their Serial key.
publicclass MessageDB{
//database to hold the information
//holds the Alerts/messages
Map<String, List><Message>> AlertMap;
//Constructor
MessageDB(){
AlertMap =new HashMap<String, List><Message>>();
}
//print, outputs the contents of the hashMap
publicvoid print(){
//want to print out the Key and all the Messages
//associated with that key
//print, outputs the contents of the hashMap
if(AlertMap.isEmpty())
System.out.println("Map Is Empty");
for (String key : AlertMap.keySet())
System.out.println("\n\nSerial (key): " + key
+"\nValues in Map: \n" + AlertMap.get(key));
}
void delete(Message msg)
{
for (String key : AlertMap.keySet())
{
if(msg.Serial.equals(key))
{
System.out.println("Successfully removed: " + key);
AlertMap.remove(key);
}
}
}
void delete(Message msg,int packetID)
{
for (String key : AlertMap.keySet())
{
if(msg.Serial.equals(key) && msg.PacketID == packetID)
{
System.out.println("Successfully removed: " + key +"with PacketID: " + packetID);
AlertMap.remove(key);
}
else
{
System.out.println("Coudln't find!");
}
}
}
}
Here's my delete function from that class I just posted:
void delete(Message msg,int packetID)
{
for (String key : AlertMap.keySet())
{
if(msg.Serial.equals(key) && msg.PacketID == packetID)
{
System.out.println("Successfully removed: " + key +"with PacketID: " + packetID);
AlertMap.remove(key);
}
else
{
System.out.println("Coudln't find!");
}
}
}
When I run the following test:
publicclass MainTest
{
publicstaticvoid main(String [] args)
{
MessageDB testDB =new MessageDB();
Message msg =null;
for(int i = 0; i < 2; i++)
{
msg =new Message();
msg.Serial ="333";
msg.Identifer ="UPDATE " + i;
testDB.add(msg);
}
testDB.delete(msg, 1);
testDB.print();
}
}
It prints out both values, without deleting any of them and prints out, couldn't find.Any ideas why?
[5926 byte] By [
lokiea] at [2007-11-27 10:16:16]

Thanks for the responce hunter, I forgot to show my message object code, everytime a new message object is create, it will increment the PacketID by 1. So in my code, the first message object will be PacketID =1, the other message will be 2.
Here's my new loop:
void delete(Message msg, int packetID)
{
for (String key : AlertMap.keySet())
{
if(msg.Serial.equals(key) && msg.PacketID == packetID)
{
System.out.println("Successfully removed: " + key + "with PacketID: " + packetID);
AlertMap.remove(key);
break;
}
else
System.out.println("Couldn't find!");
}
}
Here's where I set the packet ID:
package tester;
//this class will hold the Event/Message
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.Set;
import java.util.Iterator;
public class Message {
..
..
//these are datamembers that will be used during
//the sending of the message to the reciever
//PacketID should be incremented each time a new message is created
int SizeOfPacket;
String PacketType;
int PacketID;
static int nextPacketID = 1;
static synchronized int getNextId()
{
return nextPacketID++;
}
Message() {
Identifer = "";
...
...
//increment the message count
PacketID = getNextId();
}
....
....
}
lokiea at 2007-7-28 15:44:37 >

Alright I'll go and check it out and let you know what I find.
Here's the whole class including the add function:
package tester;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.io.*;
//this class should store all the Messages or "Events" and
//you can access them based on their Serial key.
public class MessageDB {
//database to hold the information
//holds the Alerts/messages
Map<String, List><Message>> AlertMap;
//Constructor
MessageDB() {
AlertMap = new HashMap<String, List><Message>>();
}
//print, outputs the contents of the hashMap
public void print() {
//want to print out the Key and all the Messages
//associated with that key
//print, outputs the contents of the hashMap
if(AlertMap.isEmpty())
System.out.println("Map Is Empty");
for (String key : AlertMap.keySet())
System.out.println("\n\nSerial (key): " + key
+ "\nValues in Map: \n" + AlertMap.get(key));
}
void delete(Message msg)
{
for (String key : AlertMap.keySet())
{
if(msg.Serial.equals(key))
{
System.out.println("Successfully removed: " + key);
AlertMap.remove(key);
}
}
}
void delete(Message msg, int packetID)
{
for (String key : AlertMap.keySet())
{
if(msg.Serial.equals(key) && msg.PacketID == packetID)
{
System.out.println("Successfully removed: " + key + "with PacketID: " + packetID);
AlertMap.remove(key);
break;
}
else
System.out.println("Couldn't find!");
}
}
void add(Message msg) {
//getting the position of the List by EntityID if avaiable
List<Message> AlertList = AlertMap.get(msg.Serial);
//checking to see if there is a unique Key already in the Map.
if (AlertList == null) {
//if there isnt a key in the map, add a new key, and a new List mapping
//to the key EntityID;
AlertList = new ArrayList<Message>();
AlertMap.put(msg.Serial, AlertList);
AlertList.add(msg);
} else {
//adding message to List
AlertList.add(msg);
}
}
}
lokiea at 2007-7-28 15:44:37 >
