I would like to override System.out.println(map) to print not
{A=1, B=2, C=3,...}
but
A 1
B 2
C 3
...
just like what I have done in readData function of MapManager class.
I have found some code that could be the hint for the solution from the definition code of AbstractMap<K,V> which is extended by TreeMap<K,V> and implements Map<K,V> In the definition of AbstractMap<K,V>, toString() is implemented like below:
public String toString() {
Iterator<Entry<K,V>> i = entrySet().iterator();
if (! i.hasNext())
return "{}";
StringBuilder sb = new StringBuilder();
sb.append('{');
for (;;) {
Entry<K,V> e = i.next();
K key = e.getKey();
V value = e.getValue();
sb.append(key == this ? "(this Map)" : key);
sb.append('=');
sb.append(value == this ? "(this Map)" : value);
if (! i.hasNext()) return sb.append('}').toString();
sb.append(',').append(' ');
}
}
If I would modify this code to make it does what I intend it to do, it would be like this:
public String toString() {
Iterator<Entry<K,V>> i = entrySet().iterator();
if (! i.hasNext())return "{}";
StringBuilder sb = new StringBuilder();
sb.append('{');
for (;;) {
Entry<K,V> e = i.next();
K key = e.getKey();
V value = e.getValue();sb.append(key == this ? "(this Map)" : key);
sb.append(' '); //whitespace instead of ‘=’
sb.append(value == this ? "(this Map)" : value);
if (! i.hasNext())return sb.toString();// make sb into String without appending anything.
sb.append('\n'); //append newline instead of ‘,’ and ‘ ’
}
}
BUT added to this, I would like to print the map’s entries in the ascending order of value and the alphabetical order of key, so what I want println to do is what the following instructions would do:
set = box.entrySet();
list = new ArrayList<>(set);
Collections.sort(list, new ValueComparator<Map.Entry<String, Double>>());
Iterator<Map.Entry<String, Double>> it = list.iterator();
while(it.hasNext()) {
Map.Entry<String, Double> entry = it.next();
double value = entry.getValue().doubleValue();
System.out.println(entry.getKey() + " " + value);
}
How could I do overrinding in my code to achieve this? One important constraint is that I must not modify the code inside public class Problem{ }.
Below is the entire code that I wrote:
import java.util.*;
import java.util.Map.Entry;
import java.io.*;
class MapManager{
private static BufferedReader br;
private static Set<Map.Entry<String, Double>> set;
private static List<Map.Entry<String, Double>> list;
static class ValueComparator<T> implements Comparator<T>{
public int compare(Object o1, Object o2) {
if(o1 instanceof Map.Entry && o2 instanceof Map.Entry) {
Map.Entry<String, Double> e1 = (Map.Entry<String, Double>) o1;
Map.Entry<String, Double> e2 = (Map.Entry<String, Double>) o2;
double v1 = e1.getValue().doubleValue();
double v2 = e2.getValue().doubleValue();
return (int)(v1 - v2);
}
return -1;
}
}
public static TreeMap<String, Double> readData(String fn){
TreeMap<String, Double> box = new TreeMap<>();
int data = 0, i = 0, j = 0, digits = 0, points = 0;
String buffer = " ";
String name = " ";
String price = " ";
(omitted)
set = box.entrySet();
list = new ArrayList<>(set);
Collections.sort(list, new ValueComparator<Map.Entry<String, Double>>());
Iterator<Map.Entry<String, Double>> it = list.iterator();
while(it.hasNext()) {
Map.Entry<String, Double> entry = it.next();
double value = entry.getValue().doubleValue();
System.out.println(entry.getKey() + " " + value);
}
return box;
}
}
public class Problem {
public static void main(String[] args) {
Map<String, Double> map = MapManager.readData("input.txt");
if(map == null) {
System.out.println("Input file not found.");
return;
}
System.out.println(map);
}
}