HashMap 数据未将新数据附加到 ArrayList [英] HashMap data not appending new data into ArrayList

查看:54
本文介绍了HashMap 数据未将新数据附加到 ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码从 Applet 客户端连接 java 套接字.我在下面代码中的 addNewClient() 函数中发生的每个新连接中存储客户端的 IP 地址和一些随机数.我将此信息存储在 HashMap 中.我在此 HashMapArrayList 中添加了更多客户端信息.

I am using the following code to connect with java socket from an Applet client. I store Client's IP Address and some random number in every NEW connection happening in addNewClient() function in the below code. I store this info in HashMap. I add more client info in the ArrayList of this HashMap.

如果ArrayList中已经有一些客户端信息,我需要通读它.我正在使用 Iterator 在下面的 SocketConnection 类中尝试.

If there is already some client info in ArrayList, I need to read through it. I am trying that in SocketConnection class below using Iterator.

我看到的问题是,我将一些 3 个客户端信息添加到 ArrayList.但是,当我使用 Iterator 通读它时,它只能获取最后添加的客户端信息,而其他 KEYS 只是变空了.但是,与此同时,它正确地将 ArrayList 大小设置为 3

The problem I see is, I am adding some 3 client info into the ArrayList. But, when i read through it using Iterator, it can get only last added Client info, and other KEYS are just getting empty. But, at the same time, its giving the ArrayList size correctly as 3

请高手参考我下面的完整代码,并告诉我那里可能存在什么问题?

Could some experts please refer my below complete code, and advise me what could be the problem in there?

public class DataSharingSocketListner {
            public static void main(String[] args) {

                System.out.println("client trying to connect before thread creation");

                Thread thr = new Thread(new SocketThread());
                thr.start();
            }
        }

        class SocketThread implements Runnable {

            HashMap<String, ClientInfo> clientInfo = new HashMap<String, ClientInfo>();

            ArrayList<HashMap<String, ClientInfo>> myList = new ArrayList<HashMap<String, ClientInfo>>();

            @Override
            public void run() {
                try {
                    System.out.println("client trying to connect after thread creation");

                    ServerSocket server = new ServerSocket(8080);
                    while (true) {

                        SocketConnection client = new SocketConnection(server.accept(), clientInfo, myList);
                        client.start();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

        class SocketConnection extends Thread {
            InputStream input;
            PrintWriter output;
            Socket socket;
            ObjectOutputStream out = null;
            OutputStream clientOutput;
            Scanner scannerObj;
            HashMap<String, byte[]> hm;
            InetAddress addr;

            HashMap<String, ClientInfo> clientinfo;

            ArrayList<HashMap<String, ClientInfo>> clientList;

            public SocketConnection(Socket socket, HashMap<String, ClientInfo> clientInfo, ArrayList<HashMap<String, ClientInfo>> myList) {

                super("Thread 1");

                this.socket = socket;
                //this.hm = dataHashMap;
                this.clientinfo = clientInfo;
                this.clientList = myList;

                try {

    // IT IS PRINTING TOTAL SIZE 3 SUCCESSFULLY HERE
                    int totalClientList = clientList.size();
                    System.out.println("totalClientList: " + totalClientList);

                    if ( totalClientList>0 )
                    {
                        for (int i=0; i<totalClientList; i++)
                        {
                            System.out.println("client list reading " + i);

                            HashMap<String, ClientInfo> tmpData = (HashMap<String, ClientInfo>) clientList.get(i);

  // IT IS GETTING ONLY THE LAST KEY, OTHER KEYS ARE SHOWING EMPTY
                            Set<String> key = tmpData.keySet();
                            Iterator it = key.iterator();

                            while (it.hasNext()) {
                                System.out.println("hasNexthasNext");
                                String hmKey = (String)it.next();
                                ClientInfo hmData = (ClientInfo) tmpData.get(hmKey);

                                System.out.println("Key: "+hmKey +" & Data: "+hmData.getRandomNo());
                                it.remove(); // avoids a ConcurrentModificationException
                            }
                        }
                        // TO ADD NEW CLIENT EVERY TIME
                        addNewClient();
                    }
                    else {
                        System.out.println("Client List shows empty");

                        // TO ADD NEW CLIENT EVERY TIME
                        addNewClient();
                    }

                    // Not used yet, will be used
                    input = socket.getInputStream();
                    scannerObj = new Scanner(socket.getInputStream());
                    clientOutput = socket.getOutputStream();


                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            public int genRandomNumber() {
                Random r = new Random( System.currentTimeMillis() );
                return 10000 + r.nextInt(20000);
            }
            String getLocalIP () {
                InetAddress inetAddress = null;
                String ipAddress = null;
                try {
                    inetAddress = InetAddress.getLocalHost();
                    ipAddress = inetAddress.getHostAddress();
                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("ipAddress : " + ipAddress);

                return ipAddress;
            }
            void addNewClient () {

                String ipAddress = getLocalIP();

                if ( ipAddress!=null )
                {
                    ClientInfo clientobj = new ClientInfo();

                    clientobj.setIPAdd(ipAddress);
                    int randno =  genRandomNumber();
                    System.out.println("genRandomNumber() : " + randno);
                    clientobj.setRandomNo(randno);

                    String key = String.valueOf(randno);

                    clientinfo.put(key, clientobj);

                    clientList.add(clientinfo);
                }
            }

            @Override
            public void run() {

                System.out.println("Going to Read client data");

                //do something
                {
                    String hostIP = addr.getHostAddress() ;
                    System.out.println("hostIP: " + hostIP);

                    //do something
                }
            }



        }

        class ClientInfo {

            private String IPAddress;
            private long RandomNumber;
            private byte[] data;

            public static void main(String []args) {
                System.out.println("Client info Main");
            }

            //Setter
            void setIPAdd (String ip) {
                System.out.println("setIPAdd called");
                IPAddress = ip;
            }
            void setRandomNo (long randomno) {
                RandomNumber = randomno;
            }
            void setImageData (byte[] imgData) {
                data = imgData;
            }

            //Getter 
            String getIPAdd () {
                return IPAddress;
            }
            long getRandomNo () {
                return RandomNumber;
            }
            byte[] getImageData () {
                return data;
            }

        }

更新:根据 Amrish 的建议,更改了以下代码,解决了问题.

UPDATE: As per Amrish suggestion, changed the following code, it solved the issue.

    int totalClientList = clientList.size();
                        System.out.println("totalClientList: " + totalClientList);

                        if ( totalClientList>0 )
                        {
                            for (int i=0; i<totalClientList; i++)
                            {
                                System.out.println("client list reading " + i);

                                HashMap<String, ClientInfo> tmpData = (HashMap<String, ClientInfo>) clientList.get(i);
                                Set<String> key = tmpData.keySet();
                                System.out.println("key: " + key);
        }
                            addNewClient();
                        }
                        else {
                            System.out.println("Client List shows empty");
                            addNewClient();
                        }

    void addNewClient () {

                String ipAddress = getLocalIP(); 

                if ( ipAddress!=null )
                {
// CREATE NEW OBJECT EVERY TIME WHEN STORING
                    HashMap<String, ClientInfo> clientInfo = new HashMap<String, ClientInfo>();
                    ClientInfo clientobj = new ClientInfo();

                    //System.out.println("Test log 1" + clientobj);
                    clientobj.setIPAdd(ipAddress);
                    // System.out.println("Test log 2");
                     int randno =  genRandomNumber();
                     System.out.println("genRandomNumber() : " + randno);
                     clientobj.setRandomNo(randno);

                    String key = String.valueOf(randno);
                    //System.out.println("key: " + key);

                    clientInfo.put(key, clientobj);

                    clientList.add(clientInfo);
                }
            }

推荐答案

你的 Arraylist 有 3 个 hashmap,但每个 hashmap 只有一个对象.因此,您得到的大小为 3,但在遍历哈希图时只返回一个对象.

Your Arraylist has 3 hashmap but each hashmap has only one object. Hence , you are getting size as 3 but only one object is returned when you iterate over the hashmap.

这篇关于HashMap 数据未将新数据附加到 ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆