Solved

Map vs. Set

Posted on 2013-01-24
4
323 Views
Last Modified: 2013-02-02
Hi there;

Can the same keys occur in maps in Java?
Also for same key but different value mapping, what can be used?

A 1 4
A 23 5
A 345 4
-------
B 12 6
B 23 7
B 34444 8

12 23 345 in A and 12 23 34444 in B are unique keys for each group, so how can i group those, so that I can reach let's say longest element? Any stragies? Where to put where? Which collections?

Regards.
0
Comment
Question by:jazzIIIlove
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 4

Expert Comment

by:tvedtem
ID: 38816860
A Map maps keys to values.
A single key can only have one value.
There is nothing to stop you making the value a List object, should you need a 1-many data structure..

Does that answer your question ?  I didn't understand what you meant by reaching the 'longest element'.  A HashMap will allow you to find entries by key.
0
 
LVL 36

Expert Comment

by:mccarl
ID: 38817116
The below is some code that may be able to help what you want. It is based on MANY assumptions, as the description that you give above (and in your previous, now delete, question) is way too vague. But hopefully, it may be a starting point. If you can say where the below is not in line with your requirements, I can help you further.
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class TestMapOfLists {
    
    public static void main(String[] args) {
        Map<String, List<ItemDetail>> map = new HashMap<String, List<ItemDetail>>();
        map.put("A", Arrays.asList(new ItemDetail("1", 4), new ItemDetail("23", 5), new ItemDetail("345", 4)));
        map.put("B", Arrays.asList(new ItemDetail("12", 6), new ItemDetail("23", 7), new ItemDetail("34444", 8)));
        
        System.out.println("Map data\n--------\n" + map);
        
        
        Map<String, ItemDetail> results = new HashMap<String, TestMapOfLists.ItemDetail>();
        
        for (Map.Entry<String, List<ItemDetail>> entry : map.entrySet()) {
            int maxLength = 0;
            
            for (ItemDetail itemDetail : entry.getValue()) {
                int length = itemDetail.getId().length();
                
                if (length > maxLength) {
                    results.put(entry.getKey(), itemDetail);
                    maxLength = length;
                }
            }
        }
        
        System.out.println("Results\n--------\n" + results);
    }
    
    private static class ItemDetail {
        private String id;
        private int price;
        
        public ItemDetail(String id, int price) {
            this.id = id;
            this.price = price;
        }

        public String getId() {
            return id;
        }

        public int getPrice() {
            return price;
        }

        @Override
        public String toString() {
            return id + ": " + price;
        }
    }
}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38818265
Can the same keys occur in maps in Java?
No
Also for same key but different value mapping, what can be used?
You need a 'multimap'
http://commons.apache.org/collections/api-3.1/org/apache/commons/collections/MultiMap.html
You can of course make your own multimap without 3rd party libraries if you want
0
 
LVL 28

Accepted Solution

by:
dpearson earned 500 total points
ID: 38821521
The basic way to support this is to build a structure like this:

Map<String, List<String>> multiKeyMap = new HashMap<String, List<String>>() ;

to add a value you do this:
if (multiKeyMap.get(key) == null) multiKeyMap.put(key, new ArrayList<String>()) ;
multiKeyMap.get(key).add(value) ;

to get the list of values for a key you do this:
multiKeyMap.get(key) ; // Returns a list

You should be able to put the rest together pretty easily once you start from this.

Doug
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
Suggested Courses

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question