Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.List; public class Main { public static double calculateCosineSimilarity(List<Double> list1, List<Double> list2) { if (list1.size() != list2.size()) { System.err.println("Two lists must have the same dimensionality."); return 0; } double dividend = 0, divisor1 = 0, divisor2 = 0; for (int i = 0; i < list1.size(); i++) { dividend += list1.get(i) * list2.get(i); divisor1 += Math.pow(list1.get(i), 2); divisor2 += Math.pow(list2.get(i), 2); } return dividend / (Math.sqrt(divisor1) * Math.sqrt(divisor2)); } }