I have hashmap as
Map<Integer, CustomClass>
CustomClass is defined as
Class CustomClass {
String s,
Integer i
... constructors & getter setters
}
I have the object initialization as
Map<Integer, CustomClass> map = new HashMap<>()
populated with the values.
I want to sort on the basis of the CustomClass's String s data member.
I have tried writing lambda expression but unable to get the proper sorted order map.
I have tried mocking the following code
import java.util.*;
class FileMap implements Comparable<FileMap>{
private String fileName;
private int file;
public FileMap(String fileName, int file){
this.fileName = fileName;
this.file = file;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public int getFile() {
return file;
}
public void setFile(int file) {
this.file = file;
}
@Override
public int compareTo(FileMap that){
return this.fileName.compareTo(that.getFileName());
}
@Override
public String toString(){
return this.fileName;
}
}
class Main {
public static void main(String[] args) {
FileMap fm1 = new FileMap("abc.txt", 0);
FileMap fm2 = new FileMap("abd.txt", 0);
FileMap fm3 = new FileMap("abe.txt", 0);
FileMap fm4 = new FileMap("abf.txt", 0);
Map<Integer, FileMap> fileMap = new HashMap<>();
fileMap.put(0, fm1);
fileMap.put(1, fm3);
fileMap.put(2, fm2);
fileMap.put(3, fm4);
System.out.println(fileMap);
Map<Integer, FileMap> tree = new TreeMap<>();
tree.putAll(fileMap);
System.out.println(tree);
}
}