하나의 키와 두 개의 값으로 HashMap을 구현할 수 있습니까? 해시 맵처럼?
하나의 키를 사용하여 세 가지 값의 저장을 구현하는 다른 방법을 알려 주면 (도움이 없다면) 도와주세요.
하나의 키와 두 개의 값으로 HashMap을 구현할 수 있습니까? 해시 맵처럼?
하나의 키를 사용하여 세 가지 값의 저장을 구현하는 다른 방법을 알려 주면 (도움이 없다면) 도와주세요.
답변:
당신은 할 수 있습니다 :
Map<KeyType, List<ValueType>>.Map<KeyType, WrapperType>.Map<KeyType, Tuple<Value1Type, Value2Type>>.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"];
이 솔루션의 단점은 두 맵이 관련되어 있다는 것이 확실하지 않으며 프로그래밍 오류로 인해 두 맵이 동기화되지 않은 것을 볼 수 있다는 것입니다.
Map<KeyType, Tuple<Value1Type, Value2Type>>
아닙니다 HashMap. 기본적으로 HashMap키부터 값 모음까지가 필요 합니다.
외부 라이브러리를 사용하고 싶다면 Guava 는 and와 Multimap같은 구현 에서 정확히이 개념을 사용합니다 .ArrayListMultimapHashMultimap
ArrayListMultimap자신 과 같은 것을 만들 거나 ... 또는 다른 것을 사용하십시오 HashMap<String, List<Integer>>. 기본적으로 값을 처음 추가 할 때마다 빈 목록을 만들어야합니다.
HashMap<String, List<Integer>>
또 다른 좋은 선택은 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];
기록을 위해 순수한 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
경우에 일관성을 보장하기 위해 참고 여러 스레드는이 데이터 구조를 액세스 ConcurrentHashMap및 CopyOnWriteArrayList인스턴스 필요에 사용할 수 있습니다.
computeIfAbsent. map.computeIfAbsent(key, k -> new ArrayList<>()).add(value);
가장 쉬운 방법은 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
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루프에서. 동일한 키 및 여러 값과 같은 효과가 있습니다.
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 라이브러리입니다. 이것이 필수 솔루션입니다.
Paul의 의견에 대한 답변을 게시 할 수 없으므로 Vidhya에 대한 새로운 의견을 작성 중입니다.
래퍼는 SuperClass값으로 저장하려는 두 클래스에 대한 것 입니다.
래퍼 클래스 내부에서 두 클래스 객체의 인스턴스 변수 객체로 연결을 넣을 수 있습니다.
예 :
class MyWrapper {
Class1 class1obj = new Class1();
Class2 class2obj = new Class2();
...
}
과에서 의 HashMap 우리는 이런 식으로 넣을 수 있습니다
Map<KeyObject, WrapperObject>
WrapperObj 에는 클래스 변수가 있습니다.class1Obj, class2Obj
암시 적으로 할 수 있습니다.
// 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[]>();
키 비교가 equals ()가 아닌 == 연산자로 수행되는 조건에 따라 identityHashMap을 사용하여 수행 할 수 있습니다.
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;
}
}
여러 키 또는 값을 갖도록 클래스를 만들 수 있으며이 클래스의 객체를 맵에서 매개 변수로 사용할 수 있습니다. https://stackoverflow.com/a/44181931/8065321을 참조 할 수 있습니다.
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
값 : 링크, 해시, 맵, 자바
linked로 교체했기 때문에지도에 더 이상 존재하지 않습니다 hash.