Adding an element into a HashSet inside a HashMap Java [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have this problem which I looked for into the net and I could be helped... I also looked other Questions and they didnt work i dont know why... So I need your help....
So this is a field in which I create the HashMap:
private HashMap <String,HashSet<String>> userBuisness = new HashMap <String,HashSet<String>>();
And this is my try to add an element (i take a line from a file, i split it and then i add these elements into my HashMap):
String output = inputReader.nextLine();
String fields[] = output.split("\t");
userBuisness.put(fields[0],fields[1]);

As #AndyTurner said in a comment:
fields[1] is a String, not a HashSet<String>. You can build the latter using new HashSet<>(Arrays.asList(fields[1])).
But there are other issues with this snippet too. It would be better to rewrite like this, pay close attention to every little detail that I changed:
private Map<String, Set<String>> userBusiness = new HashMap<>();
...
String[] fields = output.split("\t");
userBusiness.put(fields[0], new HashSet<>(Collections.singletonList(fields[1])));

Related

(Android) What are these variable? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
This post was edited and submitted for review 3 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
source: this post
//Retrieve the values
Set<String> set = myScores.getStringSet("key", null);
//Set the values
Set<String> set = new HashSet<String>();
set.addAll(listOfExistingScores);
scoreEditor.putStringSet("key", set);
scoreEditor.commit();
is myScores refers to sharedpreference?
what is listOfExistingScores refers to? is it ArrayList?
is scoreEditor refers to ArrayList? or ListView? or custom ListView?
According to that answser you can say that those three variable defines following things :
myScores :
As you said in op, it's shared preference variable. It holdes sharedpreference.
listOfExistingScores :
It refers to the array declared by user. It is stored in hashset.
scoreEditor :
Score editor is sharedpreferences editor. You can read more details about it by given link.

Error with String[] and writeObject [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
In writeObject of FileOutputStream we declare one String Array that time error are occurs.
My Code is
writeObject(String[] str {"1"});
To begin with, 1 is not a String .
Also, your syntax is not correct to pass the array , try this way :
yourObjectOutputStream.writeObject(new String[] {"1"});
Or if you prefer
String[] str = new String[] {"1"};
yourObjectOutputStream.writeObject(str);

Local variable is redundant [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am new to android, can anyone tell me what why is emailresult redundant?
From what I understand is that I retrieve textToUse from another method and name it email here, and then use email to undergo the matcher.find() with the result named emailresult. I then returned emailresult and after that returned the entire email.
I have mess around with it some time, like deleting emailresult and just use email. But then I will still have to create another variable to go under this location:
String emailresult = email.substring(matcher.start(), matcher.end());
It is redundant because you aren't doing anything with emailresult after assigning it a value besides returning it. You can simply do the following without the need to create a variable:
return email.substring(matcher.start(), matcher.end());
There is no need to create a variable
return email.substring(matcher.start(), matcher.end());

put(java.lang.String,java.lang.Inte ger) in java.util.Map<java.lang.String,java.lang.Integer> cannot be applied to (java.lang.String,java.lang.String) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am getting this error while compiling my code. Please help me with this-
code is
mapConnectionProperties = new HashMap<String, Integer>();
mapConnectionProperties.put(mobileSeriesMappingDTO
.getExternalIP(), mobileSeriesMappingDTO.getExternalPort());
mobileSeriesMappingDTO.getExternalPort() is seemingly a String. Convert it to an Integer.
Integer.parseInt(mobileSeriesMappingDTO.getExternalPort())
Your mobileSeriesMappingDTO.getExternalPort() gives a String,
Transform it to an Integer with :
Integer.parseInt(mobileSeriesMappingDTO.getExternalPort())

replace x, y in arraylist of arraylists in java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to figure out how to replace a specific index in a arraylist of arraylists
The only stuff I've been able to find so far is how to just get the index with .get(x).get(y)
I'm having no real luck with google at the moment, so I figured I'd finally make a post about it. Any help would be great, thanks in advance.
You can do
.get(x).set(y, value);
Something like this:
ArrayList<ArrayList<String>> s = . . .;
s.get(3).set(4, "new value");
As #peeskillet suggest a best solution..
But check this core java code if it have some sense.
String arrayList[] = {....your array content...};
for (String token : arrayList) {
token = token.replace("x", "replaceanythings");
token = token.replace("y", "replaceanythings");
}

Categories

Resources