Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
public List testList(List numberList) {
List realList = new ArrayList();
for (Iterator iterator = numberList.iterator(); iterator.hasNext();) {
String num = (String) iterator.next();
realList.add(call(num));
}
return realList;
}
I want to write this code as a Java stream type.
I think you nead that:
numberList.stream()
.map(arg-> arg.toString())
.map(arg -> call(arg))
.collect(Collectors.toList());
public List testList(List numberList) {
return numberList.stream()
.map(s -> call((String) s))
.collect(Collectors.toList());
}
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 8 years ago.
Improve this question
I have a ArrayList with integer value and I want to read this value from another ArrayList consist string value. How can I do that ?
public static ArrayList<Integer> scorerValue = new ArrayList<Integer>();
and I want read here using
ArrayList<String> score_Number ;
Okay. So let's first declare our List<String>..
List<String> score_Number = new ArrayList<String>();
// Remember you program to an interface. Not a concrete implementation.
Then to convert you..
// Do some magic.
That's right Magic. Magic involving the toString() method. You will need to call toString() in a for each loop. Finally, you will need to use the List.add() method, to add the newly created String values to your list score_Number.
NOTE: Follow the naming conventions. score_Number should be scoreNumber.
Try,
ArrayList<Integer> integerList = new ArrayList<Integer>();
// add Integer Elements
ArrayList<String> stringList = new ArrayList<String>();
for(Integer i : integerList){
stringList.add(i+"");
}
try this:
ArrayList<Integer> scorerValue = new ArrayList<Integer>();
scorerValue.add(1);
ArrayList<String> score_Number = new ArrayList<String>();
for (Integer i : scorerValue)
score_Number.add(i.toString());
System.out.println(score_Number);
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.