user5256621
user5256621

Reputation:

Sum up two vectors

May I know how to sum two vectors in java?

public void CosineSimilarity(ArrayList<Integer> h,String a, Object[] array) throws Exception { 
           Vector value  = new Vector();
           double cos_sim=(cosine_similarity(vec1,vec2))*60/100;
           System.out.println(cos_sim); //I get [0.333] and [0.358]
           value.add(cos_sim);   
           CompareType(value,a,array);
}

Here the CompareType function

 public void CompareType(Vector value,String a,Object[] array ) throws Exception {
                // TODO Auto-generated method stub
                String k;
                double c = 0;
                String sql="Select Type from menu ";
                DatabaseConnection db = new DatabaseConnection();
                Connection  conn =db.getConnection();
                PreparedStatement  ps = conn.prepareStatement(sql);
                ResultSet rs = ps.executeQuery();
                Vector value1 = new Vector();
                while (rs.next()) 
                {
                k=rs.getString("Type");
                if(a.equals(k))
                {
                    c=10.0/100;
                    value1.add(c);
                    }
                else
                {
                    c=0;
                    value1.add(c);  
                }
                 }
                System.out.println(value1); // I get [0.0] and [0.1]
                Sum(value,value1);

                ps.close();
                rs.close();
                conn.close();

            }

What should I write in below function so the two values vectors can be added up and return two total values?

private void Sum(Vector value, Vector value1) {

        // TODO Auto-generated method stub

    }

Upvotes: 4

Views: 10138

Answers (1)

slartidan
slartidan

Reputation: 21576

Easy thing with Java8 streaming API (this example code outputs 6.0 as the sum of 1.0+2.0+3.0:

/** setup test data and call {@link #sum(Vector)} */
public static void main(String[] args) {
    Vector a = new Vector();
    Vector b = new Vector();

    a.add(1.0);
    a.add(2.0);
    b.add(3.0);

    System.out.println(sum(a, b));
}

/** Sum up all values of two vectors */
private static double sum(Vector value, Vector value1) {
    return sum(value) + sum(value1);
}

/** Sum up all values of one vector */
private static double sum(Vector value) {
    return

            // turn your vector into a stream
            value.stream()

            // make the stream of objects to a double stream (using generics would
            // make this easier)
            .mapToDouble(x -> (double) x)

            // use super fast internal sum method of java
            .sum();
}

Some ideas on how to make your code even better:

  • Use generics. They will help you to avoid casts and the compiler will automatically show you bugs of your code.
  • Name your variables and methods meaningfully. Use sumA and sumB instead of sum and sum1, etc.
  • Use Java coding conventions (like using small case for method names). This will help other Java developers understand your code faster.
  • Use interfaces and superclasses as variable types, return types and parameter types. This makes your code better reusable. Use Collection or List interfaces in your example.
  • Use java.util.ArrayList in favor of Vector. (from official javadoc: "If a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector.")

Upvotes: 3

Related Questions