0

I want to prepare a HashMap in such way that

Key   :  Country Code
Value :  List of returned orderEntries  

the following process data method process every 5 orderEntry which can be from any country.

let me make it more clear. I have list of orderEntries that come from different countries now I want to put these entries into map based on country key. Like if 20 entries coming from US then US will be the key and 20 Entries would be the values. But problem is that I don't want to create a list for each county inside map.

public void processSegmentData(final List resultSet)
    {

        for (final Object orderEntry : resultSet)
        {
            if (orderEntry instanceof OrderEntryModel)
            {
                String countryCode = null;

                final OrderModel order = ((OrderEntryModel) orderEntry).getOrder();

                if (order.getDeliveryAddress() != null)
                {
                    countryCode = order.getDeliveryAddress().getCountry().getIsocode();
                }


                orderEntriesMap.put(Config.getParameter(countryCode+".return.pid"), orderEntries);

            }

        }

    }
Anil
  • 578
  • 4
  • 12
Devendra
  • 1,864
  • 6
  • 29
  • 49
  • I could not get what you want. Can you please elaborate or provide some more info? Not sure if you want a Map> or something like that – shikjohari Feb 09 '15 at 09:04
  • let me clear more. I have list of orderEntries from different countries now I want to put these entries into map based on countries. – Devendra Feb 09 '15 at 09:05
  • Yes I want something Map> but I have problem with that if Entries coming from 10 countries then I have to make 10 lists – Devendra Feb 09 '15 at 09:06
  • Why don't you want to create a list? what you want to do with those entries? – Anil Feb 09 '15 at 09:16

4 Answers4

1

so you are after a hashmap which contains a linked list Something along the lines of:

public HashMap<String, LinkedList<OrderEntryModel>> processSegmentData(final List resultSet) {
    HashMap<String, LinkedList<OrderEntryModel>> orderEntriesMap = new HashMap<String, LinkedList<OrderEntryModel>>();

    for (final Object orderEntry : resultSet) {
        if (orderEntry instanceof OrderEntryModel) {
            String countryCode = null;

            final OrderModel order = ((OrderEntryModel) orderEntry).getOrder();

            if (order.getDeliveryAddress() != null) {
                countryCode = order.getDeliveryAddress().getCountry().getIsocode();
            }
            if (!orderEntriesMap.containsKey(countryCode)) {
                orderEntriesMap.put(countryCode, new LinkedList<OrderEntryModel>());
            }
            orderEntriesMap.get(countryCode).add((OrderEntryModel) orderEntry);                

        }

    }
    return orderEntriesMap;
}

would be an example based on the source code you provided guessing object names.

Damian Nikodem
  • 1,324
  • 10
  • 26
  • Could you please elaborate your answer? – Devendra Feb 09 '15 at 09:03
  • Can I Try With Only list Instead of LinkedList? – Devendra Feb 09 '15 at 09:16
  • List in just a interface, you can add linked list or array list. – Anil Feb 09 '15 at 09:17
  • @Anil Arraylist is actually significantly less efficient to use than linked list. The only benefit of arraylist is that it is faster at a 'indexed lookup' (e.g. get(Integer) )... for LinkedList add and remove operations for both head and tail are always o(1), for arraylist when the size of the internal array is reached then a add operation is o(n). in both cases when traversing the list with a iterator then performance is identical. – Damian Nikodem Feb 09 '15 at 09:53
  • @DamianNikodem I want to add million integers and sort them, what should I use? ArrayList or LinkedList? – Anil Feb 09 '15 at 11:27
  • @Anil Please go through this link... http://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist – Devendra Feb 09 '15 at 12:45
  • @Anil ,Apologies for the delay I was waiting for my test to finish, Array List performed so poorly that I had to cut the test short, because I was too lazy to write something that would overcome linked list's known speed issues with getByIndex I simply used Collections.sort(), I know that it is possible to achieve significantly better performance on sorting Linked Lists though. over 50 iterations (with the first 10 being skipped to account for JVM warmup), My results are here: http://pastebin.com/MaJYTuDV Worst case scenario for LL was: 37ms, worst case scenario for AL was 161575 ms – Damian Nikodem Feb 09 '15 at 12:54
  • @DamianNikodem I was not talking about insertion at head at all, I want to just sort them... work case 161575ms ? what were you doing? adding at the head? – Anil Feb 09 '15 at 13:15
  • @Dev , I've read that before, I was little worried about the statement "Arraylist is actually significantly less efficient to use than linked list " – Anil Feb 09 '15 at 13:18
  • @anil actually for one of the tests that's exactly what was performed, if you take a look at the pastebin it has all of my results. As someone who has used both types of collection ( and had to write my own implementations in the past I can honestly say that array list is usually a bad idea) as I stated in my comment earlier the only operation where array list performs better than linked list is a indexed lookup, ( and yes this includes javas implementation of merge sort, but there are better algorithms available ) , insert and remove are significantly faster , iteration is about the same, – Damian Nikodem Feb 09 '15 at 15:06
1

Just Create a Map<String,List<String>>. and follow the following approach

Map<String,List<String>> countryMap = new HashMap<String, List<String>>();
        for (final String orderEntry : orders){
            if(countryMap.containsKey(orderEntry.getCountry())){
                countryMap.get(orderEntry.getCountry()).add(orderEntry);
            }else{
                //create a new list and add orderEntry
                countryMap.put(orderEntry.getCountry(),orderEntry);
            }
        }

You need to modify this according to your stuff

shikjohari
  • 2,278
  • 11
  • 23
  • I am trying to see how this code example would first compile, and then second not throw a NullPointerException – Damian Nikodem Feb 09 '15 at 09:56
  • @DamianNikodem .. I know, its just a sample which I came to from the above example. You need to tweak it acc to your code. – shikjohari Feb 09 '15 at 09:59
  • I wrote the accepted answer for this question (its not my question) I was just pointing out that there are enough logic and syntax issues (even after your edit 4 minutes ago there is still no way that code would compile. ) that posting code was sortof moot. – Damian Nikodem Feb 09 '15 at 10:07
  • @DamianNikodem I was just providing an idea on how to write and was not writing the exact code as you did. Anyways thanks for pointing it out. Next time i ll take care – shikjohari Feb 09 '15 at 10:11
1

But problem is that I don't want to create a list for each county inside map.

I understand your problem but map store unique key, you can not store same country code.

you have to use Map<String, List<String>>() that will hold your country code as key and then put your values inside List<String>.

after doing this if you have any problem edit your question will help you to resolve that.

atish shimpi
  • 4,873
  • 2
  • 32
  • 50
0

You could use Guava's Multimap to simplify things. A Multimap allows you to store multiple entries against a single key, e.g.:

Multimap<String, OrderEntry> orderEntriesMultimap = HashMultimap.create();

for (final Object orderEntry : resultSet) {       
    // omitted...
    orderEntriesMultimap.put(Config.getParameter(countryCode+".return.pid"), orderEntry);
}

You can then retrieve all the associated values by key:

Collection<OrderEntryModel> entries = orderEntriesMultimap.get(key);
Jonathan
  • 20,053
  • 6
  • 63
  • 70