IT

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

lottoking 2020. 5. 17. 11:01
반응형

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


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

하나의 키를 사용하여 세 가지 값의 저장을 구현하는 다른 방법을 말하면 도움이 되겠습니까?


당신은 할 수 있습니다 :

  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];

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

랩퍼 클래스 사용

// 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"];

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


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

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


또 다른 좋은 선택은 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"를 포함하는 컬렉션이 생성됩니다 .


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

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


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];

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);


기록을 위해 순수한 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인스턴스 필요에 사용할 수 있습니다.


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

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

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

또는 사용 org.springframework.util.LinkedMultiValueMap


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


그렇습니다 multimap.

참조 : http://google-collections.googlecode.com/svn/trunk/javadoc/index.html?com/google/common/collect/Multimap.html


가장 쉬운 방법은 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
}

}

maven link: https://mvnrepository.com/artifact/com.google.collections/google-collections/1.0-rc2

more on this: http://tomjefferys.blogspot.be/2011/09/multimaps-google-guava.html


I could not post a reply on Paul's comment so I am creating new comment for Vidhya here:

Wrapper will be a SuperClass for the two classes which we want to store as a value.

and inside wrapper class, we can put the associations as the instance variable objects for the two class objects.

e.g.

class MyWrapper {

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

and in HashMap we can put in this way,

Map<KeyObject, WrapperObject> 

WrapperObj will have class variables: class1Obj, class2Obj


You can do it implicitly.

// 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";

This is very simple and effective. If you want values of diferent classes instead, you can do the following:

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

Can be done using an identityHashMap, subjected to the condition that the keys comparison will be done by == operator and not equals().


I prefer the following to store any number of variables without having to create a separate class.

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

Here is THE ANSWER : ) : )

String key= "services_servicename"

ArrayList 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);   }

I have got the Best Results.

You Just Have to Create New HashMap Like

HashMap servicesNameHashmap = new HashMap();

in Your For Loop. It will have Same Effect Like Same Key And Multiple Values.

Happy Coding :)


I am so used to just doing this with a Data Dictionary in Objective C. It was harder to get a similar result in Java for Android. I ended up creating a custom class, and then just doing a hashmap of my custom class.

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;
    }

}

We can create a class to have multiple keys or values and the object of this class can be used as a parameter in map. You can refer to https://stackoverflow.com/a/44181931/8065321


Try LinkedHashMap, sample:

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

output:

keys: 1,1,2,3

values: linked,hash, map, java

참고URL : https://stackoverflow.com/questions/4956844/hashmap-with-multiple-values-under-the-same-key

반응형