0

I have stored HashMap into an ArrayList and now I want each position of ArrayList in different array so please can somebody tell me how it is possible?

My array list is like this:

{key:value, key:value, key:value, key:value}, 
{key:value, key:value, key:value, key:value}, 
{key:value, key:value, key:value, key:value} 
Simson
  • 3,373
  • 2
  • 24
  • 38
  • What is your expected output? – Amit Bera Sep 24 '19 at 09:09
  • 3
    Welcome to Stack Overflow! Please turn to the [help] to learn how/what to ask here. Just dropping requirements "this is what I want" isn't appreciated. When you try something yourself, and you get stuck with a specific problem, we will gladly help. But please understand that this place is not intended to give guidance with the possibly many steps required to get you from your vision to a working program. And why exactly are you tagging with "weakhashmap"? – GhostCat Sep 24 '19 at 09:10
  • You mean to say you have `List>>`? And you need to convert it to `List`, right? – Mushif Ali Nawaz Sep 24 '19 at 09:11
  • My expected output is i want each position in different array like, array1= [ key:value, key:value, key:value, key:value] Arry 2 = [key:value, key:value, key:value, key:value] Arry3 = [key:value, key:value, key:value, key:value] – Vishal Parekh Sep 24 '19 at 09:16
  • Yes Mushif i have..... Arraylist > – Vishal Parekh Sep 24 '19 at 09:18
  • @VishalParekh What do you mean by each position in different array? If your `ArrayList>` contains 2 Maps with 2 key and values then what would be the output? Can you explain it in your question? – Mushif Ali Nawaz Sep 24 '19 at 09:22
  • You're looking for `array1`, `array2`, etc., to be arrays of map entries (e.g., `Map.Entry[]`), then? – Kevin Anderson Sep 24 '19 at 09:42

1 Answers1

0

Assuming the map contains String as key and value. In case different replace datatypes. You can achieve this by:

Entry<String, String>[] array = list.get(0).entrySet().stream().collect(Collectors.toList()).toArray((Map.Entry<String, String>[])new Map.Entry [list.size()]);
Gaurav Gupta
  • 142
  • 7
  • There is no point in performing `.collect(Collectors.toList())`, just to call `toArray` on the result. You can invoke `toArray` on the Stream, but in fact, the Stream operation is obsolete as well. You can invoke `toArray` directly on the `entrySet()`, copying the data only one time instead of three times. – Holger Sep 25 '19 at 08:49