2 Replies Latest reply on Nov 30, 2008 8:59 PM by 807589

    What shoud I use? (HashMap, TreeMap, ArrayList, something else..?)


      I have an assignment to make a P2P program in java.

      Here's a scenario of what could happen:
      A user requests an index of all files that are shared on lan > every other client connected sends his index (all clients do this simultaneously).

      My question is: In which form should all clients send their index (ArrayList? HashMap?) , and when received, how should they be all placed together? (it would be nice if they were sorted by filename: TreeMap?)

      Extra info: There should be 3 elements associated with the filename: filesize, MD5 checksum(no duplicates), ip address, so that a user can select a filename, click download, and the program knows the ip address and md5 so it's ready to be downloaded.

      Final question: what is the best way to represent this graphically? (I'd rather not have the checksum and ip address visible to the user)

      Greetz Daan
        • 1. Re: What shoud I use? (HashMap, TreeMap, ArrayList, something else..?)
          I'd send all the information usually associated with a file in a serialisable Object, like
          public class FileInformation implements java.io.Serializable {
            private static final long serialVersionUID = 1L;
            private String name;
            private long size;
            private Date lastModified;
            private byte[] md5hash;
            // getters and setters
            public boolean equals(Object o) {
              return ((FileInformation) o).name.equals(name); // throws NullPointerException if o is null, ClassCastException if it's not FileInformation
            public int hashCode() {
              return name.hashCode(); // or maybe return the first few bytes of md5 as an int, md5hash[0] * 256 + md5hash[1]?
          , a list of them being in an ArrayList and not necessarily sorted before sending it to the other client.

          On the client side, a FileInformation object could be associated with the IP it's from in an ArrayList of objects like
          public class FileOriginInformation {
            private FileInformation info;
            private String ip;
          . Then it becomes easy for other clients to send the list, with ObjectOutputStream, and for clients to display everything at once.

          I would display the checksum, personally, but the IP could be hidden. eDonkey displays everything in a sortable list-view control in detail mode, that would be a JTable in Java.

          • 2. Re: What shoud I use? (HashMap, TreeMap, ArrayList, something else..?)
            Thanks a lot!
            Exactly what I was looking for :)

            Greetz Daan