How can I Add of same type of file names as key with total memory as value in a Map of Java?












-2















suppose there are different files of different types. for example razi.mp3 of size 10mb, test.txt of 5 mb, song.mp3 of 20mb, file.js of 100mb, mark.txt of 10mb, zoom.js of 50mb. I need to print file type with total size. in this case I want to print:
mp3 : 30mb
txt : 15mb
js : 150mb
Can I solve this problem by using map in java or any other solution?



Basically I want to use map with comparator interface. Any other ways are most welcome.



Any type of help and effort is very much appreciable. Thanks in advance for every one who are interesting in this problem.










share|improve this question























  • "I want to use map with comparator interfac" - Why? If you compare entries, do you want to sort them? And how do you want to map the files? What do want to achive?

    – LuCio
    Nov 16 '18 at 7:50











  • Actually I was trying to compare same file type and if equal then adding the memories, hence I was using comparator. Any way thanks for responding.

    – Razi Ahmad
    Nov 16 '18 at 11:14
















-2















suppose there are different files of different types. for example razi.mp3 of size 10mb, test.txt of 5 mb, song.mp3 of 20mb, file.js of 100mb, mark.txt of 10mb, zoom.js of 50mb. I need to print file type with total size. in this case I want to print:
mp3 : 30mb
txt : 15mb
js : 150mb
Can I solve this problem by using map in java or any other solution?



Basically I want to use map with comparator interface. Any other ways are most welcome.



Any type of help and effort is very much appreciable. Thanks in advance for every one who are interesting in this problem.










share|improve this question























  • "I want to use map with comparator interfac" - Why? If you compare entries, do you want to sort them? And how do you want to map the files? What do want to achive?

    – LuCio
    Nov 16 '18 at 7:50











  • Actually I was trying to compare same file type and if equal then adding the memories, hence I was using comparator. Any way thanks for responding.

    – Razi Ahmad
    Nov 16 '18 at 11:14














-2












-2








-2








suppose there are different files of different types. for example razi.mp3 of size 10mb, test.txt of 5 mb, song.mp3 of 20mb, file.js of 100mb, mark.txt of 10mb, zoom.js of 50mb. I need to print file type with total size. in this case I want to print:
mp3 : 30mb
txt : 15mb
js : 150mb
Can I solve this problem by using map in java or any other solution?



Basically I want to use map with comparator interface. Any other ways are most welcome.



Any type of help and effort is very much appreciable. Thanks in advance for every one who are interesting in this problem.










share|improve this question














suppose there are different files of different types. for example razi.mp3 of size 10mb, test.txt of 5 mb, song.mp3 of 20mb, file.js of 100mb, mark.txt of 10mb, zoom.js of 50mb. I need to print file type with total size. in this case I want to print:
mp3 : 30mb
txt : 15mb
js : 150mb
Can I solve this problem by using map in java or any other solution?



Basically I want to use map with comparator interface. Any other ways are most welcome.



Any type of help and effort is very much appreciable. Thanks in advance for every one who are interesting in this problem.







java hashmap






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 6:05









Razi AhmadRazi Ahmad

65




65













  • "I want to use map with comparator interfac" - Why? If you compare entries, do you want to sort them? And how do you want to map the files? What do want to achive?

    – LuCio
    Nov 16 '18 at 7:50











  • Actually I was trying to compare same file type and if equal then adding the memories, hence I was using comparator. Any way thanks for responding.

    – Razi Ahmad
    Nov 16 '18 at 11:14



















  • "I want to use map with comparator interfac" - Why? If you compare entries, do you want to sort them? And how do you want to map the files? What do want to achive?

    – LuCio
    Nov 16 '18 at 7:50











  • Actually I was trying to compare same file type and if equal then adding the memories, hence I was using comparator. Any way thanks for responding.

    – Razi Ahmad
    Nov 16 '18 at 11:14

















"I want to use map with comparator interfac" - Why? If you compare entries, do you want to sort them? And how do you want to map the files? What do want to achive?

– LuCio
Nov 16 '18 at 7:50





"I want to use map with comparator interfac" - Why? If you compare entries, do you want to sort them? And how do you want to map the files? What do want to achive?

– LuCio
Nov 16 '18 at 7:50













Actually I was trying to compare same file type and if equal then adding the memories, hence I was using comparator. Any way thanks for responding.

– Razi Ahmad
Nov 16 '18 at 11:14





Actually I was trying to compare same file type and if equal then adding the memories, hence I was using comparator. Any way thanks for responding.

– Razi Ahmad
Nov 16 '18 at 11:14












3 Answers
3






active

oldest

votes


















1














Yes. you can solve this problem via map.



  public class FileMeta {

String name;
String extension;
int size;

public FileMeta(String name, String extension, int size) {
this.name = name;
this.extension = extension;
this.size = size;
}

public String getName() {
return name;
}

public FileMeta setName(String name) {
this.name = name;
return this;
}

public String getExtension() {
return extension;
}

public FileMeta setExtension(String extension) {
this.extension = extension;
return this;
}

public int getSize() {
return size;
}

public FileMeta setSize(int size) {
this.size = size;
return this;
}

}

public static void main(String args) {
Map<String, List<FileMeta>> map = new ConcurrentHashMap<>();
FileMeta obj1 = new FileMeta("razi", "mp3", 10);
FileMeta obj2 = new FileMeta("test", "txt", 5);
FileMeta obj3 = new FileMeta("song", "mp3", 20);
FileMeta obj4 = new FileMeta("file", "js", 100);
FileMeta obj5 = new FileMeta("mark", "txt", 10);
FileMeta obj6 = new FileMeta("zoom", "js", 50);

putinMap(map, obj1);
putinMap(map, obj2);
putinMap(map, obj3);
putinMap(map, obj4);
putinMap(map, obj5);
putinMap(map, obj6);

map.entrySet().stream().forEach(entry-> {
int size = entry.getValue().stream().mapToInt(FileMeta::getSize).sum();
System.out.println(entry.getKey() + " : " + size + " mb");
});


}

private static void putinMap(Map<String, List<FileMeta>> map, FileMeta obj) {
if (map.containsKey(obj.getExtension())) {
map.get(obj.getExtension()).add(obj);
} else {
ArrayList<FileMeta> list = new ArrayList<>();
list.add(obj);
map.put(obj.getExtension(), list);
}
}


in which key is the type of file Like mp3 or txt and value is the consist of some object which contain name and file size and extension.



you have to iterate on each row and sum of each filesize.






share|improve this answer


























  • Please don't use a wrapper when a primitive would be more appropriate. Unless size can be null use a long.

    – Peter Lawrey
    Nov 16 '18 at 7:44











  • @Khalid shah can u please elaborate more? How can I add file size for each similar extension. I am very beginner in map.

    – Razi Ahmad
    Nov 16 '18 at 12:18











  • @RaziAhmad see updated answer.

    – Khalid Shah
    Nov 16 '18 at 12:54











  • @Khalid shah thanks bro for this help

    – Razi Ahmad
    Nov 16 '18 at 14:34



















0














The following works:



import java.util.*;


public class Main {



static class FileInfo {
private String name;
private int sizeInMb;

public FileInfo(String name, int sizeInMb) {
this.name = name;
this.sizeInMb = sizeInMb;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getSizeInMb() {
return sizeInMb;
}

public void setSizeInMb(int sizeInMb) {
this.sizeInMb = sizeInMb;
}
}

public static void main(String args) {
List<FileInfo> fileInfos = new ArrayList<>();
fileInfos.add(new FileInfo("razi.mp3", 10));
fileInfos.add(new FileInfo("test.txt", 5));
fileInfos.add(new FileInfo("song.mp3", 20));
fileInfos.add(new FileInfo("file.js", 100));
fileInfos.add(new FileInfo("mark.txt", 10));
fileInfos.add(new FileInfo("zoom.js", 50));

Map<String, Integer> sizes = new HashMap<>();

//initiatise map
fileInfos.forEach(fileInfo -> {
final String extension = fileInfo.getName().substring(fileInfo.getName().lastIndexOf("."));
sizes.put(extension, 0);
});

System.out.println(sizes);

//put summaries
fileInfos.stream().forEach(fileInfo -> {
//get extension
String extension = fileInfo.getName().substring(fileInfo.getName().lastIndexOf("."));
sizes.put(extension, sizes.get(extension) + fileInfo.getSizeInMb());
});

System.out.println(sizes);
}


}






share|improve this answer
























  • Thanks @Alfred Samanga its working for me

    – Razi Ahmad
    Nov 16 '18 at 14:32





















0














you can create a FileObject class with instance members as name ,inputstream , size and type.



@Getter
@Setter
public class FileObject {
private String name;
private InputStream inputStream;
private String type;
private Long size;

public FileObject(String name, InputStream inputStream, String type, Long size) {
this.name = name;
this.inputStream = inputStream;
this.type = type;
this.size = size;
}
}


Create array of FilObject with you data.



Then you can create a Map with type attribute as key (corrspoding inital value as 0 )and for each Fileobject add the size value to corresponding key in map.



Map<String, FileObject> fileObjectMap = new HashMap<>();


After that you will be able to print your required output.

Hope it helps.






share|improve this answer





















  • 1





    Please don't use a wrapper when a primitive would be more appropriate. Unless size can be null use a long.

    – Peter Lawrey
    Nov 16 '18 at 7:44











  • @Krishna Murari can u please elaborate more? How can I add file size for each similar extension. I am very beginner in map.

    – Razi Ahmad
    Nov 16 '18 at 12:19











  • @RaziAhmad i have made some changes ,look into it

    – krishna Murari
    Nov 17 '18 at 18:08











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332310%2fhow-can-i-add-of-same-type-of-file-names-as-key-with-total-memory-as-value-in-a%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Yes. you can solve this problem via map.



  public class FileMeta {

String name;
String extension;
int size;

public FileMeta(String name, String extension, int size) {
this.name = name;
this.extension = extension;
this.size = size;
}

public String getName() {
return name;
}

public FileMeta setName(String name) {
this.name = name;
return this;
}

public String getExtension() {
return extension;
}

public FileMeta setExtension(String extension) {
this.extension = extension;
return this;
}

public int getSize() {
return size;
}

public FileMeta setSize(int size) {
this.size = size;
return this;
}

}

public static void main(String args) {
Map<String, List<FileMeta>> map = new ConcurrentHashMap<>();
FileMeta obj1 = new FileMeta("razi", "mp3", 10);
FileMeta obj2 = new FileMeta("test", "txt", 5);
FileMeta obj3 = new FileMeta("song", "mp3", 20);
FileMeta obj4 = new FileMeta("file", "js", 100);
FileMeta obj5 = new FileMeta("mark", "txt", 10);
FileMeta obj6 = new FileMeta("zoom", "js", 50);

putinMap(map, obj1);
putinMap(map, obj2);
putinMap(map, obj3);
putinMap(map, obj4);
putinMap(map, obj5);
putinMap(map, obj6);

map.entrySet().stream().forEach(entry-> {
int size = entry.getValue().stream().mapToInt(FileMeta::getSize).sum();
System.out.println(entry.getKey() + " : " + size + " mb");
});


}

private static void putinMap(Map<String, List<FileMeta>> map, FileMeta obj) {
if (map.containsKey(obj.getExtension())) {
map.get(obj.getExtension()).add(obj);
} else {
ArrayList<FileMeta> list = new ArrayList<>();
list.add(obj);
map.put(obj.getExtension(), list);
}
}


in which key is the type of file Like mp3 or txt and value is the consist of some object which contain name and file size and extension.



you have to iterate on each row and sum of each filesize.






share|improve this answer


























  • Please don't use a wrapper when a primitive would be more appropriate. Unless size can be null use a long.

    – Peter Lawrey
    Nov 16 '18 at 7:44











  • @Khalid shah can u please elaborate more? How can I add file size for each similar extension. I am very beginner in map.

    – Razi Ahmad
    Nov 16 '18 at 12:18











  • @RaziAhmad see updated answer.

    – Khalid Shah
    Nov 16 '18 at 12:54











  • @Khalid shah thanks bro for this help

    – Razi Ahmad
    Nov 16 '18 at 14:34
















1














Yes. you can solve this problem via map.



  public class FileMeta {

String name;
String extension;
int size;

public FileMeta(String name, String extension, int size) {
this.name = name;
this.extension = extension;
this.size = size;
}

public String getName() {
return name;
}

public FileMeta setName(String name) {
this.name = name;
return this;
}

public String getExtension() {
return extension;
}

public FileMeta setExtension(String extension) {
this.extension = extension;
return this;
}

public int getSize() {
return size;
}

public FileMeta setSize(int size) {
this.size = size;
return this;
}

}

public static void main(String args) {
Map<String, List<FileMeta>> map = new ConcurrentHashMap<>();
FileMeta obj1 = new FileMeta("razi", "mp3", 10);
FileMeta obj2 = new FileMeta("test", "txt", 5);
FileMeta obj3 = new FileMeta("song", "mp3", 20);
FileMeta obj4 = new FileMeta("file", "js", 100);
FileMeta obj5 = new FileMeta("mark", "txt", 10);
FileMeta obj6 = new FileMeta("zoom", "js", 50);

putinMap(map, obj1);
putinMap(map, obj2);
putinMap(map, obj3);
putinMap(map, obj4);
putinMap(map, obj5);
putinMap(map, obj6);

map.entrySet().stream().forEach(entry-> {
int size = entry.getValue().stream().mapToInt(FileMeta::getSize).sum();
System.out.println(entry.getKey() + " : " + size + " mb");
});


}

private static void putinMap(Map<String, List<FileMeta>> map, FileMeta obj) {
if (map.containsKey(obj.getExtension())) {
map.get(obj.getExtension()).add(obj);
} else {
ArrayList<FileMeta> list = new ArrayList<>();
list.add(obj);
map.put(obj.getExtension(), list);
}
}


in which key is the type of file Like mp3 or txt and value is the consist of some object which contain name and file size and extension.



you have to iterate on each row and sum of each filesize.






share|improve this answer


























  • Please don't use a wrapper when a primitive would be more appropriate. Unless size can be null use a long.

    – Peter Lawrey
    Nov 16 '18 at 7:44











  • @Khalid shah can u please elaborate more? How can I add file size for each similar extension. I am very beginner in map.

    – Razi Ahmad
    Nov 16 '18 at 12:18











  • @RaziAhmad see updated answer.

    – Khalid Shah
    Nov 16 '18 at 12:54











  • @Khalid shah thanks bro for this help

    – Razi Ahmad
    Nov 16 '18 at 14:34














1












1








1







Yes. you can solve this problem via map.



  public class FileMeta {

String name;
String extension;
int size;

public FileMeta(String name, String extension, int size) {
this.name = name;
this.extension = extension;
this.size = size;
}

public String getName() {
return name;
}

public FileMeta setName(String name) {
this.name = name;
return this;
}

public String getExtension() {
return extension;
}

public FileMeta setExtension(String extension) {
this.extension = extension;
return this;
}

public int getSize() {
return size;
}

public FileMeta setSize(int size) {
this.size = size;
return this;
}

}

public static void main(String args) {
Map<String, List<FileMeta>> map = new ConcurrentHashMap<>();
FileMeta obj1 = new FileMeta("razi", "mp3", 10);
FileMeta obj2 = new FileMeta("test", "txt", 5);
FileMeta obj3 = new FileMeta("song", "mp3", 20);
FileMeta obj4 = new FileMeta("file", "js", 100);
FileMeta obj5 = new FileMeta("mark", "txt", 10);
FileMeta obj6 = new FileMeta("zoom", "js", 50);

putinMap(map, obj1);
putinMap(map, obj2);
putinMap(map, obj3);
putinMap(map, obj4);
putinMap(map, obj5);
putinMap(map, obj6);

map.entrySet().stream().forEach(entry-> {
int size = entry.getValue().stream().mapToInt(FileMeta::getSize).sum();
System.out.println(entry.getKey() + " : " + size + " mb");
});


}

private static void putinMap(Map<String, List<FileMeta>> map, FileMeta obj) {
if (map.containsKey(obj.getExtension())) {
map.get(obj.getExtension()).add(obj);
} else {
ArrayList<FileMeta> list = new ArrayList<>();
list.add(obj);
map.put(obj.getExtension(), list);
}
}


in which key is the type of file Like mp3 or txt and value is the consist of some object which contain name and file size and extension.



you have to iterate on each row and sum of each filesize.






share|improve this answer















Yes. you can solve this problem via map.



  public class FileMeta {

String name;
String extension;
int size;

public FileMeta(String name, String extension, int size) {
this.name = name;
this.extension = extension;
this.size = size;
}

public String getName() {
return name;
}

public FileMeta setName(String name) {
this.name = name;
return this;
}

public String getExtension() {
return extension;
}

public FileMeta setExtension(String extension) {
this.extension = extension;
return this;
}

public int getSize() {
return size;
}

public FileMeta setSize(int size) {
this.size = size;
return this;
}

}

public static void main(String args) {
Map<String, List<FileMeta>> map = new ConcurrentHashMap<>();
FileMeta obj1 = new FileMeta("razi", "mp3", 10);
FileMeta obj2 = new FileMeta("test", "txt", 5);
FileMeta obj3 = new FileMeta("song", "mp3", 20);
FileMeta obj4 = new FileMeta("file", "js", 100);
FileMeta obj5 = new FileMeta("mark", "txt", 10);
FileMeta obj6 = new FileMeta("zoom", "js", 50);

putinMap(map, obj1);
putinMap(map, obj2);
putinMap(map, obj3);
putinMap(map, obj4);
putinMap(map, obj5);
putinMap(map, obj6);

map.entrySet().stream().forEach(entry-> {
int size = entry.getValue().stream().mapToInt(FileMeta::getSize).sum();
System.out.println(entry.getKey() + " : " + size + " mb");
});


}

private static void putinMap(Map<String, List<FileMeta>> map, FileMeta obj) {
if (map.containsKey(obj.getExtension())) {
map.get(obj.getExtension()).add(obj);
} else {
ArrayList<FileMeta> list = new ArrayList<>();
list.add(obj);
map.put(obj.getExtension(), list);
}
}


in which key is the type of file Like mp3 or txt and value is the consist of some object which contain name and file size and extension.



you have to iterate on each row and sum of each filesize.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 14:40

























answered Nov 16 '18 at 6:11









Khalid ShahKhalid Shah

1,62921021




1,62921021













  • Please don't use a wrapper when a primitive would be more appropriate. Unless size can be null use a long.

    – Peter Lawrey
    Nov 16 '18 at 7:44











  • @Khalid shah can u please elaborate more? How can I add file size for each similar extension. I am very beginner in map.

    – Razi Ahmad
    Nov 16 '18 at 12:18











  • @RaziAhmad see updated answer.

    – Khalid Shah
    Nov 16 '18 at 12:54











  • @Khalid shah thanks bro for this help

    – Razi Ahmad
    Nov 16 '18 at 14:34



















  • Please don't use a wrapper when a primitive would be more appropriate. Unless size can be null use a long.

    – Peter Lawrey
    Nov 16 '18 at 7:44











  • @Khalid shah can u please elaborate more? How can I add file size for each similar extension. I am very beginner in map.

    – Razi Ahmad
    Nov 16 '18 at 12:18











  • @RaziAhmad see updated answer.

    – Khalid Shah
    Nov 16 '18 at 12:54











  • @Khalid shah thanks bro for this help

    – Razi Ahmad
    Nov 16 '18 at 14:34

















Please don't use a wrapper when a primitive would be more appropriate. Unless size can be null use a long.

– Peter Lawrey
Nov 16 '18 at 7:44





Please don't use a wrapper when a primitive would be more appropriate. Unless size can be null use a long.

– Peter Lawrey
Nov 16 '18 at 7:44













@Khalid shah can u please elaborate more? How can I add file size for each similar extension. I am very beginner in map.

– Razi Ahmad
Nov 16 '18 at 12:18





@Khalid shah can u please elaborate more? How can I add file size for each similar extension. I am very beginner in map.

– Razi Ahmad
Nov 16 '18 at 12:18













@RaziAhmad see updated answer.

– Khalid Shah
Nov 16 '18 at 12:54





@RaziAhmad see updated answer.

– Khalid Shah
Nov 16 '18 at 12:54













@Khalid shah thanks bro for this help

– Razi Ahmad
Nov 16 '18 at 14:34





@Khalid shah thanks bro for this help

– Razi Ahmad
Nov 16 '18 at 14:34













0














The following works:



import java.util.*;


public class Main {



static class FileInfo {
private String name;
private int sizeInMb;

public FileInfo(String name, int sizeInMb) {
this.name = name;
this.sizeInMb = sizeInMb;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getSizeInMb() {
return sizeInMb;
}

public void setSizeInMb(int sizeInMb) {
this.sizeInMb = sizeInMb;
}
}

public static void main(String args) {
List<FileInfo> fileInfos = new ArrayList<>();
fileInfos.add(new FileInfo("razi.mp3", 10));
fileInfos.add(new FileInfo("test.txt", 5));
fileInfos.add(new FileInfo("song.mp3", 20));
fileInfos.add(new FileInfo("file.js", 100));
fileInfos.add(new FileInfo("mark.txt", 10));
fileInfos.add(new FileInfo("zoom.js", 50));

Map<String, Integer> sizes = new HashMap<>();

//initiatise map
fileInfos.forEach(fileInfo -> {
final String extension = fileInfo.getName().substring(fileInfo.getName().lastIndexOf("."));
sizes.put(extension, 0);
});

System.out.println(sizes);

//put summaries
fileInfos.stream().forEach(fileInfo -> {
//get extension
String extension = fileInfo.getName().substring(fileInfo.getName().lastIndexOf("."));
sizes.put(extension, sizes.get(extension) + fileInfo.getSizeInMb());
});

System.out.println(sizes);
}


}






share|improve this answer
























  • Thanks @Alfred Samanga its working for me

    – Razi Ahmad
    Nov 16 '18 at 14:32


















0














The following works:



import java.util.*;


public class Main {



static class FileInfo {
private String name;
private int sizeInMb;

public FileInfo(String name, int sizeInMb) {
this.name = name;
this.sizeInMb = sizeInMb;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getSizeInMb() {
return sizeInMb;
}

public void setSizeInMb(int sizeInMb) {
this.sizeInMb = sizeInMb;
}
}

public static void main(String args) {
List<FileInfo> fileInfos = new ArrayList<>();
fileInfos.add(new FileInfo("razi.mp3", 10));
fileInfos.add(new FileInfo("test.txt", 5));
fileInfos.add(new FileInfo("song.mp3", 20));
fileInfos.add(new FileInfo("file.js", 100));
fileInfos.add(new FileInfo("mark.txt", 10));
fileInfos.add(new FileInfo("zoom.js", 50));

Map<String, Integer> sizes = new HashMap<>();

//initiatise map
fileInfos.forEach(fileInfo -> {
final String extension = fileInfo.getName().substring(fileInfo.getName().lastIndexOf("."));
sizes.put(extension, 0);
});

System.out.println(sizes);

//put summaries
fileInfos.stream().forEach(fileInfo -> {
//get extension
String extension = fileInfo.getName().substring(fileInfo.getName().lastIndexOf("."));
sizes.put(extension, sizes.get(extension) + fileInfo.getSizeInMb());
});

System.out.println(sizes);
}


}






share|improve this answer
























  • Thanks @Alfred Samanga its working for me

    – Razi Ahmad
    Nov 16 '18 at 14:32
















0












0








0







The following works:



import java.util.*;


public class Main {



static class FileInfo {
private String name;
private int sizeInMb;

public FileInfo(String name, int sizeInMb) {
this.name = name;
this.sizeInMb = sizeInMb;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getSizeInMb() {
return sizeInMb;
}

public void setSizeInMb(int sizeInMb) {
this.sizeInMb = sizeInMb;
}
}

public static void main(String args) {
List<FileInfo> fileInfos = new ArrayList<>();
fileInfos.add(new FileInfo("razi.mp3", 10));
fileInfos.add(new FileInfo("test.txt", 5));
fileInfos.add(new FileInfo("song.mp3", 20));
fileInfos.add(new FileInfo("file.js", 100));
fileInfos.add(new FileInfo("mark.txt", 10));
fileInfos.add(new FileInfo("zoom.js", 50));

Map<String, Integer> sizes = new HashMap<>();

//initiatise map
fileInfos.forEach(fileInfo -> {
final String extension = fileInfo.getName().substring(fileInfo.getName().lastIndexOf("."));
sizes.put(extension, 0);
});

System.out.println(sizes);

//put summaries
fileInfos.stream().forEach(fileInfo -> {
//get extension
String extension = fileInfo.getName().substring(fileInfo.getName().lastIndexOf("."));
sizes.put(extension, sizes.get(extension) + fileInfo.getSizeInMb());
});

System.out.println(sizes);
}


}






share|improve this answer













The following works:



import java.util.*;


public class Main {



static class FileInfo {
private String name;
private int sizeInMb;

public FileInfo(String name, int sizeInMb) {
this.name = name;
this.sizeInMb = sizeInMb;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getSizeInMb() {
return sizeInMb;
}

public void setSizeInMb(int sizeInMb) {
this.sizeInMb = sizeInMb;
}
}

public static void main(String args) {
List<FileInfo> fileInfos = new ArrayList<>();
fileInfos.add(new FileInfo("razi.mp3", 10));
fileInfos.add(new FileInfo("test.txt", 5));
fileInfos.add(new FileInfo("song.mp3", 20));
fileInfos.add(new FileInfo("file.js", 100));
fileInfos.add(new FileInfo("mark.txt", 10));
fileInfos.add(new FileInfo("zoom.js", 50));

Map<String, Integer> sizes = new HashMap<>();

//initiatise map
fileInfos.forEach(fileInfo -> {
final String extension = fileInfo.getName().substring(fileInfo.getName().lastIndexOf("."));
sizes.put(extension, 0);
});

System.out.println(sizes);

//put summaries
fileInfos.stream().forEach(fileInfo -> {
//get extension
String extension = fileInfo.getName().substring(fileInfo.getName().lastIndexOf("."));
sizes.put(extension, sizes.get(extension) + fileInfo.getSizeInMb());
});

System.out.println(sizes);
}


}







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 6:22









Alfred SamangaAlfred Samanga

905




905













  • Thanks @Alfred Samanga its working for me

    – Razi Ahmad
    Nov 16 '18 at 14:32





















  • Thanks @Alfred Samanga its working for me

    – Razi Ahmad
    Nov 16 '18 at 14:32



















Thanks @Alfred Samanga its working for me

– Razi Ahmad
Nov 16 '18 at 14:32







Thanks @Alfred Samanga its working for me

– Razi Ahmad
Nov 16 '18 at 14:32













0














you can create a FileObject class with instance members as name ,inputstream , size and type.



@Getter
@Setter
public class FileObject {
private String name;
private InputStream inputStream;
private String type;
private Long size;

public FileObject(String name, InputStream inputStream, String type, Long size) {
this.name = name;
this.inputStream = inputStream;
this.type = type;
this.size = size;
}
}


Create array of FilObject with you data.



Then you can create a Map with type attribute as key (corrspoding inital value as 0 )and for each Fileobject add the size value to corresponding key in map.



Map<String, FileObject> fileObjectMap = new HashMap<>();


After that you will be able to print your required output.

Hope it helps.






share|improve this answer





















  • 1





    Please don't use a wrapper when a primitive would be more appropriate. Unless size can be null use a long.

    – Peter Lawrey
    Nov 16 '18 at 7:44











  • @Krishna Murari can u please elaborate more? How can I add file size for each similar extension. I am very beginner in map.

    – Razi Ahmad
    Nov 16 '18 at 12:19











  • @RaziAhmad i have made some changes ,look into it

    – krishna Murari
    Nov 17 '18 at 18:08
















0














you can create a FileObject class with instance members as name ,inputstream , size and type.



@Getter
@Setter
public class FileObject {
private String name;
private InputStream inputStream;
private String type;
private Long size;

public FileObject(String name, InputStream inputStream, String type, Long size) {
this.name = name;
this.inputStream = inputStream;
this.type = type;
this.size = size;
}
}


Create array of FilObject with you data.



Then you can create a Map with type attribute as key (corrspoding inital value as 0 )and for each Fileobject add the size value to corresponding key in map.



Map<String, FileObject> fileObjectMap = new HashMap<>();


After that you will be able to print your required output.

Hope it helps.






share|improve this answer





















  • 1





    Please don't use a wrapper when a primitive would be more appropriate. Unless size can be null use a long.

    – Peter Lawrey
    Nov 16 '18 at 7:44











  • @Krishna Murari can u please elaborate more? How can I add file size for each similar extension. I am very beginner in map.

    – Razi Ahmad
    Nov 16 '18 at 12:19











  • @RaziAhmad i have made some changes ,look into it

    – krishna Murari
    Nov 17 '18 at 18:08














0












0








0







you can create a FileObject class with instance members as name ,inputstream , size and type.



@Getter
@Setter
public class FileObject {
private String name;
private InputStream inputStream;
private String type;
private Long size;

public FileObject(String name, InputStream inputStream, String type, Long size) {
this.name = name;
this.inputStream = inputStream;
this.type = type;
this.size = size;
}
}


Create array of FilObject with you data.



Then you can create a Map with type attribute as key (corrspoding inital value as 0 )and for each Fileobject add the size value to corresponding key in map.



Map<String, FileObject> fileObjectMap = new HashMap<>();


After that you will be able to print your required output.

Hope it helps.






share|improve this answer















you can create a FileObject class with instance members as name ,inputstream , size and type.



@Getter
@Setter
public class FileObject {
private String name;
private InputStream inputStream;
private String type;
private Long size;

public FileObject(String name, InputStream inputStream, String type, Long size) {
this.name = name;
this.inputStream = inputStream;
this.type = type;
this.size = size;
}
}


Create array of FilObject with you data.



Then you can create a Map with type attribute as key (corrspoding inital value as 0 )and for each Fileobject add the size value to corresponding key in map.



Map<String, FileObject> fileObjectMap = new HashMap<>();


After that you will be able to print your required output.

Hope it helps.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 17 '18 at 18:07

























answered Nov 16 '18 at 6:12









krishna Murarikrishna Murari

1016




1016








  • 1





    Please don't use a wrapper when a primitive would be more appropriate. Unless size can be null use a long.

    – Peter Lawrey
    Nov 16 '18 at 7:44











  • @Krishna Murari can u please elaborate more? How can I add file size for each similar extension. I am very beginner in map.

    – Razi Ahmad
    Nov 16 '18 at 12:19











  • @RaziAhmad i have made some changes ,look into it

    – krishna Murari
    Nov 17 '18 at 18:08














  • 1





    Please don't use a wrapper when a primitive would be more appropriate. Unless size can be null use a long.

    – Peter Lawrey
    Nov 16 '18 at 7:44











  • @Krishna Murari can u please elaborate more? How can I add file size for each similar extension. I am very beginner in map.

    – Razi Ahmad
    Nov 16 '18 at 12:19











  • @RaziAhmad i have made some changes ,look into it

    – krishna Murari
    Nov 17 '18 at 18:08








1




1





Please don't use a wrapper when a primitive would be more appropriate. Unless size can be null use a long.

– Peter Lawrey
Nov 16 '18 at 7:44





Please don't use a wrapper when a primitive would be more appropriate. Unless size can be null use a long.

– Peter Lawrey
Nov 16 '18 at 7:44













@Krishna Murari can u please elaborate more? How can I add file size for each similar extension. I am very beginner in map.

– Razi Ahmad
Nov 16 '18 at 12:19





@Krishna Murari can u please elaborate more? How can I add file size for each similar extension. I am very beginner in map.

– Razi Ahmad
Nov 16 '18 at 12:19













@RaziAhmad i have made some changes ,look into it

– krishna Murari
Nov 17 '18 at 18:08





@RaziAhmad i have made some changes ,look into it

– krishna Murari
Nov 17 '18 at 18:08


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332310%2fhow-can-i-add-of-same-type-of-file-names-as-key-with-total-memory-as-value-in-a%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python