1

I have data like in following logical format:

FolderID-1
    FileID-1
    FileID-2    
FolderID-2
    FileID-3
    FileID-4
    FileID-5
    FileID-6    
FolderID-3
    FileID-7
    FileID-8
    FileID-9
    FileID-10

I have list of FileID object which have FoldeID I need to update one field in this list and need to pass to this list in other method.

I need to get FileID object based on fileid & folderid in that method. To achieve the same I know two way
1 HashMap<folderid,List<FileID>> OR 2 HashMap<folderid, HashMap<fileid ,FileID>

Is there any other efficient way to do the same? Thanks for looking here.

6
  • 1
    I'm not sure if this fits your purpose, but what if you create composite Key {folderID, fileID} and use it as a HashMap key with corresponding File value? Just pay attention on equals and hashCode methods of Key class Commented Jan 10, 2017 at 10:16
  • In my approach there will be unique folderId and fileID So, I am thinking to keep as key {folderID + fileID} (sum of both id) Commented Jan 10, 2017 at 10:20
  • Yes, in case if folderId + fileID is unique this seems to be perfect match for the Key Commented Jan 10, 2017 at 10:22
  • 1
    "sum of both id" is not guaranteed to be unique. Folder 7 plus file 6 will be 13. Folder 8 plus file 5 is equal to 13. Commented Jan 10, 2017 at 19:43
  • 1
    A more efficient key would be to use a long (64 bit) integer, with the folder id (32 bit integer) in the high 32 bits, and the file id (another 32 bit integer) in the low 32 bits. You can create it with something like long key = ((long)folderId << 32) + fileId; This assumes that you won't have more than 4 billion folders, and no folder will have more than 4 billion files. Commented Jan 11, 2017 at 5:33

1 Answer 1

1

Hi I read your cmnt you can go ahead with the string key (using fileid and folder id) that will work for you. But your data comes with a nice logical structure . file id and folder id will be unique as well as a single folder will contain file having the file id is consecutive. So, My approach to solve this entirely depends on this structure.

I made two Class FileIdObj and FolderIdObj thats contains the data redarding the file and folder respectively.

public static void fileIdBasedOnFileIdAndFolderId( List<FileIdObj> fileList)
    {
        Map<Integer,FolderIdObj> folderIdMap=new HashMap<Integer,FolderIdObj>();
        Map<Integer,FileIdObj> fileIdMap=new HashMap<Integer,FileIdObj>();

        for(int i=0;i<fileList.size();i++)
        {
            FileIdObj file=fileList.get(i);
            fileIdMap.put(file.getFileId(), file);

            int folderId=file.getFolderId();
            FolderIdObj folder=new FolderIdObj();

            if(folderIdMap.containsKey(folderId))
            {
                folder=folderIdMap.get(folderId);
                folder.setEndFileId(file.getFileId());
            }else
            {
                folder.setFolderId(folderId);
                folder.setStartFileId(file.getFileId());
                folder.setEndFileId(file.getFileId());
            }

            folderIdMap.put(folderId, folder);
        }

        Set<Integer> set=folderIdMap.keySet();
        Iterator it=set.iterator();
        while(it.hasNext())
        {
            FolderIdObj obj=folderIdMap.get(it.next());
            System.out.println("folder id: "+obj.getFolderId()+" start fileId: "+obj.getStartFileId()+
                    " end fileId: "+obj.getEndFileId());
        }

        System.out.println();
        System.out.println();

        set=fileIdMap.keySet();
        it=set.iterator();
        while(it.hasNext())
        {
            FileIdObj obj=fileIdMap.get(it.next());
            System.out.println("file id: "+obj.getFileId()+" folder id:"+obj.getFolderId());
        }
    }

the list on the argument contains the file object only. Please see below for the details of the two class.

public class FileIdObj {

    private int folderId;
    private int fileId;

    public int getFolderId() {
        return folderId;
    }
    public void setFolderId(int folderId) {
        this.folderId = folderId;
    }
    public int getFileId() {
        return fileId;
    }
    public void setFileId(int fileId) {
        this.fileId = fileId;
    }

}
public class FolderIdObj {

    private int folderId;
    private int startFileId;
    private int endFileId;

    public int getFolderId() {
        return folderId;
    }
    public void setFolderId(int folderId) {
        this.folderId = folderId;
    }
    public int getStartFileId() {
        return startFileId;
    }
    public void setStartFileId(int startFileId) {
        this.startFileId = startFileId;
    }
    public int getEndFileId() {
        return endFileId;
    }
    public void setEndFileId(int endFileId) {
        this.endFileId = endFileId;
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.