How to store customers on a list

I start the server, and I have an arraylist of type Client. When the client connects through ServerSocket.accept () I pass the new Socket to the arraylists constructor. This is all inside the constructor

this.add(new Client(Socket client)); 

My problem is that the client disconnects, it closes the Socket, but it does not delete its place in the arraylist and does not shift everything down. Thus, the arraist is constantly growing.

What can I do / use to fix this problem?

Sometimes I run commands that will be executed on all clients, so I store clients in arraylist.

Is there a better alternative for storing clients on a server?

Update 1

Classes are in the initial stages. Very little has been implemented. So far, the HashMap option proposed in the answer is best for me. thank you for your responses

+5
source share
4 answers

An interesting problem.

You should use a hash map here. Add a client with the object as the value and use some key. Whenever you unplug it, remove it from the card.

Good question could I, what should be the key? it can be a reference to an object (it depends on your choice) or everything that is unique in relation to the clientโ€™s object (there must be something, if not, you can easily generate it).

 Map<Integer,Client> clientMap = new HashMap<Integer,Client>(); 
+3
source

You must remove the client from the ArrayList, the rest of the elements in the list will automatically move up the list.

 //java.util.ArrayList.remove(Object) shifts any subsequent elements // to the left (subtracts one from their indices). 

Let's say if the client is client A and ArrayList is ArrayListA, then you should apply

 ArrayListA.remove(ClientA); 

But better use a HashMap to store customer information, as indicated by Danial Sandelo's answer.

+2
source

If the client is your own class, you can try the following:

 public class Client { public final List<Client> clients; public Client(List<Client> clients, Socket socketClient) { this.clients = clients; clients.add(this); } public void disconnect() { clients.remove(this); }} List<Client> clients = new ArrayList<Client>(); new Client(clients, new Socket()); new Client(clients, new Socket()); new Client(clients, new Socket()); 
0
source

Many assumptions have been made, for example, I assume that you have a disconnect() method in your Client class. Please provide more information if you want a more accurate solution.

One way to do this could be by passing a callback to your client so that it will be removed from the ArrayList when it has been done. Very crude implementation:

 public class Callback { private ArrayList<Client> clients; public Callback(ArrayList<Client> clients) { this.clients = clients; } public void remove(Client client) { clients.remove(client); } } 

Then pass the Callback when you instantiate the Client :

 Callback callback = new Callback(list); list.add(new Client(socket, callback)); 

Then call the remove() method of the callback. If Client has a disconnect() method, then inside it you can do

 public void disconnect() { // bla bla callback.remove(this); } 

Thus, Client can clean up after itself when it needs to :)

0
source

All Articles