0
votes

Sample data store in hashmap is as ,

e.g. {"client1-data1":data1,"client2-data2":data2,"client3-data3":data3,"client4-data4":data4,"client1-data2":data2,"client2-data1":data1,"client3-data4":data4,"client4-data3":data3}

every data can be get repeated for every other client , the key will be unique as client1-data1 combination value will get repeated but key will be unique.

Issue for handling multiple clients,

for every user , a different thread is created while making a connection so every thread will created a PrintWriter object which gets added to a Arraylist

List writers = new ArrayList();

Is their anyways where i can store the client_id along with the Printwriter object in the same array and pass the data to clients while filter the data with that client id as stored in the Hashmap in the above example.

PLease do reply/suggest

thanks, Praveen T

2
1. every data can be get repeated for every other client , the key will be unique as client1-data1 combination value will get repeated but key will be unique. - I didn't understand this. - ArjunShankar
2. for every user , a different thread is created - 'user' == 'client'? - ArjunShankar
3. Is this java? If you choose to edit your answer, do include the [java] tag. - ArjunShankar
@ArjunShankar yes user==client - praveen2609

2 Answers

0
votes

Printers and Clients are unique, so take a Set(no duplicate Id) or HashMap and manage something like this :

final HashMap<Writer, HashMap<Integer, List<String>>> writers = new HashMap<Writer, HashMap<Integer, List<String>>>();

Let us know if I've misunderstood something.

0
votes

You can use a collection which stores a pair of elements. A round about way of doing this is to use a Map.

Map<PrintWriter, String> writeAndClientId = new HashMap<>();

You can loop over these using

for(Map.Entry<PrintWriter, String> entry: writeAndClientId.entrySet())

If you want to be able to get one clientId at a time you can use

Map<String, PrintWriter> clientIdToWriter = new HashMap<>();

PrintWriter pw = clientIdToWriter.get(clientId);