-2

I want to transform a list of String to a map, where the key of map is a simple increment.

For example:

List<String> result = new ArrayList<String>();
result.add("hello");
result.add("Java");

Pretend result:

Map<Integer, String> mapOfList;
map(1, "Hello");
map(2, "Java");

Try:

AtomicInteger atomic=new AtomicInteger(0);
mapOfList=result.stream().collect(atomic.incrementAndGet(), s -> s);
Goldbones
  • 1,407
  • 3
  • 21
  • 55
  • 1
    Your question is unclear. Can you elaborate on it? Also, create some examples. And don't confuse arrays with lists. An `ArrayList` has nothing to do with an array, both are completely different structures. – Zabuzard Aug 06 '18 at 13:21
  • 1
    What is unclear? What examples more you want? – Goldbones Aug 06 '18 at 13:21
  • Your question is unclear. I have no idea what you are asking for. – Zabuzard Aug 06 '18 at 13:22
  • 1
    You cannot do that without an iteration. Even existing functions that fill an array use iteration internally. – f1sh Aug 06 '18 at 13:22
  • What does it mean for an element to be empty? Do you mean null? Empty string? – Brandon Aug 06 '18 at 13:23
  • Yep, this is not a possible N(1) approach... – Mr. Polywhirl Aug 06 '18 at 13:23
  • Thanks @jhamon . It´s what I needed – Goldbones Aug 06 '18 at 13:24
  • Assuming you understand that this is an `ArrayList` and not an array, you can empty the entire list using `ArrayList.clear()`. Maybe that is what you are trying to do? It's really tough to understand what you are trying to do. Your words are confusing. You are saying both "fill" and "empty" – Brandon Aug 06 '18 at 13:24
  • Pulling another thread: you asked how to do this to each element without iteration. When doing something repetitively, the alternative to iteration is recursion. Using recursion to walk sequentially over an array though is strange. – Brandon Aug 06 '18 at 13:26
  • @Brandon, it´s different empty from null – Goldbones Aug 06 '18 at 13:27
  • @Brandon what I mean was visually. Instead of making something like for(...) – Goldbones Aug 06 '18 at 13:29

1 Answers1

2

You need to iterate. Here's one-line using an int stream:

IntStream.range(0, fillMyList.size()).forEach(i -> fillMyList.set(i, ""));
ernest_k
  • 44,416
  • 5
  • 53
  • 99