#Inserting Numbers As Strings Into A Hashmap
1 messages ยท Page 1 of 1 (latest)
because hashmap doesn't have a defined iteration order
due to how it stores data
if you want iteration order to be consistent with insertion order, use LinkedHashMap
well, for this kind of structure you don't even need a map
an array will do
another option is to use a TreeMap where you provide a comparator which sorts your entries
it might be
which order to you want ?
alphabetical order ? numeric order ?
of what ? keys ? values ?
Detected code, here are some useful tools:
No it doesn't, linked variant only keep insertion order
it's sensitive to insertion order
TreeMap will preserve it no matter the insertion order
but I wrote that it is ๐ค
you can't insert in an array
array is not sensitive because you manually select a position to insert
Being sensitive to insertion order is the only reason why linkedhashmap exists
linkedhashmap always appends to the end
No I don't, you manually say where you want to put elements in an array, you can't insert
insert means that the structure grows in size
an array has fixed size and you tell where you want elements to be
so not only you can't insert
but order is also defined by the user
which is totally unrelated to other data structures
because you inserted in same order
also
what's the point of this
why 1=A ?
is it always equal to A ?
but
why can't you just do 'A' - character
?
Snippets
Snippet 24, VALID
for(char c = 'A'; c <= 'Z'; c++) {
print(c);
}```
## System out
ABCDEFGHIJKLMNOPQRSTUVWXYZ```
Snippets
Snippet 23, REJECTED
public statis char numberToChar(int number) {
if (number < 0 ||| number > 25) throw new IllegalArgumentException();
return 'A' + number;
}```
## [WARNING] The code couldn't end properly...
Problematic source code:
```java
public statis char numberToChar(int number) {
if (number < 0 ||| number > 25) throw new IllegalArgumentException();
return 'A' + number;
}```
Cause:
The code doesn't compile:
';' expected
';' expected
';' expected
illegal start of expression
cannot find symbol
symbol: variable statis
location: class
unreachable statement
unreachable statement
variable number might not have been initialized
Remaining code:
```java
print(numberToChar(3));```
## System out
[Nothing]
cool, I made a typo
Snippets
Snippet 24, REJECTED
public static char numberToChar(int number) {
if (number < 0 ||| number > 25) throw new IllegalArgumentException();
return 'A' + number;
}```
## [WARNING] The code couldn't end properly...
Problematic source code:
```java
public static char numberToChar(int number) {
if (number < 0 ||| number > 25) throw new IllegalArgumentException();
return 'A' + number;
}```
Cause:
The code doesn't compile:
illegal start of expression
Remaining code:
```java
print(numberToChar(3));```
## System out
[Nothing]
Snippets
Snippet 25, VALID
public static String numberToChar(int number) {
if (number < 0 || number > 25) throw new IllegalArgumentException();
return String.valueOf('A' + number);
}```
### Snippet 26, VALID
```java
print(numberToChar(3));```
## System out
68```
lol 68
Snippets
Snippet 25, VALID
public static String numberToChar(int number) {
if (number < 0 || number > 25) throw new IllegalArgumentException();
return String.valueOf((char)('A' + number));
}```
### Snippet 27, VALID
```java
print(numberToChar(3));```
## System out
D```
there
???? @craggy bobcat
converting char to string is just a single line, so I have no idea what you are asking... and why do you wnat strings when you specifically need to work with chars ?
heh
?
How is string related to a job ?
How does it even make sense ?
are you a js dev or what ?
I don't understand what you are trying to do
If you just want to print this, you could use my code from before and just slightly modify the print

Snippets
Snippet 25, VALID
for(char c = 'A'; c <= 'Z'; c++) {
print((c - 'A') + " = " + c);
}```
## System out
0 = A1 = B2 = C3 = D4 = E5 = F6 = G7 = H8 = I9 = J10 = K11 = L12 = M13 = N14 = O15 = P16 = Q17 = R18 = S19 = T20 = U21 = V22 = W23 = X24 = Y25 = Z```
Snippets
Snippet 26, REJECTED
IntStream.range('A', 'Z').mapToObj((c - 'A') + " = " + (char)c).collect(Collectors.joining(", "))```
## [WARNING] The code couldn't end properly...
Problematic source code:
```java
IntStream.range('A', 'Z').mapToObj((c - 'A') + " = " + (char)c).collect(Collectors.joining(", "))```
Cause:
The code doesn't compile:
cannot find symbol
symbol: variable c
location: class
cannot find symbol
symbol: variable c
location: class
## System out
[Nothing]
Snippets
Snippet 27, VALID
IntStream.range('A', 'Z').mapToObj(c -> (c - 'A') + " = " + (char)c).collect(Collectors.joining(", "))```
jshell> `"0 = A, 1 = B, 2 = C, 3 = D, 4 = E, 5 = F, 6 = G, 7 = H, 8 = I, 9 = J, 10 = K, 11 = L, 12 = M, 13 = N, 14 = O, 15 = P, 16 = Q, 17 = R, 18 = S, 19 = T, 20 = U, 21 = V, 22 = W, 23 = X, 24 = Y"`
## System out
[Nothing]
Detected code, here are some useful tools:
emulate what ? this does nothing
but you are not using map
so your code does nothing
please show what you actually want to do
Filling a map with values isn't what you want
since it does nothing in its own
"most efficient" is relative
you could use a loop to populate the map, with a bit of calculation
since numbers & letters already map to each other through ASCII
the ASCII value for A is 65. so just add 65 to whatever the number is, cast to char, then convert to String
int num = 0;
char value = num + 65; // 'A'
String letter = String.valueOf(value); // "A"```
but how ????
yes I know that, this is obvious
the question is how
why are you trying so hard to avoid answering my question
please don't use 65 but 'A' like I and Quniteger did earlier
But the problem isn't that, the problem is that we still don't know how he is gonna use the app, from what we know, he wants to iterate on it in a certain order, which isn't the job of a map at the first place, we don't know if he will edit the map, etc
And he is constantly dodging to answer the question of how he is using the map -_-
you seem to disregard presentation a lot. i was expressing the numeric
so i will definitely not do what you mentioned, cause it doesnt express what i was saying ๐
i get what you mean, 65 is a magic number. but the purpose is to emphasize the numeric relationship
I mean, I showed that earlier by doing a loop on chars
yes, but that doesnt mean they understand the concept or the code you showed. you proved it, yeah
Detected code, here are some useful tools:
why would order matter?
as long as 0 maps to A, why does it matter that 0 -> A appears in the map before 1 -> B?
that seems to be the question others were wondering. why do you need order? why do you need to iterate?
based on your example, doesnt seem like either is needed
still have any questions/concerns?
could use a loop to populate the map, or a stream, etc..
you could use what Alathreon suggested, in terms of populating the map
could use a Stream like they did
or you could use a loop
efficient in what way?
high performance code may be messy, and prolongs the time it takes to find bugs. its not efficient for finding bugs
"least amount of code" is not a good metric to go by
that small amount of code could be cryptic, hard to understand
readability & performance
and its a balance. the more readable your code becomes, the less performant it may be
the more performant your code is, the less readable may be
always prefer readability. worry about performance when it becomes a problem
when a bug appears, you want to be able to solve it ASAP
some bugs could ruin the entire app
so readability is critical
performance is good. you need it these days. but if a bug breaks your system, "being fast" wont matter
doesnt matter how fast your app is, if it doesnt work
based on the project, the "bigger picture", and in your specific situation....
it doesnt matter
solve this, move onto the next problem
spent way too much time on this
you can even keep what you already got
Detected code, here are some useful tools:
there are faaaaaaar bigger problems out there to be solved
i would recommend "stop putting so much time into it"
its not a bottleneck
its not a bug
could it be improved? sure. but are there more important things to worry about? yes, 100% yes
id say consider this problem solved & move onto the next. if i were to write it, id probably use Alathreon's approach with the stream
this
but thats only because i know this project wont be touched again. its a throw-away
if you plan on using this in a larger system, & have more concerns, you should talk about that
now you sound like the who were waiting on you ๐
i'd use the stream version, get it done with quickly
Then you can replace all your map by Quinteger code
none
just do the single line of code that quinteger gave you
HashMap<String, String> map = new HashMap<String, String>();
map.put("0", "A");
map.put("1", "B");
map.put("2", "C");
map.put("3", "D");
map.put("4", "E");
map.put("5", "F");
map.put("6", "G");
map.put("7", "H");
map.put("8", "I");
map.put("9", "J");
map.put("10", "K");
map.put("11", "L");
map.put("12", "M");
map.put("13", "N");
map.put("14", "O");
map.put("15", "P");
map.put("16", "Q");
map.put("17", "R");
map.put("18", "S");
map.put("19", "T");
map.put("20", "U");
map.put("21", "V");
map.put("22", "W");
map.put("23", "X");
map.put("24", "Y");
map.put("25", "Z");
...
String character = map.get(String.valueOf(remainder));
Instead of this
...
String character = String.valueOf('A' + remainder);
you do this
wait, I don't understand they need a loop now ????
they could use a loop
A loop to do what ?
they want mappings, having a map is the elegant way to approach it
its not a requirement, and we dont know where they plan to use this. sure, they could have a function which returns the letter corresponding to the number
but then again, why not have all maps implemented like that?
because this is an object environment
because they want mappings
they said they only want it so they can transform an int into a string
just do the math
you don't need a map for this
yeah, its mapped using ascii. you can take advantage of that
just do 'A' + number
yes
and?
and also what I used in my loop at the start of this question
we are going in a loop here
what value are you adding to this post with all this
they can do that. they want a map
They didn't say that they want a map and don't want to do arithmetic
check the post, and look at how consistently they mention how they want a map through-out this post
then why do they want a map ?
why are you still begging for this? you already gave them the anti-map solution
no
They don't want the map
they just don't know what the other solutions are
You might notice that they didn't understand how quinteger solve their problem
they might not want the map. so give them solutions for both & let them decide based on what you guys have said
Right, better like that ๐
You can't just say that they want the map
and even if you say that
you have to explain why
they said they wanted the map
but they might not know what is better for them
expecially here
where the better solution isn't a map
yeah, that doesnt mean they're wrong though
based on what they said
but you arent sure how they plan on using it
I asked several times this question
And the answer they gave was to get char by integer
but thats doesnt tell you anything about how they're using it.. they said "populate map", they never said "no longer need map"
all options were given
i dont get what you're going on about
here
"full use of the map"
if they object, they can post
like I have shown, they shared how they are using it
im not saying a map is needed. im saying they never said it
and you know what they say about assumpsions
so let them chime in, if thats the case. if they dont need a map, cool, they have a solution for that too now
this is all bloat
it's not just that, they are contradicting themselve in the use of the map and no matter how you see it, it's very hard to see how it could work better with a map than without
like, unless you can shuffle keys and values
So wait and see I guess
you can introduce a function which performs the conversion
String toLetter(int number)```
var letter = toLetter(0); // A```
it'll perform calculations each time to determine which letter should be used
that is Integer -> String
it accepts an int, it returns a String
im not gonna feed you the code ๐
you'd just take the number, perform the calculation that we've mentioned before
convert to char, convert that char to String
like what i've done here
take a number, convert it to char using the ASCII offset
how would toString help here?
primitives do not have a toString method. you could use String.valueOf, but it's not needed
there already exists a mapping between numbers & letters. it's ASCII
all you're doing is taking advantage of that mapping system, rather than using your own
yes, it can be done without an array too
i wouldn't recommend using an array
you want mappings. use a Map in that case
an array could be used for mapping, but it's not suitable for your situation
could have array[65] = "A"
because it doesnt provide good mapping
array[65] = "A", thats a good mapping
but its index 65 of the array
indices 0-64 will go unused
so thats wasted space, room for error (what if someone does array[63]?)
"map" is the concept that matters
whether it's a hash map, or a tree map...
that depends on your use case
What I wrote yesterday
sounds like a hash map would be fit
can't blame em for not understanding though
but yes, this is what quinteger & ala have been on about
Here
And if you want to cache the strings and not recreate them every time, you can just use an array
may as well just use a map, at that point
Well, an array will definitely have the required order if a need arises to iterate all entries
that's the benefit of using a map, being able to keep that data in memory, not having to re-calculate
quinteger was just suggesting that you can use an array to possibly improve performance of the function mentioned before
instead of having to re-calculate each time, could have an array store frequently-requested values
i was just saying a Map would be the better approach, since it's less complex & covers the whole "have to recalculate" problem
it's more descriptive (we have "mappings", a "map" is the tool)
it handles the same problem (not having to re-calculate)
yes, many
hence why i recommend a map
Empty index?
you can't make the mistake of accessing an empty index
if you had an array to cache the conversions
array[65] = "A"
what about 0-64?
If the task is to transform numbers 0 to 25 into letters, there would be no empty indices
You have an array of 26
yes, you can manually write out the array java array[0] = "A" array[1] = "B"
thats what they were doing initially, with the map
You can also fill it with a loop
yes
they could just use a Map
it's higher level, provides more capability. should always prefer higher-level abstractions
if you want mappings, you should prefer a Map, as it gives you all the ancillary benefits, functions that are focused on mapped data
could achieve mapping through arrays, could take advantage of ASCII mappings.. but in the end, what matters is that your code does what it needs
and that you can interact with that code fluently
Detected code, here are some useful tools:
I am against because the map is useless, you could just do 'A' + number instead of the whole map
add parhentesis around 'A' + 26
feels like enough time has been spent on this, there are other problems to solve
there are multiple ways to do this, just use what you see fit
what
no
// Outputs an integer value of zero.
System.out.println("\nA: " + (char)('A' + 26));
this @craggy bobcat
Detected code, here are some useful tools:
you just replace
map.get(String.valueOf(remainder))
by
String.valueOf((char)('A' - remainder)))
wait
it's a +
not a minus
you just replace
map.get(String.valueOf(remainder))
by
String.valueOf((char)('A' + remainder)))
this @craggy bobcat
I have no idea why there was a -
isn't that what you want?
..yes
@craggy bobcat
Your question has been closed due to inactivity.
If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.
Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.
When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.
Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.
With enough info, someone knows the answer for sure ๐
Closing thread...