[Solved] Java remove dupplicate attribute in List


static class Message {
    String message;
    long time;
    
    public Message(String message, long time) {
        this.message = message;
        this.time = time;
    }
}

public static void putLatestMessage(Map<String, Message> messageMap, Message message) {
    if (messageMap.containsKey(message.message) && messageMap.get(message.message).time >= message.time) {
        return;
    } else {
        messageMap.put(message.message, message);
    }
}

public static void main(String[] args) {
    Map<String, Message> messageMap = new HashMap<>();
    putLatestMessage(messageMap, new Message("A", 1));
    putLatestMessage(messageMap, new Message("B", 2));
    putLatestMessage(messageMap, new Message("A", 2));
    putLatestMessage(messageMap, new Message("C", 2));
    putLatestMessage(messageMap, new Message("D", 1));
}

This will solve your issue.

P.S – plz go through basic data structures available in Java and their use cases.

solved Java remove dupplicate attribute in List