목록을 지도로 변환하는 방법?
최근에 나는 동료와 어떤 것이 가장 최적의 전환 방법이 될 것인가에 대해 대화를 나눈다.List
로Map
자바에서, 그리고 그렇게 함으로써 얻을 수 있는 특별한 이점이 있다면.
나는 최적의 전환 방식을 알고 싶고, 누군가 나를 안내해 줄 수 있다면 정말 고맙겠다.
이 좋은 접근법인가?
List<Object[]> results;
Map<Integer, String> resultsMap = new HashMap<Integer, String>();
for (Object[] o : results) {
resultsMap.put((Integer) o[0], (String) o[1]);
}
자바-8을 사용하면 스트림과 클래스를 사용하여 한 줄로 이 작업을 수행할 수 있다.
Map<String, Item> map =
list.stream().collect(Collectors.toMap(Item::getKey, item -> item));
짧은 데모:
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Test{
public static void main (String [] args){
List<Item> list = IntStream.rangeClosed(1, 4)
.mapToObj(Item::new)
.collect(Collectors.toList()); //[Item [i=1], Item [i=2], Item [i=3], Item [i=4]]
Map<String, Item> map =
list.stream().collect(Collectors.toMap(Item::getKey, item -> item));
map.forEach((k, v) -> System.out.println(k + " => " + v));
}
}
class Item {
private final int i;
public Item(int i){
this.i = i;
}
public String getKey(){
return "Key-"+i;
}
@Override
public String toString() {
return "Item [i=" + i + "]";
}
}
출력:
Key-1 => Item [i=1]
Key-2 => Item [i=2]
Key-3 => Item [i=3]
Key-4 => Item [i=4]
코멘트에 언급된 바와 같이, 다음을 사용할 수 있다.Function.identity()
대신에item -> item
비록 내가 찾아냈지만i -> i
다소 노골적인
그리고 당신의 기능이 편향적이지 않을 경우 이진 연산자를 사용할 수 있다는 것을 완전히 명심하라.예를 들어 이것을 생각해 보자.List
int 값에 대해 intu의 결과를 계산하는 매핑 함수 3:
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6);
Map<String, Integer> map =
intList.stream().collect(toMap(i -> String.valueOf(i % 3), i -> i));
이 코드를 실행할 때 다음과 같은 오류가 표시됨java.lang.IllegalStateException: Duplicate key 1
1%이 4% 에 키 매핑 1% 3은 4% 3과 같기 때문에 키 매핑 기능이 주어진 키 값이 같기 때문이다.이 경우 병합 연산자를 제공할 수 있다.
여기 값을 합친 것이 있다.(i1, i2) -> i1 + i2;
은 방법 될 수 있는 이다.Integer::sum
.
Map<String, Integer> map =
intList.stream().collect(toMap(i -> String.valueOf(i % 3),
i -> i,
Integer::sum));
이제 출력:
0 => 9 (i.e 3 + 6)
1 => 5 (i.e 1 + 4)
2 => 7 (i.e 2 + 5)
도움이 되길! :)
List<Item> list;
Map<Key,Item> map = new HashMap<Key,Item>();
for (Item i : list) map.put(i.getKey(),i);
물론 각 항목에 다음 항목이 있다고 가정할 때getKey()
적절한 유형의 키를 반환하는 방법
이 질문이 중복으로 닫히지 않은 경우 Google 컬렉션을 사용하는 것이 정답:
Map<String,Role> mappedRoles = Maps.uniqueIndex(yourList, new Function<Role,String>() {
public String apply(Role from) {
return from.getName(); // or something else
}});
짧고 달콤하다.
Java 8을 사용하여 다음을 수행할 수 있다.
Map<Key, Value> result= results
.stream()
.collect(Collectors.toMap(Value::getName,Function.identity()));
Value
당신이 사용하는 어떤 물체가 될 수 있다.
자바 8 이후, @ZouZou가 the을 사용한 대답.Collectors.toMap
수집가는 확실히 이 문제를 해결하는 관용적인 방법이다.
그리고 이것은 매우 흔한 일이므로, 우리는 그것을 정적 유틸리티로 만들 수 있다.
그렇게 해서 해결책은 정말로 원라이너가 된다.
/**
* Returns a map where each entry is an item of {@code list} mapped by the
* key produced by applying {@code mapper} to the item.
*
* @param list the list to map
* @param mapper the function to produce the key from a list item
* @return the resulting map
* @throws IllegalStateException on duplicate key
*/
public static <K, T> Map<K, T> toMapBy(List<T> list,
Function<? super T, ? extends K> mapper) {
return list.stream().collect(Collectors.toMap(mapper, Function.identity()));
}
그리고 여기에 당신이 그것을 사용하는 방법이 있다.List<Student>
:
Map<Long, Student> studentsById = toMapBy(students, Student::getId);
알렉시스는 이미 자바 8에 메소드를 이용한 답을 올렸다.toMap(keyMapper, valueMapper)
. 이 방법 구현에 대한 설명에 따라:
반환된 맵의 유형, 변이성, 연속성 또는 스레드 안전성에 대한 보장이 없다.
그래서 만약 우리가 의 구체적인 구현에 관심이 있다면Map
인터페이스(예: HashMap
그러면 과부하된 양식을 다음과 같이 사용할 수 있다.
Map<String, Item> map2 =
itemList.stream().collect(Collectors.toMap(Item::getKey, //key for map
Function.identity(), // value for map
(o,n) -> o, // merge function in case of conflict with keys
HashMap::new)); // map factory - we want HashMap and not any Map implementation
둘 중 하나를 사용하더라도Function.identity()
또는i->i
괜찮은데 그런 것 같다.Function.identity()
대신에i -> i
관련 답변에 따라 메모리를 절약할 수 있다.
A List
그리고Map
개념적으로 다르다.List
주문된 품목 모음입니다.항목에는 중복 항목이 포함될 수 있으며, 항목에는 고유 식별자(키) 개념이 없을 수 있다.Map
키에 매된된. 는 오직 수 각각의 키는 오직 하나의 값만을 가리킬 수 있다.
그러므로, 당신의 생각에 따라.List
의 항목으로 변환할 수 있거나 그렇지 않을 수 있음Map
의 .가 당신의 것을 ?List
의 물건에는 중복이 없다?각 품목에 고유한 키가 있는가?만약 그렇다면, 그것들을 안에 넣는 것은 가능하다.Map
.
또한 Google guava 라이브러리의 Maps.uniqueIndex(...)를 사용하여 간단한 방법으로 이 작업을 수행할 수 있다.
범용법
public static <K, V> Map<K, V> listAsMap(Collection<V> sourceList, ListToMapConverter<K, V> converter) {
Map<K, V> newMap = new HashMap<K, V>();
for (V item : sourceList) {
newMap.put( converter.getKey(item), item );
}
return newMap;
}
public static interface ListToMapConverter<K, V> {
public K getKey(V item);
}
Java-8 스트림 사용
Map<Integer, String> map = results.stream().collect(Collectors.toMap(e -> ((Integer) e[0]), e -> (String) e[1]));
Java-8이 없다면, 당신은 이것을 한 줄의 Commons 컬렉션과 Closure 클래스에서 할 수 있을 것이다.
List<Item> list;
@SuppressWarnings("unchecked")
Map<Key, Item> map = new HashMap<Key, Item>>(){{
CollectionUtils.forAllDo(list, new Closure() {
@Override
public void execute(Object input) {
Item item = (Item) input;
put(i.getKey(), item);
}
});
}};
이미 말한 바와 같이, Java-8에서는 Collectors의 간결한 해결책을 가지고 있다.
list.stream().collect(
groupingBy(Item::getKey)
)
또한 다른 groupingBy 방법을 통과하는 여러 그룹을 두 번째 매개 변수로 중첩할 수 있다.
list.stream().collect(
groupingBy(Item::getKey, groupingBy(Item::getOtherKey))
)
이런 식으로, 우리는 다음과 같은 다단계 지도를 가지게 될 것이다.Map<key, Map<key, List<Item>>>
무엇을 달성하고자 하는가에 따라 많은 해결책이 떠오른다.
모든 목록 항목이 키 및 값임
for( Object o : list ) {
map.put(o,o);
}
목록 요소에는 이름을 찾을 수 있는 것이 있다.
for( MyObject o : list ) {
map.put(o.name,o);
}
목록 요소들은 그것들을 찾아 볼 것이 있고, 그들이 독특하다는 보장은 없다.Gogles MultiMaps 사용
for( MyObject o : list ) {
multimap.put(o.name,o);
}
모든 요소에게 키로 위치 부여:
for( int i=0; i<list.size; i++ ) {
map.put(i,list.get(i));
}
...
그것은 정말로 당신이 무엇을 성취하고 싶은지에 달려있다.
예제에서 볼 수 있듯이, 지도는 키에서 값까지의 매핑인 반면, 목록은 각각 위치를 갖는 일련의 요소일 뿐이다.그래서 그것들은 자동적으로 전환되지 않는다.
여기 내가 정확히 이 목적을 위해 쓴 작은 방법이 있다.Apache Commons의 Validate를 사용한다.
얼마든지 쓰세요.
/**
* Converts a <code>List</code> to a map. One of the methods of the list is called to retrive
* the value of the key to be used and the object itself from the list entry is used as the
* objct. An empty <code>Map</code> is returned upon null input.
* Reflection is used to retrieve the key from the object instance and method name passed in.
*
* @param <K> The type of the key to be used in the map
* @param <V> The type of value to be used in the map and the type of the elements in the
* collection
* @param coll The collection to be converted.
* @param keyType The class of key
* @param valueType The class of the value
* @param keyMethodName The method name to call on each instance in the collection to retrieve
* the key
* @return A map of key to value instances
* @throws IllegalArgumentException if any of the other paremeters are invalid.
*/
public static <K, V> Map<K, V> asMap(final java.util.Collection<V> coll,
final Class<K> keyType,
final Class<V> valueType,
final String keyMethodName) {
final HashMap<K, V> map = new HashMap<K, V>();
Method method = null;
if (isEmpty(coll)) return map;
notNull(keyType, Messages.getString(KEY_TYPE_NOT_NULL));
notNull(valueType, Messages.getString(VALUE_TYPE_NOT_NULL));
notEmpty(keyMethodName, Messages.getString(KEY_METHOD_NAME_NOT_NULL));
try {
// return the Method to invoke to get the key for the map
method = valueType.getMethod(keyMethodName);
}
catch (final NoSuchMethodException e) {
final String message =
String.format(
Messages.getString(METHOD_NOT_FOUND),
keyMethodName,
valueType);
e.fillInStackTrace();
logger.error(message, e);
throw new IllegalArgumentException(message, e);
}
try {
for (final V value : coll) {
Object object;
object = method.invoke(value);
@SuppressWarnings("unchecked")
final K key = (K) object;
map.put(key, value);
}
}
catch (final Exception e) {
final String message =
String.format(
Messages.getString(METHOD_CALL_FAILED),
method,
valueType);
e.fillInStackTrace();
logger.error(message, e);
throw new IllegalArgumentException(message, e);
}
return map;
}
자바 8 를 한다.List<?>
사물을 a로 만들다.Map<k, v>
:
List<Hosting> list = new ArrayList<>();
list.add(new Hosting(1, "liquidweb.com", new Date()));
list.add(new Hosting(2, "linode.com", new Date()));
list.add(new Hosting(3, "digitalocean.com", new Date()));
//example 1
Map<Integer, String> result1 = list.stream().collect(
Collectors.toMap(Hosting::getId, Hosting::getName));
System.out.println("Result 1 : " + result1);
//example 2
Map<Integer, String> result2 = list.stream().collect(
Collectors.toMap(x -> x.getId(), x -> x.getName()));
복사한 코드:
https://https:///www.mkyong.com/java8/java-8-convert-list-to-map/
Java 8의 스트림 API를 활용할 수 있다.
public class ListToMap {
public static void main(String[] args) {
List<User> items = Arrays.asList(new User("One"), new User("Two"), new User("Three"));
Map<String, User> map = createHashMap(items);
for(String key : map.keySet()) {
System.out.println(key +" : "+map.get(key));
}
}
public static Map<String, User> createHashMap(List<User> items) {
Map<String, User> map = items.stream().collect(Collectors.toMap(User::getId, Function.identity()));
return map;
}
}
자세한 내용은 http://codecramp.com/java-8-streams-api-convert-list-map/를 참조하십시오.
캉고_V의 대답은 좋지만 너무 복잡한 것 같아.나는 이것이 더 단순하다고 생각한다 - 어쩌면 너무 단순할지도 모른다.기울어진 경우 문자열을 일반 마커로 교체하고 모든 키 유형에 대해 사용할 수 있도록 할 수 있다.
public static <E> Map<String, E> convertListToMap(Collection<E> sourceList, ListToMapConverterInterface<E> converterInterface) {
Map<String, E> newMap = new HashMap<String, E>();
for( E item : sourceList ) {
newMap.put( converterInterface.getKeyForItem( item ), item );
}
return newMap;
}
public interface ListToMapConverterInterface<E> {
public String getKeyForItem(E item);
}
다음과 같이 사용됨:
Map<String, PricingPlanAttribute> pricingPlanAttributeMap = convertListToMap( pricingPlanAttributeList,
new ListToMapConverterInterface<PricingPlanAttribute>() {
@Override
public String getKeyForItem(PricingPlanAttribute item) {
return item.getFullName();
}
} );
Apache Commons MapUtils.fumulateMap
Java 8을 사용하지 않고 어떤 이유로든 명시적 루프를 사용하지 않으려면,MapUtils.populateMap
아파치 커먼즈 출신이야
리스트가 있다고 해Pair
s
List<ImmutablePair<String, String>> pairs = ImmutableList.of(
new ImmutablePair<>("A", "aaa"),
new ImmutablePair<>("B", "bbb")
);
그리고 당신은 이제 지도 하나를 원한다.Pair
의 핵심Pair
반대하다
Map<String, Pair<String, String>> map = new HashMap<>();
MapUtils.populateMap(map, pairs, new Transformer<Pair<String, String>, String>() {
@Override
public String transform(Pair<String, String> input) {
return input.getKey();
}
});
System.out.println(map);
출력:
{A=(A,aaa), B=(B,bbb)}
라고 하는 말이다.for
루프는 아마 이해하기 더 쉬울 거야(아래 내용은 동일한 출력을 제공함):
Map<String, Pair<String, String>> map = new HashMap<>();
for (Pair<String, String> pair : pairs) {
map.put(pair.getKey(), pair);
}
System.out.println(map);
참조URL: https://stackoverflow.com/questions/4138364/how-to-convert-list-to-map
'programing' 카테고리의 다른 글
excellent_text는 어떻게 작동하는가? (0) | 2022.04.17 |
---|---|
모든 작업의 이름을 기억하지 않고 vuex 디스패치를 호출하고 디스패치에서 문자열로 보내는 방법 (0) | 2022.04.17 |
Nuxt.js / Vuex - mapActions 도우미의 네임스페이스 모듈 사용, [vuex] 알 수 없는 작업 유형: FETCH_LABel (0) | 2022.04.17 |
두 REST 리소스의 결과를 vue-다중 선택 항목으로 그룹화된 항목과 결합 (0) | 2022.04.17 |
Vue에서 JS와 SCSS 사이의 변수 공유 (0) | 2022.04.17 |