简体   繁体   中英

how to use Hashmap to filter data in multithreading to serve multiple clients

Sample data store in hashmap is as ,

eg {"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

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM