Have complication with ArrayList of ArrayList [closed] - java
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.
Related
Java Remove ArrayList by Value [closed]
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.
Removing all values from HashMap but keeping the keys? [closed]
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
creating arrayList of arraylists [closed]
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 was trying to create an ArrayList of ArrayLits. Then, whenever I added an ArrayList to my initial ArrayList, I get My 2nd ArrayList in the initial ArrayList copied to the first ArrayList in the initial ArrayList. Here is my Code. Thank you for any help. ArrayList<ArrayList> msebi = new ArrayList<ArrayList>(); ArrayList<Pair> temp = new ArrayList<Pair>(); Stack<ArrayList> arrayListStack = new Stack<ArrayList>(); Pair pair = null; for(int i=1;i<finalList.size()-1;i++){ pair = finalList.get(i); if(finalList.get(i+1).getDepth() > finalList.get(i).getDepth()){ temp.add(pair); if(i == finalList.size()-2){ temp.add(finalList.get(i+1)); msebi.add(temp); } else { temp.add(pair); msebi.add(temp); temp.clear(); } } Kindly Note that I have created another Class that is Named Pair. This Pair consist of a String and depth, and thus we have getDepth(); Also, I have in the main class ArrayList finalList = new ArrayList(); Thus, Whenever I am trying to copy the content of the ArrayLists within the msebi ArrayList, I got the values of the second arraylist the same as the first one.
When you add your ArrayList called temp to your outer ArrayList called msebi, msebi contains a reference to temp, not a copy. Then you immediately clear() temp and build it up again. You have multiple references to the same temp list in msebi. Create a new ArrayList instead of clearing the old one. This way, when a list is added, it's left alone, because you will be operating on another list object.
how to compare the key and the value of a hashmap [closed]
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 want to compare if the value is contained in the name in my hashmap, I want to be able to do this for all of my items in my hashmap. I have already populated my hashmap and just want to compare if if the value is contained in the name. I currently have Map<String, String> barcodeMap = Maps.newHashMap(); while ((nextLine = reader.readNext()) != null) { barcodeMap.put(nextLine[1], nextLine[0]); } I want to compare for example if nextLine[0] is abc and nextLine[1] is abc123, I want to compare if abc is in abc123 and if it is then make nextLine[1] abc
try this Map map = ... for(Entry e : map.entrySet) { Object k = e.getKey(); Object v = e.getValue(); ... compare }
Use containsValue() method to validate the value object present in the map. http://docs.oracle.com/javase/7/docs/api/java/util/Map.html#containsValue%28java.lang.Object%29 map.containsValue(value);
Retrieve ArrayLIst which is a value in HashMap [closed]
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 have a HashMap HashMap<Integer, ArrayList<Bundle>> hashMap I am able to put values in the HashMap . And the hashmap is passed to the next class . Now how to get the ArrayList of Bundle from HashMap. Any how I am using Iterator Set . Still I am not able to list the values of HashMap which is an arraylist. This is actually am Message picker . And I have multiselection mode on using callback for listview. When I click on a sms list , I need to get some details and store it in a hashmap . There might be chance that a sms can have multiple recepinets. The key will be position . I need to take all the values of the HashMap that is ArrayList into one single ArrayList. Say int the 0th position we have [0,[A,B]] ,[1,[C,D]] , i want to create a new arraylist and store A , B, C, D in it.
Here you have a complete example: public class Test { private final String value; public Test(String value) { this.value = value; } public static void main(String[] args) { HashMap<Integer, ArrayList<Test>> example = new HashMap<Integer, ArrayList<Test>>(); ArrayList<Test> test1 = new ArrayList<Test>(); test1.add(new Test("HELLO")); ArrayList<Test> test2 = new ArrayList<Test>(); test2.add(new Test("HELLO2")); example.put(1, test1); example.put(2, test2); // We get the second arraylist // Where 2 is the Integer we added in example.put(2, test2); ArrayList<Test> testExtracted = example.get(2); System.out.println(testExtracted.get(0).value); // Prints HELLO2 } }
you can try like that ArrayList<Bundle> value = hashMap.get(yourkey);