T
T
TostMaria2014-11-26 11:21:07
Java
TostMaria, 2014-11-26 11:21:07

How to add only keys to HashMap?

Good afternoon!
Java question. There is a need to read lines of the form <number>+<" ">+<rest of the line> from the file.
For example: "95 Good afternoon!"
In the future, the information must be converted - to make a certain selection of numbers, and by these numbers - to get a string that corresponded to the number in the source file.
Do I understand correctly that HashMap is suitable for this?
But I have a question, is it possible to put only keys in a HashMap? And then only values?
For example, to extract the characters of the string itself, you need to use the following code (Google helped):
int start = 10;
int end = 14;
char buf[] = new char[end - start];
s.getChars(start, end, buf, 0);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Pavlov, 2014-11-26
@TostMaria

String a="41 text";
Map<Integer,String> map = new HashMap<>();
String[] temp = a.split(" ",2);
if(temp.length==2){
    map.put(Integer.valueOf(temp[0]),temp[1])
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question