sum

Java 1.7: Sum of Iterable<T extends Number>

放肆的年华 提交于 2021-01-27 07:33:46
问题 I need to create a helper method which allows to create a sum of any Iterable<? extends Number>, because we have many vectors and require a fast method to determine the sum, so I created the following method: static Integer sum(Iterable<Integer> it) { Integer result = 0; for(T next : it) { result += next; } return result; } This method only works for ints however, but we also have doubles and longs. Because you can't have two methods with the same signature (Our compiler thinks Integer sum

Java 1.7: Sum of Iterable<T extends Number>

蓝咒 提交于 2021-01-27 07:31:12
问题 I need to create a helper method which allows to create a sum of any Iterable<? extends Number>, because we have many vectors and require a fast method to determine the sum, so I created the following method: static Integer sum(Iterable<Integer> it) { Integer result = 0; for(T next : it) { result += next; } return result; } This method only works for ints however, but we also have doubles and longs. Because you can't have two methods with the same signature (Our compiler thinks Integer sum

Exact sum of a long array

一曲冷凌霜 提交于 2021-01-27 04:24:07
问题 In order to get the exact sum of a long[] I'm using the following snippet. public static BigInteger sum(long[] a) { long low = 0; long high = 0; for (final long x : a) { low += (x & 0xFFFF_FFFFL); high += (x >> 32); } return BigInteger.valueOf(high).shiftLeft(32).add(BigInteger.valueOf(low)); } It works fine by processing the numbers split in two halves and finally combining the partial sums. Surprisingly, this method works too: public static BigInteger fastestSum(long[] a) { long low = 0;

How do you find the sum of all numbers in a set in Java? [duplicate]

这一生的挚爱 提交于 2021-01-26 02:58:20
问题 This question already has answers here : Is there possibility of sum of ArrayList without looping (13 answers) Closed 1 year ago . How would you find the sum of the elements of a set in Java? Would it be the same with an array? In python, I could do: my_set = {1, 2, 3, 4} print(sum(my_set)) 回答1: Aside from using loops explicitly, for List<Integer> list you can do: int sum = list.stream().mapToInt(Integer::intValue).sum(); If it's an int[] ar then do: int sum = IntStream.of(ar).sum(); This is

How do you find the sum of all numbers in a set in Java? [duplicate]

廉价感情. 提交于 2021-01-26 02:57:16
问题 This question already has answers here : Is there possibility of sum of ArrayList without looping (13 answers) Closed 1 year ago . How would you find the sum of the elements of a set in Java? Would it be the same with an array? In python, I could do: my_set = {1, 2, 3, 4} print(sum(my_set)) 回答1: Aside from using loops explicitly, for List<Integer> list you can do: int sum = list.stream().mapToInt(Integer::intValue).sum(); If it's an int[] ar then do: int sum = IntStream.of(ar).sum(); This is

How do you find the sum of all numbers in a set in Java? [duplicate]

拥有回忆 提交于 2021-01-26 02:55:41
问题 This question already has answers here : Is there possibility of sum of ArrayList without looping (13 answers) Closed 1 year ago . How would you find the sum of the elements of a set in Java? Would it be the same with an array? In python, I could do: my_set = {1, 2, 3, 4} print(sum(my_set)) 回答1: Aside from using loops explicitly, for List<Integer> list you can do: int sum = list.stream().mapToInt(Integer::intValue).sum(); If it's an int[] ar then do: int sum = IntStream.of(ar).sum(); This is

Counting the number of unique values by date in R

冷暖自知 提交于 2021-01-20 12:07:20
问题 Please help me to count the number of unique IDs per Date. so, initially, there is this data frame of IDs and dates ID Date 1 2009/11/1 1 2009/11/2 1 2009/11/2 2 2009/11/1 2 2009/11/1 2 2009/11/2 3 2009/11/1 3 2009/11/3 It is possible to rearrange it by date. If we do so then we will see that on the 1st there are 3 unique IDs. On the 2ed 2 unique ID and on the 3rd there is one unique ID. So the final table should look like this: Date uniqueIDs 2009/11/1 3 2009/11/2 2 2009/11/3 1 I know that

How can I sum the values associated with a reoccurring key in a hashmap

泪湿孤枕 提交于 2021-01-20 07:23:33
问题 I want to add the values of the same keys in a hashmap. For example: ABC --> 123 DEF --> 456 ABC --> 123 XXX --> 111 XXX --> 222 should become: ABC --> 246 DEF --> 456 XXX --> 333 Here's the code I have so far: public class Reading { @SuppressWarnings("unchecked") public static void main(String[] args) throws IOException { //Create hashmap to store the the string and int in the excel sheet HashMap<String, Integer> hm = new HashMap<String, Integer>(); String key = null; int value = Integer.MIN

data.table sum and subset

隐身守侯 提交于 2021-01-18 05:29:25
问题 I have a data.table that I am wanting to aggregate library(data.table) dt1 <- data.table(year=c("2001","2001","2001","2002","2002","2002","2002"), group=c("a","a","b","a","a","b","b"), amt=c(20,40,20,35,30,28,19)) I am wanting to sum the amt by year and group and then filter where the summed amt for any given group is greater than 100. I've got the data.table sum nailed. dt1[, sum(amt),by=list(year,group)] year group V1 1: 2001 a 60 2: 2001 b 20 3: 2002 a 65 4: 2002 b 47 I am having trouble

Adding up all the elements of each column in a 2d array

陌路散爱 提交于 2021-01-16 04:11:50
问题 So I have this dummy 2d array: int mat[][] = { {10, 20, 30, 40, 50, 60, 70, 80, 90}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50, 51, 89}}; I want to add up all the values by columns so it would add 10 + 15 + 27 + 32 and return 84 and so on. I have this so far: public void sum(int[][] array) { int count = 0; for (int rows = 0; rows < array.length; rows++) { for (int columns = 0; columns < array[rows].length; columns++) { System.out.print(array[rows][columns] + "\t"); count += array[0]