hier ist Ihre komplette Lösung. Es ist in JAVA,
bitte überprüfen,
class SampleData{
private String key;
private HashMap<String, Integer> headerKeyName = new HashMap<String, Integer>();
private int total;
private String hashMapkey;
private Integer hashMapValue;
SampleData(String key, String hashMapkey, Integer hashMapValue){
this.key = key;
this.hashMapkey = hashMapkey;
this.hashMapValue = hashMapValue;
}
public HashMap<String, Integer> getHeaderKeyName() {
return headerKeyName;
}
public void setHeaderKeyName(HashMap<String, Integer> headerKeyName) {
this.headerKeyName = headerKeyName;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getHashMapkey() {
return hashMapkey;
}
public void setHashMapkey(String hashMapkey) {
this.hashMapkey = hashMapkey;
}
public Integer getHashMapValue() {
return hashMapValue;
}
public void setHashMapValue(Integer hashMapValue) {
this.hashMapValue = hashMapValue;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
@Override
public int hashCode() {
return this.getHashMapkey().length();
}
@Override
public boolean equals(Object obj) {
SampleData sd = (SampleData)obj;
if(sd.getKey().equals(this.getKey()) && sd.getHashMapkey().equals(this.getHashMapkey())){
sd.setHashMapValue(sd.getHashMapValue() + this.getHashMapValue());
return true;
}
return false;
}
}
Ihre Hauptklasse ist,
public class StackOverflowQuery{
public static void main(String[] args) {
String [][]answer;
SampleData s1 = new SampleData("A", "STARTS WITHIN 16 - 30 DAYS", 3); // set this value from your
SampleData s2 = new SampleData("B", "STARTS WITHIN 1 - 15 DAYS", 1);
SampleData s3 = new SampleData("B", "STARTS WITHIN 16 - 30 DAYS", 2);
SampleData s4 = new SampleData("C", "STARTS WITHIN 1 - 15 DAYS", 1);
SampleData s5 = new SampleData("C", "STARTS WITHIN 16 - 30 DAYS", 2);
SampleData s6 = new SampleData("C", "STARTS WITHIN 16 - 30 DAYS", 2);
SampleData s7 = new SampleData("D", "STARTS WITHIN 31 - 45 DAYS", 2);
HashSet<SampleData> hs = new LinkedHashSet<SampleData>();
Map<String, SampleData> hashMap = new LinkedHashMap<String, SampleData>();
hs.add(s1);
hs.add(s2);
hs.add(s3);
hs.add(s4);
hs.add(s5);
hs.add(s6);
hs.add(s7);
Set<String> listOfHeaders = new LinkedHashSet<String>();
listOfHeaders.add("Key");
for(SampleData sd: hs){
listOfHeaders.add(sd.getHashMapkey());
}
listOfHeaders.add("total");
for(String listofHead : listOfHeaders){
System.out.print(listofHead + "\t");
}
System.out.println();
for(SampleData sd: hs){
if(hashMap.get(sd.getKey()) != null){
SampleData sampleData = hashMap.get(sd.getKey());
HashMap<String , Integer> hm = sampleData.getHeaderKeyName();
if(hm.get(sd.getHashMapkey()) != null){
hm.put(sd.getHashMapkey(), hm.get(sd.getHashMapkey()) + sd.getHashMapValue());
}else{
hm.put(sd.getHashMapkey(), sd.getHashMapValue());
}
sampleData.setTotal(sampleData.getTotal() + sd.getHashMapValue().intValue());
hashMap.put(sd.getKey(), sampleData);
}else{
sd.getHeaderKeyName().put(sd.getHashMapkey(), sd.getHashMapValue());
sd.setTotal(sd.getHashMapValue().intValue());
sd.getHeaderKeyName().put(sd.getHashMapkey(), sd.getHashMapValue());
hashMap.put(sd.getKey(), sd);
}
}
System.out.println();
for(Entry<String, SampleData> entry : hashMap.entrySet()){
int tempArrayIndex = 0;
System.out.print(entry.getKey() + " \t\t ");
SampleData currentSampleData = entry.getValue();
for(String listofHead : listOfHeaders){
if(listofHead.equalsIgnoreCase("Key") || listofHead.equalsIgnoreCase("total")){
continue; // it's not part of print over here..
}
if(currentSampleData.getHeaderKeyName().get(listofHead) != null){
System.out.print(currentSampleData.getHeaderKeyName().get(listofHead) + " \t\t ");
}else{
System.out.print(" 0 \t\t ");
}
}
System.out.print(currentSampleData.getTotal());
System.out.println();
}
}
}
Und out-put (Es tut mir leid um die bieten nicht aus Stoßen ein richtiges Format wird),
Key STARTS WITHIN 16 - 30 DAYS STARTS WITHIN 1 - 15 DAYS STARTS WITHIN 31 - 45 DAYS total
A 3 0 0 3
B 2 1 0 3
C 4 1 0 5
D 0 0 2 2
Anzeige Ihres Codes –