I have a String,
我有一个字符串,
String value = "Sunday - H15,Sunday - H03,Sunday - H13,Sunday - H01,Sunday - H05,Friday - H23,Saturday - H05,Monday - H16,Monday - H17,Monday - H18,Monday - H19,Monday - H20";
In this String we have Days(Sunday, Monday etc..) and the Alphanumneric (H15, H03 etc..) is hours.
在这个字符串中我们有Days(星期日,星期一等等)和Alphanumneric(H15,H03等......)是小时。
Now my requirement is:
现在我的要求是:
I want a Map<String, List<String>>
in which Key should be Day and Value should be the list of Hours which has same day.
我想要一个Map
Eg:
Sunday : [H15,H03,H13,H01,H05]
Monday : [H16,H17,H18,H19,H20]
Friday : [H23]
Saturday : [H05]
Approach:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DayPartProcessingTest {
public static void main(String[] args) {
String propertyValue = "Sunday - H15,Sunday - H03,Sunday - H13,Sunday - H01,Sunday - H05,Friday - H23,Saturday - H05,Monday - H16,Monday - H17,Monday - H18,Monday - H19,Monday - H20";
Map<String, List<String>> map = new HashMap<String, List<String>>();
String[] dayParts = propertyValue.split(",");
for (String string : dayParts) {
String[] array = string.split(" - ");
if (map.get(array[0]) == null) {
List<String> list = new ArrayList<>();
list.add(array[1]);
map.put(array[0], list);
} else {
List<String> tempList = map.get(array[0]);
tempList.add(array[1]);
map.put(array[0], tempList);
}
}
System.out.println(map);
}
}
This is working fine and as required but is there any other possible way. I could not think any other :(
这工作正常,并根据需要,但有任何其他可能的方式。我想不出任何其他:(
4 个解决方案
#1
1
An alternative solution would be to use a Java 8 Stream:
另一种解决方案是使用Java 8 Stream:
Map<String, List<String>> collect = Arrays.stream(value.split(","))
.collect(Collectors.groupingBy(s -> s.split(" - ")[0],
Collectors.mapping(s -> s.split(" - ")[1], Collectors.toList())));
Arrays.stream(value.split(","))
will create the stream for the values splitted by ,
.
The groupingBy
Collector will group your stream by the given key (in this case the days).
The Collectors.mapping
will transform your values for the values of the map.
The Collectors.toList
will then collect the transformed values into a list.
Arrays.stream(value.split(“,”))将为分割的值创建流。 groupingBy Collector将按给定键(在本例中为日期)对您的流进行分组。 Collectors.mapping将转换您的地图值的值。然后Collectors.toList会将转换后的值收集到列表中。
#2
1
Try this.
String propertyValue = "Sunday - H15,Sunday - H03,Sunday - H13,Sunday - H01,Sunday - H05,Friday - H23,Saturday - H05,Monday - H16,Monday - H17,Monday - H18,Monday - H19,Monday - H20";
Map<String, List<String>> map = new HashMap<String, List<String>>();
String[] dayParts = propertyValue.split(",");
for (String string : dayParts) {
String[] array = string.split(" - ");
map.computeIfAbsent(array[0], k -> new ArrayList<>()).add(array[1]);
}
System.out.println(map);
Or
String propertyValue = "Sunday - H15,Sunday - H03,Sunday - H13,Sunday - H01,Sunday - H05,Friday - H23,Saturday - H05,Monday - H16,Monday - H17,Monday - H18,Monday - H19,Monday - H20";
Map<String, List<String>> map = Arrays.stream(propertyValue.split(","))
.map(e -> e.split(" - "))
.collect(Collectors.groupingBy(
a -> a[0],
Collectors.mapping(a -> a[1], Collectors.toList())));
#3
0
Your code can be cleaned up a bit like so
您的代码可以清理一下
for (String string : dayParts) {
String[] array = string.split(" - ");
List<String> list = map.get(array[0]);
if(list == null) {
list = new ArrayList<String>();
}
list.add(array[1]);
map.put(array[0], list);
}
I did not use the containsKey(..)
/get(..)
combination here, since in potential case of concurrent access to the map, there can be a race condition.
我没有在这里使用containsKey(..)/ get(..)组合,因为在潜在的并发访问地图的情况下,可能存在竞争条件。
#4
0
You could use containsKey
instead of map.get(array[0]) == null
. You can also reduce the number of actions when adding to the map
您可以使用containsKey而不是map.get(array [0])== null。您还可以在添加到地图时减少操作数量
if (!map.containsKey(array[0])) {
map.put(array[0], new ArrayList<String>());
}
map.get(array[0]).add(array[1]);
#1
1
An alternative solution would be to use a Java 8 Stream:
另一种解决方案是使用Java 8 Stream:
Map<String, List<String>> collect = Arrays.stream(value.split(","))
.collect(Collectors.groupingBy(s -> s.split(" - ")[0],
Collectors.mapping(s -> s.split(" - ")[1], Collectors.toList())));
Arrays.stream(value.split(","))
will create the stream for the values splitted by ,
.
The groupingBy
Collector will group your stream by the given key (in this case the days).
The Collectors.mapping
will transform your values for the values of the map.
The Collectors.toList
will then collect the transformed values into a list.
Arrays.stream(value.split(“,”))将为分割的值创建流。 groupingBy Collector将按给定键(在本例中为日期)对您的流进行分组。 Collectors.mapping将转换您的地图值的值。然后Collectors.toList会将转换后的值收集到列表中。
#2
1
Try this.
String propertyValue = "Sunday - H15,Sunday - H03,Sunday - H13,Sunday - H01,Sunday - H05,Friday - H23,Saturday - H05,Monday - H16,Monday - H17,Monday - H18,Monday - H19,Monday - H20";
Map<String, List<String>> map = new HashMap<String, List<String>>();
String[] dayParts = propertyValue.split(",");
for (String string : dayParts) {
String[] array = string.split(" - ");
map.computeIfAbsent(array[0], k -> new ArrayList<>()).add(array[1]);
}
System.out.println(map);
Or
String propertyValue = "Sunday - H15,Sunday - H03,Sunday - H13,Sunday - H01,Sunday - H05,Friday - H23,Saturday - H05,Monday - H16,Monday - H17,Monday - H18,Monday - H19,Monday - H20";
Map<String, List<String>> map = Arrays.stream(propertyValue.split(","))
.map(e -> e.split(" - "))
.collect(Collectors.groupingBy(
a -> a[0],
Collectors.mapping(a -> a[1], Collectors.toList())));
#3
0
Your code can be cleaned up a bit like so
您的代码可以清理一下
for (String string : dayParts) {
String[] array = string.split(" - ");
List<String> list = map.get(array[0]);
if(list == null) {
list = new ArrayList<String>();
}
list.add(array[1]);
map.put(array[0], list);
}
I did not use the containsKey(..)
/get(..)
combination here, since in potential case of concurrent access to the map, there can be a race condition.
我没有在这里使用containsKey(..)/ get(..)组合,因为在潜在的并发访问地图的情况下,可能存在竞争条件。
#4
0
You could use containsKey
instead of map.get(array[0]) == null
. You can also reduce the number of actions when adding to the map
您可以使用containsKey而不是map.get(array [0])== null。您还可以在添加到地图时减少操作数量
if (!map.containsKey(array[0])) {
map.put(array[0], new ArrayList<String>());
}
map.get(array[0]).add(array[1]);