This question already has answers here:
How can I initialize an ArrayList with all zeroes in Java?
(5 answers)
Closed 4 years ago.
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);
You need to iterate. Here's one-line using an int stream:
IntStream.range(0, fillMyList.size()).forEach(i -> fillMyList.set(i, ""));
Related
This question already has answers here:
Find all indexes of a value in a List [duplicate]
(3 answers)
Closed 12 days ago.
I have a list of strings and I want to add to a set all indexes from array where the string is not empty,
I tried doing this:
columnNum.addAll((Collection<? extends Integer>) IntStream.range(0, row.size()).filter(i-> StringUtils.isNotEmpty(row.get(i))));
but I get an exception
You have to use boxed:
var list = List.of("","a","","b");
var set = IntStream.range(0, list.size())
.filter(i ->
!list.get(i).isEmpty()).boxed().collect(Collectors.toSet());
Collect the stream to a List first. An IntStream is not a Collection.
columnNum.addAll(IntStream.range(0, row.size())
.filter(i-> StringUtils.isNotEmpty(row.get(i)))
.boxed().collect(Collectors.toList())); // or .toList() with Java 16+
This question already has answers here:
Zip two lists into an immutable multimap in Java 8 with Guava?
(3 answers)
Closed 2 years ago.
I have two streams
Stream<Key> keys;
Stream<Value> values;
I want combine them into a single Map
Map<Key, Value> result = someMagicMethod(keys, values);
Is there any elegant way to do that?
I know there is a method called Stream.concat, but it's not for this case.
Guava Streams.zip for streams without random access
If Guava is available at runtime, then the following can help:
List<String> keys = Arrays.asList("One", "Two");
List<Integer> values = Arrays.asList(1, 2);
Map<String, Integer> zipped = Streams.zip(keys.stream(), values.stream(), SimpleEntry::new)
.collect(Collectors.toMap(Collectors.toMap(Entry::getKey, Entry::getValue)));
System.out.println(zipped);
You can first collect both streams to a List.
List<Key> keyList = keys.collect(Collectors.toList());
List<Value> valueList = values.collect(Collectors.toList());
Map<Key, Value> map = IntStream.range(0, keyList.size())
.boxed().collect(Collectors.toMap(keyList::get, valueList::get));
System.out.println(map);
This question already has answers here:
How can I count occurrences with groupBy?
(6 answers)
Closed 2 years ago.
How to get an element of an array that occur multiple times?
ArrayList<String> arrBarCode = new ArrayList<String>();
arrBarCode.add(BarCode);
Use a Map<String, Integer> to remember the number of occurrences while iterating over the List:
ArrayList<String> arrBarCode = new ArrayList<String>();
arrBarCode.add("a");
arrBarCode.add("a");
arrBarCode.add("a");
arrBarCode.add("c");
Map<String, Integer> a = new HashMap<>();
arrBarCode.forEach(s -> a.put(s, a.computeIfAbsent(s, foo -> 0)+1));
System.out.println(a);
prints
{a=3, c=1}
This question already has answers here:
Create only 1 list from a map where map value is list using JAVA 8 Streams
(3 answers)
Closed 4 years ago.
I have a
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
The type of value is a list of strings:
List<String> valueList = map.get('key');
How can i search through this map (through all the valueLists within this map) and get all the values which startsWith 'xy' in a list back?
I hope the question is clear.
I have tried this, but no success:
map
.entrySet()
.stream()
.filter(e-> e.getValue().stream().filter(value -> value.startsWith(searchString)))
.collect(Collectors.toList());
I get this error: Stream cannot be converted to boolean
If I understood your problem correctly:
map.values()
.stream()
.flatMap(List::stream)
.filter(x -> x.startsWith(searchString))
.collect(Collectors.toList())
This question already has answers here:
Convert List of List into list in java
(5 answers)
Closed 5 years ago.
I have a multimap Map<T,List<L>> map and I need a list with all the values of the values from the map, namely List<L>. With map.values() I get a List<List<L>>, but thats not what I want.
Does someone know a clean solution without looping?
If you are using Java 8, you could collect all L values from all List<L>s in a single List<L> by Stream#flatMap:
final List<L> list = map
// get a Collection<List<L>>
.values()
// make a stream from the collection
.stream()
// turn each List<L> into a Stream<L> and merge these streams
.flatMap(List::stream)
// accumulate the result into a List
.collect(Collectors.toList());
Otherwise, a for-each approach with Collection#addAll can be applied:
final List<L> list = new ArrayList<>();
for (final List<L> values : map.values()) {
list.addAll(values);
}