Lewes
Lewes

Reputation: 123

Getting latest/newest HashMap entries

I have a HashMap<String, String>, and I'm inputting an entry into the HashMap everytime a player speaks for example. Bob492 says 'Hi'. and I will be inputting 'Bob492', and 'Hi' as its value.

Now I need a way to capture the last 5 message into a their own string, one string for each.

How would I do this?

EDIT: Thanks to a reply, it seems HashMap is not the best way to do this but a queue might be better. How would I achieve this, for example.

This is a dialog. Bob: Hey Bobby: Oh there you are! Bobby: I was looking for you Joseph: Yeah, I know I know. James: I wasn't talking to you! Joseph: Who cares?!?

And that will all be put in a queue, and I can easily fetch their username and message somehow (I don't know). Only can see 5 from that 6 messages, so Bob's message would be gone.

EDIT2: I want it similar to this: A minecraft server

Upvotes: 0

Views: 191

Answers (5)

Narendra Pathai
Narendra Pathai

Reputation: 41955

You can have a Map of CircularFifoBuffer of Apache Collections.

//maps username to last 5 messages
Map<String,CircularFifoBuffer> map = new HashMap<String,CircularFifoBuffer>();

//putting the values

if(map.get(username) == null){
    CircularFifoBuffer last5messages = new CircularFifoBuffer(5);
    last5messages.add(message);
    map.put(last5messages);
}else{
    //similar code
}

What is CircularFifoBuffer?

CircularFifoBuffer is a first in first out buffer with a fixed size that replaces its oldest element if full.

So that automatically stores the last 5 messages, removing the oldest message from the buffer.

EDIT:

As per my understanding from the comment:

Let me show you what I mean Bob: Hi Bob: I'm bored :l Joseph: Oh heyyy Josephary: Hey Joseph and bob Joseph: Oh how are you two?

class MessageEntry{
    String userName;
    String message;

    public MessageEntry(String userName, String message){
       //store
    }

    public String getUsername(){
        return userName;
    }

    public String getMessage(){
        return message;
    }

}

//just a buffer to store last 5 messages received
CircularFifoBuffer last5messages = new CircularFifoBuffer(5);


//for storing last 5 messages
last5messages.add(new MessageEntry(userName,message));


//for retrieving elements:
Object[] messages = last5Messages.toArray();
//gives you messages that you can use any way you want


for(Object message : messages){
   MessageEntry entry = (MessageEntry)message;

   String userName = entry.getUsername();
   String message = entry.getMessage();

}

Upvotes: 2

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136062

You can use LinkedHashMap, this

    LinkedHashMap m = new LinkedHashMap(16, 0.75f, true) {
        protected boolean removeEldestEntry(java.util.Map.Entry eldest) {
            return size() > 5;
        };
    };

makes sure that the map contains 5 most recent entries

    m.put("1", "1");
    m.put("2", "2");
    m.put("3", "3");
    m.put("4", "4");
    m.put("5", "5");
    m.put("6", "6");
    System.out.println(m);

output

{2=2, 3=3, 4=4, 5=5, 6=6}

Upvotes: 0

HashMap do not support such behavior. What you need to do is to create or find a class that will do the logic part of your code. The logic says that you need store in some place five messages. As simple MessageBuffer, should do the job for you.

class MessagesBuffer {

 private String[] recent;

 public MessagesBuffer(int size) { 
   this.recent = new String[size];
 }

 public void addMessage(String messsage) {

   for(int i = 1; i < recent.lenght; i++) {
     recent[i] = recent[i-1];
   }

   this.recent[0] = message;

 }

/*Other implementation details*/

} 

When you have a structure that do the logic, you can use Map to bind it with a person.

Map<User,MessageBuffer> recent = new HashMap<>();

Upvotes: 0

Filip
Filip

Reputation: 877

Is there any reason you are using a HashMap for this? As the HashMap will remove the last string the played says when a player speaks again. It sounds to me like you need a different datastructure to store your data, such as a fifo queue.

Upvotes: 0

mvlupan
mvlupan

Reputation: 3646

Unfortunately HashMap does NOT retain the order in witch the data was added.For you specific case you should use a LinkedHashMap because this kind of map is exactly like HashMap but keeps the order and you can iterate over it to get the last entries. But keep in mind that you cannot have multiple values for a single key so i dont think that you are looking for a map but for a Queue

Upvotes: 0

Related Questions