java - convert an int array to another fixed length array with lambda -
what equivalent of following function lambda?
private string convertweekdaytobitstring(int[] ia) { int[] sa = {0, 0, 0, 0, 0, 0, 0}; (int : ia) { if (i > 0 && < 8) { sa[i-1] = 1; } } return arrays.tostring(sa).replace("[", "").replace("]", "").trim(); }
the purpose of function, mark "day of week" 1 according input array. example, if input array [5,6], output should 0,0,0,0,1,1,0
you stream 7 values using intstream
, , each 1 check if it's in input array. if input array guaranteed sorted, use single statement this:
return intstream .range(0, 7) .maptoobj(x -> string.valueof(arrays.binarysearch(ia, x) >= 0 ? 1 : 0)) .collect(collectors.joining(", "));
Comments
Post a Comment