Exclude Indexes from HashMap

I am trying to implement an algorithm that does the following:

If the number stored in the variable compHandexists, its index will be stored in indexArray, and this index will be added to the method banIndex(), so this index will never be considered for further work.

OR

if the summation of any two numbers in the list is equal compHand, the indices of that number will be stored in indexArrayand added to banIndex(), so that they will never be considered for any further work.

Actually, the algorithm works fine, but always, if the last valuehashMap is 10, then it 10will be displayed twice? it should be displayed only once. for example: the result of this algorithm according to populateHash()will be: 5,6,7,7 and it should be: 5,6,7

Any idea why this is happening?

the 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);*/  
}
}
+4
source share
2 answers

I reworked your code and dropped the redundant cascades of if-else.

My solution generates the following result (0 numbers exist due to a predefined array length): iter> -1 5 6 7 0 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, tougher 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);


    }
}

: : 1; : 3 : 7; : 7 : 4; : 10 : 5; : 10 : 6; : 10 : 9; : 10 : 11; : 8 : 17; : 2

+2

isBannedIndex. for (int i=1; i<=_bannedIndexHash.size(); i++) ( ):

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class Main {

    static HashMap<Integer, Integer> h1 = new HashMap<Integer, Integer>();

    public static void main(String[] args) {

        int compHand = 10;
        populateHash(Main.h1);

        int iter = -1;
        int[] indexArray = new int[(Main.h1.size())];
        Set<Integer> bannedIndex = new HashSet<Integer>();

        for (int i = 1; i <= Main.h1.size(); i++) {
            if (!isBannedIndex(bannedIndex, i)) {
                if (i == Main.h1.size()) {
                    if (compHand == Main.h1.get(i)) {
                        indexArray[++iter] = i;
                        banIndex(bannedIndex, i);
                    }// end if
                } else {
                    if (compHand == Main.h1.get(i)) {
                        indexArray[++iter] = i;
                        banIndex(bannedIndex, i);
                    }// end if
                    else {
                        for (int j = i + 1; j <= Main.h1.size(); j++) {
                            if (!isBannedIndex(bannedIndex, j)) {
                                if ((compHand == (Main.h1.get(i) + Main.h1
                                        .get(j)))
                                        || (compHand == Main.h1.get(j))) {
                                    if (compHand == (Main.h1.get(i) + Main.h1
                                            .get(j))) {
                                        indexArray[++iter] = i;
                                        indexArray[++iter] = j;
                                        banIndex(bannedIndex, i);
                                        banIndex(bannedIndex, j);
                                        break;
                                    }// end if
                                    else {
                                        if (compHand == Main.h1.get(j)) {
                                            indexArray[++iter] = j;
                                            banIndex(bannedIndex, 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(Set<Integer> bannedIndex, int index) {
        // TODO Auto-generated method stub
        // Log.i(TAG, "@isBannedIndex(): ");

        return bannedIndex.contains(index);
    }

    private static void banIndex(Set<Integer> bannedIndex, int index) {
        // TODO Auto-generated method stub
        // Log.i(TAG, "@banIndex(): ");

        if (bannedIndex != null)
            bannedIndex.add(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);
         */

    }
}
+1

All Articles