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]
# 1

for(int i = 0; i < 2; i++)

{

msg = new Message();

msg.Serial = "333";

msg.Identifer = "UPDATE " + i;

testDB.add(msg);

}

testDB.delete(msg, 1);

You're telling it to delete the msg whose Serial equals the key in the map, and whose PacketID equals 1. Since I don't see you setting the PacketID of the msg to 1 before you add it to the map, it doesn't look like your comparison will ever work.

Also, you print "Coudln't find!" for each key that doesn't match, so if you want to only see that if no match was found in the entire map, you need to fix that.

hunter9000a at 2007-7-28 15:44:37 > top of Java-index,Java Essentials,Java Programming...
# 2

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 > top of Java-index,Java Essentials,Java Programming...
# 3

I'm also not seeing your MessageDB.add(Message) method. How do you actually put the Messages into your Map?

You should go through and add println()s in strategic locations to track what's actually happening in the code when you do comparisons and such. That'll be a big help in finding where it's not doing what you want.

hunter9000a at 2007-7-28 15:44:37 > top of Java-index,Java Essentials,Java Programming...
# 4

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 > top of Java-index,Java Essentials,Java Programming...
# 5

Thanks Hunter i got it!

I was really off.

boolean delete(String Serial, int PacketID)

{

if(AlertMap.isEmpty())

System.out.println("\nDatabase is empty!");

List<Message> AlertList = AlertMap.get(Serial);

System.out.println("Getting the Messages with Serial: " + Serial);

Iterator it = AlertList.iterator();

while(it.hasNext())

{

//System.out.println("Element: " + it.next());

Object o = it.next();

Message m = (Message)o;

if(m.PacketID == PacketID)

{

it.remove();

System.out.println("Removed Messge with Packet ID: " + PacketID);

return true;

}

}

System.out.println("Couldn't find the message with Packet ID: " + PacketID);

return false;

}

lokiea at 2007-7-28 15:44:37 > top of Java-index,Java Essentials,Java Programming...