java-8

how to solve this use-case, any way to use , array, struct , explode and structure data?

二次信任 提交于 2020-08-10 19:44:24
问题 I am using spark-sql-2.4.1v with Java 8. I need to calculate percentiles such as 25,75,90 for some given data. Given source dataset: val df = Seq( (10, "1/15/2018", 0.010680705, 10, 0.619875458, 0.010680705, "east"), (10, "1/15/2018", 0.006628853, 4, 0.16039063, 0.01378215, "west"), (10, "1/15/2018", 0.01378215, 20, 0.082049528, 0.010680705, "east"), (10, "1/15/2018", 0.810680705, 6, 0.819875458, 0.702228853, "west"), (10, "1/15/2018", 0.702228853, 30, 0.916039063, 0.810680705, "east"), (11,

Best way to read Spring Multipartfile line by line in Java 8

末鹿安然 提交于 2020-08-09 09:23:07
问题 What is the best handle a csv Spring Multipartfile? I have used something like this before: public void handleFile(MultipartFile multipartFile){ try{ InputStream inputStream = multipartFile.getInputStream(); IOUtils.readLines(inputStream, StandardCharsets.UTF_8) .stream() .forEach(this::handleLine); } catch (IOException e) { // handle exception } } private void handleLine(String s) { // do stuff per line } As far as I know, this first loads the whole file into a list in memory before

Java - read UTF-8 file with a single emoji symbol

女生的网名这么多〃 提交于 2020-08-05 09:38:39
问题 I have a file with a single unicode symbol. The file is encoded in UTF-8. It contains a single symbol represented as 4 bytes. https://www.fileformat.info/info/unicode/char/1f60a/index.htm F0 9F 98 8A When I read the file I get two symbols/chars. The program below prints ? 2 ? ? 55357 56842 ====================================== �� 16 & ====================================== ? 2 ? ====================================== Is this normal... or a bug? Or am I misusing something? How

Java - read UTF-8 file with a single emoji symbol

ⅰ亾dé卋堺 提交于 2020-08-05 09:37:12
问题 I have a file with a single unicode symbol. The file is encoded in UTF-8. It contains a single symbol represented as 4 bytes. https://www.fileformat.info/info/unicode/char/1f60a/index.htm F0 9F 98 8A When I read the file I get two symbols/chars. The program below prints ? 2 ? ? 55357 56842 ====================================== �� 16 & ====================================== ? 2 ? ====================================== Is this normal... or a bug? Or am I misusing something? How

Initialize static final Date using custom String

半腔热情 提交于 2020-08-05 09:36:17
问题 I am working with Java and come through one random problem. Here I had shared sample code of my problem. I want to initialize some of static final date field with my custom string format. public class Sample { protected static final Date MAX_DATE ; static { try { MAX_DATE = new SimpleDateFormat("yyyy-MM-dd").parse("2099-12-31"); } catch (ParseException e) { e.printStackTrace(); } } } While directly putting below line, it's asking for try and catch. protected static final Date MAX_DATE= new

Counting all elements in map of string to list

回眸只為那壹抹淺笑 提交于 2020-08-05 07:24:54
问题 Is there a better way to calculate size of all lists in map? So firstList.size() + secondList.size() ? I have done this, but creating a list just for counting doesn't feel right approach .. public static void main(String[] args) { List<String> firstList = new ArrayList<>(); firstList.add("first"); List<String> secondList = new ArrayList<>(); secondList.add("second"); Map<String, List<String>> myMap = new HashMap<>(); myMap.put("one", firstList); myMap.put("two", secondList); int size = myMap

Counting all elements in map of string to list

99封情书 提交于 2020-08-05 07:23:05
问题 Is there a better way to calculate size of all lists in map? So firstList.size() + secondList.size() ? I have done this, but creating a list just for counting doesn't feel right approach .. public static void main(String[] args) { List<String> firstList = new ArrayList<>(); firstList.add("first"); List<String> secondList = new ArrayList<>(); secondList.add("second"); Map<String, List<String>> myMap = new HashMap<>(); myMap.put("one", firstList); myMap.put("two", secondList); int size = myMap

Append object to list and return result in Java 8?

故事扮演 提交于 2020-08-05 02:33:17
问题 Is there a way of appending an object to a list and returning the result in one line in a functional non-imperative way? How would you do it if also the original list should not be mutated? Java 8 is allowed. I already know how to concat two lists in one line. (Source) List listAB = Stream.concat(listA.stream(), listB.stream()).collect(Collectors.toList()); I also know how to make a list out of objects in one line. List listO1 = Collections.singletonList(objectA); List listO2 = Stream.of

Append object to list and return result in Java 8?

谁都会走 提交于 2020-08-05 02:31:28
问题 Is there a way of appending an object to a list and returning the result in one line in a functional non-imperative way? How would you do it if also the original list should not be mutated? Java 8 is allowed. I already know how to concat two lists in one line. (Source) List listAB = Stream.concat(listA.stream(), listB.stream()).collect(Collectors.toList()); I also know how to make a list out of objects in one line. List listO1 = Collections.singletonList(objectA); List listO2 = Stream.of

Java 8 list to nested map

假装没事ソ 提交于 2020-08-02 07:10:00
问题 I hava a list of Class A like class A { private Integer keyA; private Integer keyB; private String text; } I want to transfer aList to nested Map mapped by keyA and keyB So I create below code. Map<Integer, Map<Integer,List<A>>> aMappedByKeyAAndKeyB = aList.stream() .collect(Collectors.collectingAndThen(Collectors.groupingBy(A::getKeyA), result -> { Map<Integer, Map<Integer, List<A>>> nestedMap = new HashMap<Integer, Map<Integer, List<A>>>(); result.entrySet().stream().forEach(e -> {nestedMap