in Education by
I tried to convert data from the list of strings into a Map with the Stream API. But the way I did it is not correct: public static Map converter (List data){ return data.stream().collect(Collectors.toMap(e->e, Function.identity())); If we have List.of("5", "7", "5", "8", "9", "8") task №1 as key - String, and value - character frequency task №2 as key - String, and value - string converted to integer JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Try it like this for a frequency. Note that a map can't have duplicate keys. List list2 = List.of("5", "7", "5", "8", "9","8"); the key is the String. the value starts a 1. the Integers::sum adds 1 for every occurrence. Map freq = list2.stream().collect(Collectors.toMap( e->e, (a)->1, Integer::sum)); freq.entrySet().forEach(System.out::println); prints 5=2 7=1 8=2 9=1 for the second task, to convert the strings to ints is trivial. But remember that dups can't exist so they have to be properly merged. This is similar to the first task except instead of counting we're just converting to an integer. Since you have duplicate integers, you need to provide instructions on what to do. That is the merge function (a,b)->a which says for existing value a and new value b, just keep a List list2 = List.of("5", "7", "5", "8", "9","8"); Map map = list2.stream().collectCollectors.toMap(e->e, Integer::valueOf, (a,b)->a); // just keep the first one prints 5=5 7=7 8=8 9=9

Related questions

0 votes
    I am trying to convert string into integer using jquery but it throwing NumberFormatException. How to fix it ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I have a given list of string and a list of characters and I want to check the string with ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 19, 2022 in Education by JackTerrance
0 votes
    How will you convert an integer to octal string in python?...
asked Nov 24, 2020 in Technology by JackTerrance
0 votes
    How will you convert an integer to hexadecimal string in python?...
asked Nov 24, 2020 in Technology by JackTerrance
0 votes
    What would be your preferred way to concatenate strings from a sequence such that between every two consecutive ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    Which of the following function convert an integer to hexadecimal string in python? A - unichr(x) B - ord(x) C - hex(x) D - oct(x)...
asked Apr 10, 2021 in Education by JackTerrance
0 votes
    How will you convert the returned API into an XML object? (a) SimpleElement() (b) SimpleXMLElement() ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    List strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    Liststrings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    I have code that at one place ends up with a list of data frames which I really want to convert ... starting with (this is grossly simplified for illustration): listOfDataFrames...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    Is there a data structure that implements the Map interface and holds an array of elements with continuous ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 24, 2022 in Education by JackTerrance
0 votes
    What is the purpose of map method of stream in Java 8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    How will you convert a string to a list in python?...
asked Nov 24, 2020 in Technology by JackTerrance
0 votes
    I want to store the hex values into a string, but I don't know to do that when my string is not giving me the ... using namespace std; int main(){ string s2 = "HelloWorld"; cout...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I want to store the hex values into a string, but I don't know to do that when my string is not giving me the ... using namespace std; int main(){ string s2 = "HelloWorld"; cout...
asked Apr 7, 2022 in Education by JackTerrance
...