Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have an ArrayList like this:
[{1=R111, 2=Red, 3=50000}, {1=R123, 2=Blue , 3=50000}]
and i want to remove the array by value (R111 or R123).
how to remove the array using array.remove method for array like that?
I've try this link
but it's doesn't work for my problem.
Assuming your ArrayList is this:
List<String[]> arrayList = new ArrayList<>();
arrayList.add(new String[]{"R111","Red","50000"});
arrayList.add(new String[]{"R123","Blue","50000"});
you can do something like:
for (Iterator<String[]> iterator = arrayList.iterator();iterator.hasNext();) {
String[] stringArray = iterator.next();
if("R111".equals(stringArray[0])) {
iterator.remove();
}
}
You can safely remove an element using iterator.remove() while iterating the ArrayList. Also see The collection Interface.
An alternative shorter approach using Streams would be:
Optional<String[]> array = arrayList.stream().filter(a -> "R111".equals(a[0])).findFirst();
array.ifPresent(strings -> arrayList.remove(strings));
Thanks pieter, I used Iterator like this:
for (Iterator<HashMap<String, String>> iterator = RegulerMenu.iterator(); iterator.hasNext();) {
HashMap<String, String> stringArray = iterator.next();
if("R111".equals(stringArray.get("1"))) {
iterator.remove();
}
}
It's work now, Thankyou verymuch.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am trying to delete all values associated with all keys in my HashMap, but still keep the keys.
Is the below correct / the most efficient way to do so?
for (Map.Entry<Kennel, List<Dog>> entry : hashMap.entrySet()) {
String key = entry.getKey().getId();
List<Dog> dogList = entry.getValue();
//Loop through the list associated with each key and delete each Dog in the list
for (int i=0; i<dogList.size(); i++){
dogService.delete(dogList.get(i));
dogService.save(dogList.get(i));
}
}
Simpler:
for(dogs : hashMap.values()) {
for(dog : dogs) {
dogService.delete(dog);
dogService.save(dog);
}
dogs.clear();
}
I don't know what you are trying to accomplish here but if you just want the unique keys then probably you should be using a HashSet instead of HashMap.
But, if you want to perform the deletion you can just do the following:
for (Kennel key : hashMap.keySet()) {
hashMap.put(key, null);
}
I have written Kennel key assuming that key of your HashMap is of type Kennel.
You could use at the end:
hashMap.put(entry.getKey(), null);
removing the whole list, but if you want put new dogs into it in the future (as I think you want), and your dog lists are modifiable, the following approach is more memory-friendly:
for (Map.Entry<Kennel, List<Dog>> entry : hashMap.entrySet()) {
String key = entry.getKey().getId();
Iterator<Dog> it = entry.getValue().iterator();
while (it.hasNext()) {
Dog dog = it.next();
dogService.delete(dog);
dogService.save(dog);
it.remove();
}
}
since you avoid allocating new lists in the future. Notice also the usage of it.remove() that allows deletion while iterating
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to search for a specific value in a HashMap, using an iterator, currently I have this method. I am very new to Java so your help would be greatly appreciated. helper.readAMap is hashmap which stores responses, which are generated when a user types in a certain word.
public String generateResponse(String words)
{
HashMap<String, String> map = new HashMap();
map = helper.readAMap("replies.txt");
Iterator<String> it = map.keySet();
while(it.hasNext()) {
String word = it.next();
String response = map.get(word);
if(response != null) {
return response;
}
}
return pickDefaultResponse();
}
Here:
if(key.equals(words)) {
You compare a String to a HashSet of Strings. That is like comparing an apple to a pear; it will always be false.
So you either want a single String as argument to your method, or you want to generate responses to all of the words.
I expect you want to do this:
if(words.contains(key)) { // Your input contains the key
return map.get(key); // Retrieve the response to the key from the map
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am implementing an algorithm,for that I need to use ArrayList of ArrayList.I want to retrieve each ArrayList from the Main ArrayList.
Eg: If the Main ArrayList contains 5 ArrayList and Each ArrayList in the main list contains 5 rows.
MainArrayList:
[1,2,3,4,5,a , 2,3,4,5,3,b , 7,8,9,4,5,d] //1st Arraylist
[1,2,3,4,5,e , 2,3,4,5,3,n , 7,8,9,4,5,e] //11nd Arraylist
[1,2,3,4,5,f , 2,3,4,5,3,t , 7,8,9,4,5,q] //111rd Arraylist
[1,2,3,4,5,c , 2,3,4,5,3,b , 7,8,9,4,5,a] //1vth Arraylist
[1,2,3,4,5,r , 2,3,4,5,3,m , 7,8,9,4,5,z] //v th Arraylist
I need to access each ArrayList from the main list and print it's content row by row
i.e for example
print 1st ArrayList from the main list.
1,2,3,4,5,a
2,3,4,5,3,b
7,8,9,4,5,d
similarly I need to print all arraylist in the main list same as above.
you can do it using ArrayList
List<List<Object>> twoDimArray = new ArrayList<ArrayList<>>();
twoDimArray.add(Arrays.asList("1","2","3");
for(List<Object> list: twoDimArray){
for(Object o : list){
System.out.println(o.toString());
}
}
And of course using HashMap:
Map<String,List<Object>> map = new HashMap<String,List<Object>>();
for (Map.Entry<String, String> entry : map.entrySet())
{
System.out.println(entry.getKey() + "/" + entry.getValue());
}
Use HashMap for resolving this problem.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm working on a game and I'm implementing objects where you can define in "TiledMap editor" what items a certain object holds.
So I've got to an idea where I can enter the Item ID's right there like {22:4, id:amount}. When I parse the map, I retrieve that array as a string, is there a way to convert it to an array?
Thanks in advance!
Firstly, you probably want a Map, not an array or a List.
Map<String,String> processParams(String list) {
Map<String,String> = new HashMap<String,String>();
int openBracket = list.indexOf("{");
int closeBracket = list.lastIndexOf("}");
String params = list.substring(openBracket+1,closeBracket);
String paramList = params.split(",");
for(String param: paramList) {
String pData = param.trim().split(":");
map.put(param[0].trim(),param[1].trim());
}
return map;
}
processParams("{22:4, id:amount}");
Of course, it's actually a JSON-like structure so there's probably pre-existing parsers.