Aaron
Aaron

Reputation: 47

Retrieve single value from ArrayList

i am facing the problem how to retrieve the value from ArrayList. I pass in the value with this code to remove duplicate:

Set<List<String>> allTCP=new LinkedHashSet<List<String>>();
     for(int x=0; x < TCPSourceIP.size();x++){
         allTCP.add(Arrays.asList(new String[]{TCPSourceIP.get(x),TCPSourcePort.get(x),TCPDestIP.get(x),TCPDestPort.get(x)}));
     }

But now the problem i facing is i do not know how to retrieve the value. The value i pass in is 10.1.1.1, 123, 10.1.1.2, 234 If i use System.out.println(allTCP), then i cannot use each of the value inside the List.I need to get the single value from the array to do proper output such as, The TCP Source IP is 10.1.1.1 with port 123 to Destination IP 10.1.1.2 with port 234. Previously i use this code to do output but it did not remove duplicate.

for(int x=0; x < TCPSourceIP.size(); x++){
       jTextArea1.append(x+1+")IP " + TCPSourceIP.get(x) +" is sending packet using TCP Port "+ 
         TCPSrcPort.get(x) + " to IP " + TCPDestIP.get(x) + "(" + TCPDestPort.get(x) +")" + 
         " which is non-standard port\n");

Can any one give me suggestion on how to retrieve the value from the ArrayList in order for me to do a proper output. Help would be appreciated.

Upvotes: 0

Views: 654

Answers (1)

Axel Amthor
Axel Amthor

Reputation: 11096

In cases like that I prefer to have the values encapsulated in to an object with getters/setters and put instances of those to the lists / hashes:

class Route {
   private String sourceIp;
   private Long sourcePort;
   private String destIp;
   private Long destPort;

   public Route(Sting src, Long sport, String dest, Long dport) {
        setSourceIp(src);
        ...
        ... etc.

And then

ArrayList<Route> allTcp = new ArrayList<Route>();
...
allTcp.add(new Route(src, sport, dst, dport) );

This concept would then enable an easy way to find any source, destination, port etc. by putting the complex comparison methods inside class Route. Any changes to the inner set of Route doesn't interfere with the caller(s).

Upvotes: 4

Related Questions