Reputation: 1274
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.
Upvotes: 1
Views: 154
Reputation: 312
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;
}
}
Upvotes: 1