#Why when I transplant the code do I now have errors with code?
1 messages · Page 1 of 1 (latest)
Set keys = rows.keySet();
Collection values = rows.values();
List<String> keyList = new ArrayList<String>(keys);
List<HashMap> valueList = new ArrayList<HashMap>(values); // [{2 = [0,0,0,0]}, {3 = [0,1,0,0]}..]
Iterator<String> it = keyList.iterator();
Iterator<HashMap> lit = valueList.iterator();
String [] intBinaries = new String[75]; //saying generic class
while (it.hasNext()){
String val = it.next();
HashMap vale = lit.next();
Set keys1 = vale.keySet();
Collection values1 = vale.values();
if (val.equals("Building " + custombuilding)){
int kik = 1;
intBinaries[kik-1] = keys1; //building height
intBinaries[kik]=values1.toString().split(",")[0].substring(2,3);
intBinaries[kik+1] = values1.toString().split(",")[1].substring(1,2);
intBinaries[kik+2] = values1.toString().split(",")[2].substring(1,2);
intBinaries[kik+3] = values1.toString().split(",")[3].substring(1,2);```
NVM got it to work.
Then apart from your issue... let's look at possible things to improve...
- Some useful shorthand
The 'diamond operator' can infer the generic argument from the context (where you're assigning to)
This...
List<String> keyList = new ArrayList<String>(keys);
Can be replaced with...
List<String> keyList = new ArrayList<>(keys);
- Redundant copying...
You create the keyList and valueList copies just so you can iterate them... You can iterate the originals...
If you're not changing the iterator source, you can skip the copying.
This..
Set keys = rows.keySet();
Collection values = rows.values();
List<String> keyList = new ArrayList<>(keys);
List<HashMap> valueList = new ArrayList<>(values); // [{2 = [0,0,0,0]}, {3 = [0,1,0,0]}..]
Iterator<String> it = keyList.iterator();
Iterator<HashMap> lit = valueList.iterator();
Can be replaced with...
Iterator<String> it = rows.keySet().iterator();
Iterator<HashMap> lit = rows.values().iterator();
- Repeated work
You toString() a value multiple times and split it multiple times. Capture the result and reuse it.
eg
String[] parts = values1.toString().split(",");
Then you can say...
intBinaries[kik] = parts[0].substring(2, 3);
intBinaries[kik + 1] = parts[1].substring(1, 2);
intBinaries[kik + 2] = parts[2].substring(1, 2);
intBinaries[kik + 3] = parts[3].substring(1, 2);
And you recompute the custom-building key to match on. We can capture that and maybe even skip some work...
eg. computing the key and using it earlier...
String customBuildingKey = "Building " + custombuilding;
while (it.hasNext()) {
String val = it.next();
if (val.equals(customBuildingKey)) {
HashMap vale = lit.next();
Set keys1 = vale.keySet();
Collection values1 = vale.values();
int kik = 1;
intBinaries[kik - 1] = keys1; //building height
intBinaries[kik] = values1.toString().split(",")[0].substring(2, 3);
intBinaries[kik + 1] = values1.toString().split(",")[1].substring(1, 2);
intBinaries[kik + 2] = values1.toString().split(",")[2].substring(1, 2);
intBinaries[kik + 3] = values1.toString().split(",")[3].substring(1, 2);
I can probably also make some type assumptions and complete most of the generics... Down to this...
public List<String> displayAugmentation(Map<String, Map<String, ?>> rows, int custombuilding){
String[] intBinaries = new String[75]; //saying generic class
String customBuildingKey = "Building " + custombuilding;
Iterator<String> it = rows.keySet().iterator();
Iterator<HashMap> lit = rows.values().iterator();
while (it.hasNext()) {
String val = it.next();
if (val.equals(customBuildingKey)) {
Map<String, ?> vale = lit.next();
Set<String> keys1 = vale.keySet();
Collection<?> values1 = vale.values();
int kik = 1;
String[] parts = values1.toString().split(",");
intBinaries[kik-1] = keys1; //building height
intBinaries[kik] = parts[0].substring(2, 3);
intBinaries[kik + 1] = parts[1].substring(1, 2);
intBinaries[kik + 2] = parts[2].substring(1, 2);
intBinaries[kik + 3] = parts[3].substring(1, 2);
Note: I don't know the type of the inner values (since you only ever toString() the values). There's probably improvement possible here too...
- You probably want to iterate the entries of the maps, not the keys and values separately.
This removes the need for the iterators entirely.
public <String>List displayAugmentation(Map<String, Map<String, ?>> rows, int custombuilding){
String[] intBinaries = new String[75]; //saying generic class
String customBuildingKey = "Building " + custombuilding;
for (Map.Entry<String, Map<String, ?>> row : rows.entrySet()) {
String val = row.getKey();
if (val.equals(customBuildingKey)) {
Map<String, ?> vale = row.getValue();
Collection<String> keys1 = vale.keySet();
String[] valueParts = vale.values().toString().split(",");
int kik = 1;
intBinaries[kik-1] = keys1; //building height
intBinaries[kik] = valueParts[0].substring(2, 3);
intBinaries[kik + 1] = valueParts[1].substring(1, 2);
intBinaries[kik + 2] = valueParts[2].substring(1, 2);
intBinaries[kik + 3] = valueParts[3].substring(1, 2);