1

I've just started learning java. I'm trying to write a program to find three elements from three different arrays such that, a + b + c = sum. (I'm avoiding using three for loops to make it more efficient)

I'm getting the following error.

10: error: cannot find symbol
       HashMap<int> s = new HashMap<int>();
       ^
   class HashMap
 class YesorNo

This is my code:

class YesorNo
    {
         // Function to check if there is an element from
         // each array such that sum of the three elements
         // is equal to given sum.
         boolean findTriplet(int a1[], int a2[], int a3[],
                int n1, int n2, int n3, int sum)
        {
        // Store elements of first array in hash table
           HashMap<int> s = new HashMap<int>();
             //unordered_set <int> s;
             for (int i=0; i<n1; i++)
             s.add(a1[i]);

             // sum last two arrays element one by one
             for (int i=0; i<n2; i++)
             {
                for (int j=0; j<n3; j++)
                {
                // Consider current pair and find if there
                // is an element in a1[] such that these
                // three form a required triplet
                if (s.find(sum - a2[i] - a3[j]) != s.end())
                    return true;
                }
            }
            return false;
        }

        // Driver Code
        public static void main(String[] args) 
        {
            YesorNo check = new YesorNo();
            int a1[] = { 1 , 2 , 3 , 4 , 5 };
            int n1 = a1.length;
            int a2[] = { 2 , 3 , 6 , 1 , 2 };
            int n2 = a2.length;
            int a3[] = { 3 , 2 , 4 , 5 , 6 };
            int n3 = a3.length;
            int sum=9;

            System.out.println(check.findTriplet(a1, a2, a3, n1, n2, n3, sum));
        }
    }
CoderGirl94
  • 214
  • 2
  • 12
  • 1
    Also do you have necessary imports in your program? – opensam Feb 11 '17 at 15:38
  • 1
    Use this to find the correct Collection : http://www.sergiy.ca/img/doc/java-map-collection-cheat-sheet.gif for a list of value (like an array but without limit) : List -> ArrayList ..., Map -> HashMap.. if for pairs – azro Feb 11 '17 at 15:39

2 Answers2

4

Use a HashSet, and it should be an Integer (since you can't use primitives as generic types with collections and prefer the interface).

Set<Integer> s = new HashSet<>();
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 2
    "you can't use primitives with collections" you mean we can't use primitives as *generic types*. We can use primitives with collections: `s.add(1)` will fork fine thanks to autobixing :) – Pshemo Feb 11 '17 at 15:45
  • @Pshemo Yes. Yes I do mean that. :) – Elliott Frisch Feb 11 '17 at 16:25
0

HashMap is not a single dimension Collection and it can't hold primitive types like 'int' as @Pshemo mentioned in the comments. You have to specify key and the values mapped to that key. Refer this tutorial for more information.

To insert values into HashMap,

HashMap<Integer, Integer> s = new HashMap<Integer, Integer>();
for (int i=0; i<n1; i++)
         s.put(i, a1[i]);

As an alternative for s.find, you can use

s.containsValue

But you can't jump to the end of HashMap as you have written s.end. You need to follow some tweaks mentioned here.

Community
  • 1
  • 1
learner
  • 276
  • 1
  • 3
  • 16