![]() ![]() collect(oupingBy(i->s.charAt(i)+"")) īut I recommend you become familiar with the basics before delving into streams (or until your instructor recommends to do so). and group them by character in a list of indices. filter(i->Character.isLetter(s.charAt(i))) Here is a much more compact way using streams. There are also easier ways to do this but this is how most would accomplish this prior to Java 8. ![]() Note that your key to hashMap is a String but you're using a character which is not the same. Think of an array that takes a digit as an index and you can store anything there.Ī hashMap can take anything as a key (like an index but it is called a key) and it can also store anything. This presumes a HashMap> List sCharIndex Ī hashMap is nothing more than a special data structure that takes an object as a key. ![]() ncat(o.stream(), n.stream()).collect(Collectors.toList())) Įssentially, this is what you want to do. String string = input.replaceAll("\\s", "") public HashMap> concordanceForString(String s) ĮDIT - this uses a Character as the key to the map: String input = "hello world" Got: 1īut I really think I'm not grasping HashMap very well, so I'd appreciate if anyone could guide me through the basics, or provide anything educational about using HashMap, especially ones that use ArrayList. The error message is: : Wrong number of entries in Concordance. So I was writing a java class and was planning to convert to json format. Could you explain to me the relationship of the map to those objects, and to their components which are ultimately what are recorded as keys and values? When trying to debug, it stops processing before the map call. ArrayList RequestIdentifiers new ArrayListI'm really confused by the requirement of the inputs as String and ArrayList, rather than chars and integers. I have to create a HashMap that records the letters in a string and their index values in a ArrayList, so that if the HashMap is called with some string key, each related index integer is returned, and so that the map can be called by itself such that each key is shown with their indexes, For example for the string "Hello World", the map would look something like: d=, o=, r=, W=, H=, l=, e=. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |