동일한 키 아래에 여러 값이있는 HashMap


199

하나의 키와 두 개의 값으로 HashMap을 구현할 수 있습니까? 해시 맵처럼?

하나의 키를 사용하여 세 가지 값의 저장을 구현하는 다른 방법을 알려 주면 (도움이 없다면) 도와주세요.



고마워 친구 ...하지만 MultiHashMap을 사용하는 데 약간의 제한이 있습니다
vidhya

답변:


266

당신은 할 수 있습니다 :

  1. 목록이있는 맵을 값으로 사용하십시오. Map<KeyType, List<ValueType>>.
  2. 새 랩퍼 클래스를 작성하고이 랩퍼의 인스턴스를 맵에 배치하십시오. Map<KeyType, WrapperType>.
  3. 클래스와 같은 튜플을 사용하십시오 (래퍼를 많이 만들어 저장). Map<KeyType, Tuple<Value1Type, Value2Type>>.
  4. 여러지도를 나란히 사용하십시오.

1. 목록을 값으로 사용하여 매핑

// create our map
Map<String, List<Person>> peopleByForename = new HashMap<>();    

// populate it
List<Person> people = new ArrayList<>();
people.add(new Person("Bob Smith"));
people.add(new Person("Bob Jones"));
peopleByForename.put("Bob", people);

// read from it
List<Person> bobs = peopleByForename["Bob"];
Person bob1 = bobs[0];
Person bob2 = bobs[1];

이 방법의 단점은 목록이 정확히 두 값에 바인딩되지 않는다는 것입니다.

2. 랩퍼 클래스 사용

// define our wrapper
class Wrapper {
    public Wrapper(Person person1, Person person2) {
       this.person1 = person1;
       this.person2 = person2;
    }

    public Person getPerson1 { return this.person1; }
    public Person getPerson2 { return this.person2; }

    private Person person1;
    private Person person2;
}

// create our map
Map<String, Wrapper> peopleByForename = new HashMap<>();

// populate it
Wrapper people = new Wrapper();
peopleByForename.put("Bob", new Wrapper(new Person("Bob Smith"),
                                        new Person("Bob Jones"));

// read from it
Wrapper bobs = peopleByForename.get("Bob");
Person bob1 = bobs.getPerson1;
Person bob2 = bobs.getPerson2;

이 접근 방식의 단점은 이러한 매우 간단한 컨테이너 클래스에 대해 많은 보일러 플레이트 코드를 작성해야한다는 것입니다.

3. 튜플 사용

// you'll have to write or download a Tuple class in Java, (.NET ships with one)

// create our map
Map<String, Tuple2<Person, Person> peopleByForename = new HashMap<>();

// populate it
peopleByForename.put("Bob", new Tuple2(new Person("Bob Smith",
                                       new Person("Bob Jones"));

// read from it
Tuple<Person, Person> bobs = peopleByForename["Bob"];
Person bob1 = bobs.Item1;
Person bob2 = bobs.Item2;

이것은 내 의견으로는 최고의 솔루션입니다.

4. 여러지도

// create our maps
Map<String, Person> firstPersonByForename = new HashMap<>();
Map<String, Person> secondPersonByForename = new HashMap<>();

// populate them
firstPersonByForename.put("Bob", new Person("Bob Smith"));
secondPersonByForename.put("Bob", new Person("Bob Jones"));

// read from them
Person bob1 = firstPersonByForename["Bob"];
Person bob2 = secondPersonByForename["Bob"];

이 솔루션의 단점은 두 맵이 관련되어 있다는 것이 확실하지 않으며 프로그래밍 오류로 인해 두 맵이 동기화되지 않은 것을 볼 수 있다는 것입니다.


안녕 폴 ... 어떻게하면 좀 더 명확하게 할 수 있습니까?
vidhya

@ vidhya : 특히 귀하의 문제에 맞는 것은 무엇입니까? 여러 개체가 같은 유형입니까, 다른가요?
Paul Ruane

실제로는 좋은 예입니다.
Xonatron

@Paul, # 3의 간단한 예제 코드 Map<KeyType, Tuple<Value1Type, Value2Type>>
Joarder Kamal

@ CoolMind 사람들이 실수를 해결할 수 있다고 확신합니다.
Paul Ruane

61

아닙니다 HashMap. 기본적으로 HashMap키부터 값 모음까지가 필요 합니다.

외부 라이브러리를 사용하고 싶다면 Guava 는 and와 Multimap같은 구현 에서 정확히이 개념을 사용합니다 .ArrayListMultimapHashMultimap


@Jon, OP가 요청한 위의 질문에 대해 Java로 작동하는 예제를 제공해 주시겠습니까? 게시 할 수 있으면 대단히 감사합니다
Deepak

2
@Deepak : 구아바 멀티 맵 예제를 검색하면 샘플 코드를 찾을 수 있습니다.
Jon Skeet

1
@ Deepak : 기본적으로 ArrayListMultimap자신 과 같은 것을 만들 거나 ... 또는 다른 것을 사용하십시오 HashMap<String, List<Integer>>. 기본적으로 값을 처음 추가 할 때마다 빈 목록을 만들어야합니다.
Jon Skeet

1
HashMap<String, List<Integer>>
Deepak

9
@Deepak : 예제를 직접 작성해 보시고 문제가 발생하면 가능한 한 코드를 포함하여 질문하십시오. 그런 식으로 더 많이 배울 수 있습니다.
Jon Skeet

23

또 다른 좋은 선택은 Apache Commons의 MultiValuedMap 을 사용하는 것 입니다. 특수 구현에 대해서는 페이지 상단의 알려진 모든 구현 클래스 를 살펴보십시오 .

예:

HashMap<K, ArrayList<String>> map = new HashMap<K, ArrayList<String>>()

로 대체 될 수있었습니다

MultiValuedMap<K, String> map = new MultiValuedHashMap<K, String>();

그래서,

map.put(key, "A");
map.put(key, "B");
map.put(key, "C");

Collection<String> coll = map.get(key);

coll"A", "B"및 "C"를 포함하는 컬렉션이 생성됩니다 .


13

Multimap구아바 라이브러리 및 구현을 살펴보십시오.HashMultimap

지도와 유사하지만 여러 값을 단일 키와 연관시킬 수있는 모음입니다. 키가 같지만 값이 다른 put (K, V)을 두 번 호출하면 멀티 맵에 키에서 두 값으로의 매핑이 포함됩니다.


7

Map<KeyType, Object[]>여러 값을 Map의 키와 연결하는 데 사용 합니다. 이 방법으로 키와 관련된 여러 유형의 여러 값을 저장할 수 있습니다. Object []에서 적절한 삽입 및 검색 순서를 유지하여주의해야합니다.

예 : 학생 정보를 저장하려고합니다. 키는 아이디이며 학생과 관련된 이름, 주소 및 이메일을 저장하고 싶습니다.

       //To make entry into Map
        Map<Integer, String[]> studenMap = new HashMap<Integer, String[]>();
        String[] studentInformationArray = new String[]{"name", "address", "email"};
        int studenId = 1;
        studenMap.put(studenId, studentInformationArray);

        //To retrieve values from Map
        String name = studenMap.get(studenId)[1];
        String address = studenMap.get(studenId)[2];
        String email = studenMap.get(studenId)[3];

1
나에게 이것은 최고의 답변입니다. 더 단순하고 간결하며 덜 추상적입니다.
Morey

6
HashMap<Integer,ArrayList<String>> map = new    HashMap<Integer,ArrayList<String>>();

ArrayList<String> list = new ArrayList<String>();
list.add("abc");
list.add("xyz");
map.put(100,list);

4

기록을 위해 순수한 JDK8 솔루션은 다음 Map::compute방법 을 사용 하는 것입니다.

map.compute(key, (s, strings) -> strings == null ? new ArrayList<>() : strings).add(value);

와 같은

public static void main(String[] args) {
    Map<String, List<String>> map = new HashMap<>();

    put(map, "first", "hello");
    put(map, "first", "foo");
    put(map, "bar", "foo");
    put(map, "first", "hello");

    map.forEach((s, strings) -> {
        System.out.print(s + ": ");
        System.out.println(strings.stream().collect(Collectors.joining(", ")));
    });
}

private static <KEY, VALUE> void put(Map<KEY, List<VALUE>> map, KEY key, VALUE value) {
    map.compute(key, (s, strings) -> strings == null ? new ArrayList<>() : strings).add(value);
}

출력 :

bar: foo
first: hello, foo, hello

경우에 일관성을 보장하기 위해 참고 여러 스레드는이 데이터 구조를 액세스 ConcurrentHashMapCopyOnWriteArrayList인스턴스 필요에 사용할 수 있습니다.


사용하는 것이 좋습니다 computeIfAbsent. map.computeIfAbsent(key, k -> new ArrayList<>()).add(value);
saka1029

3

Spring Framework 를 사용하는 경우 . 있습니다 : org.springframework.util.MultiValueMap.

수정 불가능한 다중 값 맵을 작성하려면 다음을 수행하십시오.

Map<String,List<String>> map = ...
MultiValueMap<String, String> multiValueMap = CollectionUtils.toMultiValueMap(map);

또는 사용 org.springframework.util.LinkedMultiValueMap


2

예, 아니오 해결책은 키에 해당하는 2 개 (3 개 이상) 값이 포함 된 값에 대해 래퍼 클래스를 작성하는 것입니다.



2

가장 쉬운 방법은 Google 컬렉션 라이브러리를 사용하는 것입니다.

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

public class Test {

    public static void main(final String[] args) {

        // multimap can handle one key with a list of values
        final Multimap<String, String> cars = ArrayListMultimap.create();
        cars.put("Nissan", "Qashqai");
        cars.put("Nissan", "Juke");
        cars.put("Bmw", "M3");
        cars.put("Bmw", "330E");
        cars.put("Bmw", "X6");
        cars.put("Bmw", "X5");

        cars.get("Bmw").forEach(System.out::println);

        // It will print the:
        // M3
        // 330E
        // X6
        // X5
    }

}

메이븐 링크 : https://mvnrepository.com/artifact/com.google.collections/google-collections/1.0-rc2

이에 대한 자세한 내용은 http://tomjefferys.blogspot.be/2011/09/multimaps-google-guava.html


1
String key= "services_servicename"

ArrayList<String> data;

for(int i = 0; i lessthen data.size(); i++) {
    HashMap<String, String> servicesNameHashmap = new HashMap<String, String>();
    servicesNameHashmap.put(key,data.get(i).getServiceName());
    mServiceNameArray.add(i,servicesNameHashmap);
}

최고의 결과를 얻었습니다.

당신은 단지 HashMap같은 새로운 것을 만들어야합니다

HashMap<String, String> servicesNameHashmap = new HashMap<String, String>();

당신의 for루프에서. 동일한 키 및 여러 값과 같은 효과가 있습니다.


1
 import java.io.*;
 import java.util.*;

 import com.google.common.collect.*;

 class finTech{
public static void main(String args[]){
       Multimap<String, String> multimap = ArrayListMultimap.create();
       multimap.put("1","11");
       multimap.put("1","14");
       multimap.put("1","12");
       multimap.put("1","13");
       multimap.put("11","111");
       multimap.put("12","121");
        System.out.println(multimap);
        System.out.println(multimap.get("11"));
   }                                                                                            
 }                                                                    

산출:

     {"1"=["11","12","13","14"],"11"=["111"],"12"=["121"]}

      ["111"]

유틸리티 기능을위한 Google-Guava 라이브러리입니다. 이것이 필수 솔루션입니다.


유효한 솔루션이며 여러 번이 방법을 사용했습니다.
letowianka

네 그것은 작동하지만 그것은 데이터를 [] 형식으로 표시하고 있습니다. 나는 그 항목을 하나씩 pls 메신저 여기에 붙어 얻는 방법을 원합니다
Sunil Chaudhary

0

Paul의 의견에 대한 답변을 게시 할 수 없으므로 Vidhya에 대한 새로운 의견을 작성 중입니다.

래퍼는 SuperClass값으로 저장하려는 두 클래스에 대한 것 입니다.

래퍼 클래스 내부에서 두 클래스 객체의 인스턴스 변수 객체로 연결을 넣을 수 있습니다.

예 :

class MyWrapper {

 Class1 class1obj = new Class1();
 Class2 class2obj = new Class2();
...
}

과에서 의 HashMap 우리는 이런 식으로 넣을 수 있습니다

Map<KeyObject, WrapperObject> 

WrapperObj 에는 클래스 변수가 있습니다.class1Obj, class2Obj


0

암시 적으로 할 수 있습니다.

// Create the map. There is no restriction to the size that the array String can have
HashMap<Integer, String[]> map = new HashMap<Integer, String[]>();

//initialize a key chosing the array of String you want for your values
map.put(1, new String[] { "name1", "name2" });

//edit value of a key
map.get(1)[0] = "othername";

이것은 매우 간단하고 효과적입니다. 다른 클래스의 값을 원하는 경우 다음을 수행 할 수 있습니다.

HashMap<Integer, Object[]> map = new HashMap<Integer, Object[]>();

0

키 비교가 equals ()가 아닌 == 연산자로 수행되는 조건에 따라 identityHashMap을 사용하여 수행 할 수 있습니다.


0

별도의 클래스를 만들지 않고 여러 변수를 저장하려면 다음을 선호합니다.

final public static Map<String, Map<String, Float>> myMap    = new HashMap<String, Map<String, Float>>();

0

Objective C의 데이터 사전을 사용 하여이 작업을 수행하는 데 너무 익숙합니다 .Android 용 Java에서 비슷한 결과를 얻는 것이 더 어려웠습니다. 결국 사용자 정의 클래스를 만든 다음 사용자 정의 클래스의 해시 맵을 수행했습니다.

public class Test1 {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addview);

//create the datastring
    HashMap<Integer, myClass> hm = new HashMap<Integer, myClass>();
    hm.put(1, new myClass("Car", "Small", 3000));
    hm.put(2, new myClass("Truck", "Large", 4000));
    hm.put(3, new myClass("Motorcycle", "Small", 1000));

//pull the datastring back for a specific item.
//also can edit the data using the set methods.  this just shows getting it for display.
    myClass test1 = hm.get(1);
    String testitem = test1.getItem();
    int testprice = test1.getPrice();
    Log.i("Class Info Example",testitem+Integer.toString(testprice));
}
}

//custom class.  You could make it public to use on several activities, or just include in the activity if using only here
class myClass{
    private String item;
    private String type;
    private int price;

    public myClass(String itm, String ty, int pr){
        this.item = itm;
        this.price = pr;
        this.type = ty;
    }

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }

    public String getType() {
        return item;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

}


0

Java 수집기 사용

// Group employees by department
Map<Department, List<Employee>> byDept = employees.stream()
                    .collect(Collectors.groupingBy(Employee::getDepartment));

부서가 당신의 열쇠 인 곳


-9

LinkedHashMap , 샘플을 사용해보십시오 .

Map<String,String> map = new LinkedHashMap<String,String>();    
map.put('1','linked');map.put('1','hash');    
map.put('2','map');map.put('3','java');.. 

산출:

키 : 1,1,2,3

값 : 링크, 해시, 맵, 자바


7
작동하지 않습니다. linked로 교체했기 때문에지도에 더 이상 존재하지 않습니다 hash.
Jeff Mercado
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.