Java – exclude indexes from HashMap
I'm trying to implement an algorithm that does the following:
If the number stored in the variable comphand exists, its index will be stored in indexarray, and the index will be added to the banindex () method so that the index will never be considered for further operations
or
If the sum of any two numbers in the list is equal to comphand, the indexes of these numbers will be stored in indexarray and added to banindex() so that they will never be considered for any further operation
In fact, the algorithm works normally, but if the last value of HashMap is 10, will 10 be displayed twice? It should only be displayed once. Why? For example, according to the algorithm of populatehash (), the result will be: 5,6,7,7, which should be: 5,6,7
Do you know why?
code
public class Test {
static HashMap<Integer, Integer> h1 = new HashMap<Integer, Integer>();
public static void main(String[] args) {
int compHand = 10;
populateHash(Test.h1);
int iter = -1;
int [] indexArray = new int[(Test.h1.size())];
HashMap<Integer, Integer> bannedIndexHash = new HashMap<Integer, Integer>();
for (int i=1; i<=Test.h1.size(); i++) {
if (! isBannedIndex(bannedIndexHash, i)) {
if (i == Test.h1.size()) {
if (compHand == Test.h1.get(i)) {
indexArray[++iter] = i;
banIndex(bannedIndexHash, i);
}// end if
}else {
if (compHand == Test.h1.get(i)) {
indexArray[++iter] = i;
banIndex(bannedIndexHash, i);
}//end if
else {
for (int j=i+1; j<=Test.h1.size(); j++) {
if ( (! isBannedIndex(bannedIndexHash, i)) &&
(! isBannedIndex(bannedIndexHash, j)) ) {
if ( (compHand == (Test.h1.get(i)+Test.h1.get(j))) || (compHand == Test.h1.get(j)) ) {
if (compHand == (Test.h1.get(i)+Test.h1.get(j))) {
indexArray[++iter] = i;
indexArray[++iter] = j;
banIndex(bannedIndexHash, i);
banIndex(bannedIndexHash, j);
break;
}//end if
else {
if (compHand == Test.h1.get(j)) {
indexArray[++iter] = j;
banIndex(bannedIndexHash, j);
}// end if
}// end else
}// end if-condition ||
}// end if-condition &&
}// end for (j)
}// end else
}//end else
}// end ! isBannedIndex(bannedIndexHash, i)
}// end for(i)
if (iter > -1) {
System.out.println("iter > -1");
for (int i=0; i<indexArray.length; i++) {
System.out.println(indexArray[i]);
}
}
}
private static boolean isBannedIndex(HashMap<Integer, Integer> _bannedIndexHash, int index) {
// TODO Auto-generated method stub
//Log.i(TAG, "@isBannedIndex(): ");
if (!_bannedIndexHash.isEmpty()) {
for (int i=1; i<_bannedIndexHash.size(); i++)
if (index == _bannedIndexHash.get(i))
return true;
return false;
}else
return false;
}
private static void banIndex(HashMap<Integer, Integer> _bannedIndexHash, int index) {
// TODO Auto-generated method stub
//Log.i(TAG, "@banIndex(): ");
if (_bannedIndexHash != null)
_bannedIndexHash.put(_bannedIndexHash.size()+1, index);
}
private static void populateHash(HashMap<Integer, Integer> hash) {
// TODO Auto-generated method stub
hash.put(1, 1);
hash.put(2, 3);
hash.put(3, 1);
hash.put(4, 1);
hash.put(5, 10);
hash.put(6, 10);
hash.put(7, 10);
/*hash.put(8, 7);
hash.put(9, 1);
hash.put(10, 10);
hash.put(11, 5);
hash.put(12, 8);
hash.put(13, 1);
hash.put(14, 1);
hash.put(15, 6);
hash.put(16, 1);
hash.put(17, 1);
hash.put(18, 2);*/
}
}
resolvent:
I rewritten your code and discarded the redundant if else concatenation
My solution generates the following output (there are 0 numbers due to the predefined array length): ITER > - 1 5 6 7 0 0 0
public class Test {
public static void main(final String[] args) {
HashMap<Integer, Integer> testHashes = new HashMap<Integer, Integer>();
int compHand = 10;
populateHash(testHashes);
int iter = -1;
int[] indexArray = new int[(testHashes.size())];
HashMap<Integer, Integer> bannedValues = new HashMap<Integer, Integer>();
for (int i = 1; i <= testHashes.size(); i++) {
if (!isBannedIndex(bannedValues, i)) {
if (compHand == testHashes.get(i)) {
indexArray[++iter] = i;
banIndex(bannedValues, i);
} else {
for (int j = i + 1; j <= testHashes.size(); j++) {
if (!isBannedIndex(bannedValues, j)) {
if(compHand == testHashes.get(i) + testHashes.get(j)) {
indexArray[++iter] = i;
indexArray[++iter] = j;
banIndex(bannedValues, i);
banIndex(bannedValues, j);
break;
} else {
if (compHand == testHashes.get(j)) {
indexArray[++iter] = j;
banIndex(bannedValues, j);
}
}
}
}
}
}
}
if (iter > -1) {
System.out.println("iter > -1");
for (int i = 0; i < indexArray.length; i++) {
System.out.println(indexArray[i]);
}
}
}
private static boolean isBannedIndex(
final HashMap<Integer, Integer> banned, final int index) {
return !banned.isEmpty() && banned.values().contains(index);
}
private static void banIndex(final HashMap<Integer, Integer> banned,
final int index) {
if (banned != null)
banned.put(banned.size() + 1, index);
}
private static void populateHash(final HashMap<Integer, Integer> hash) {
// TODO Auto-generated method stub
hash.put(1, 1);
hash.put(2, 3);
hash.put(3, 1);
hash.put(4, 1);
hash.put(5, 10);
hash.put(6, 10);
hash.put(7, 10);
/*
* hash.put(8, 7); hash.put(9, 1); hash.put(10, 10); hash.put(11, 5);
* hash.put(12, 8); hash.put(13, 1); hash.put(14, 1); hash.put(15, 6);
* hash.put(16, 1); hash.put(17, 1); hash.put(18, 2);
*/
}
}
Edit: another, more rigorous version might look like this:
import java.util.ArrayList;
import java.util.List;
public class Test {
static List<Integer> resultIndexes = new ArrayList<Integer>();
public static void main(final String[] args) {
List<Integer> testHashes = new ArrayList<Integer>();
populateHash(testHashes);
Integer compHand = new Integer(10);
for(int i = 0; i < testHashes.size(); i++){
if(isBanned(i)){
continue;
}
Integer valueA = testHashes.get(i);
if(valueA.equals(compHand)){
resultIndexes.add(i);
} else {
Integer valueB = compHand - valueA;
int index = testHashes.indexOf(valueB);
if(!isBanned(index) && index > -1 && index != i){
resultIndexes.add(i);
resultIndexes.add(testHashes.indexOf(valueB));
}
}
}
for(int i : resultIndexes){
System.out.println("Index: "+i+"; Value: "+testHashes.get(i));
}
}
private static boolean isBanned(final Integer i){
return resultIndexes.contains(i);
}
private static void populateHash(final List<Integer> hashes) {
hashes.add(1);
hashes.add(3);
hashes.add(1);
hashes.add(1);
hashes.add(10);
hashes.add(10);
hashes.add(10);
hashes.add(7); hashes.add(1); hashes.add(10); hashes.add(5);
hashes.add(8); hashes.add(1); hashes.add(1); hashes.add(6);
hashes.add(1); hashes.add(1); hashes.add(2);
}
}
Console log: index: 1; Value: 3 index: 7; Value: 7 index: 4; Value: 10 index: 5; Value: 10 index: 6; Value: 10 index: 9; Value: 10 index: 11; Value: 8 index: 17; Value: 2