Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (105)
games submitted by our members
Games in WIP (524)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  sort out hashmap entries by key  (Read 1676 times)
0 Members and 1 Guest are viewing this topic.
Offline Valodim

Junior Member




*sigh*


« Posted 2005-03-14 20:02:21 »

hey there Smiley

I came accross this problem today:

I have a HashMap<String, String>, and I want to sort out entries selectively by key, that is, only keys which start with ~ should remain (and the ~s should be removed in the process).

My first thought was to get some queue object from the keys, process them and call remove(key) on the hashmap. but this seems rather slow to me and it's not possible to remove the ~s in front of all keys in the process. :\

How would you do this? I'm not quite sure - especially  about performance aspects - on this one >_>
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #1 - Posted 2005-03-15 07:32:26 »

Use a different datatype? Something tree-based, so that sorting is easy and efficient (assuming, of course, you;ve *actually profiled it* and discovered this is your bottleneck)

malloc will be first against the wall when the revolution comes...
Offline c_lilian

Senior Member


Projects: 1


Java games will probably rock someday...


« Reply #2 - Posted 2005-03-15 07:52:39 »

If it's a single shot sorting and filtering (or at least, not too often used), you can use something like this (from memory...) :
(java 1.4 code)
List sorted = Collections.sort(map.keySet(), new Comparator(){
   public int compare(Object o1, Object o2){
        String s1 = (String) o1;
        if (s1.startsWith("~s"){
           String s2 = (String) o2;
           if (s2.startsWith("~s")){
              return s1.compareTo(s2);
           } else {
              return -1;
        } else {
              return 1;
        }
  }
});

This way you'll have a sorted list, with "~s" Strings at the beginning.

Iterate over it until a non ~s string found.

Lilian

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Valodim

Junior Member




*sigh*


« Reply #3 - Posted 2005-03-15 14:07:37 »

1  
2  
3  
4  
5  
6  
7  
        HashMap<String, String> timerdata = new HashMap<String, String>();
        Iterator<String> keys = data.keySet().iterator();
        while(keys.hasNext()){
            String temp = keys.next();
            if(temp.charAt(0) == '~')
                timerdata.put(temp.substring(1), data.get(temp));
        }


may not be the fastest, but it works Smiley

thanks for your advice Smiley
Offline c_lilian

Senior Member


Projects: 1


Java games will probably rock someday...


« Reply #4 - Posted 2005-03-15 14:12:23 »

Correct me if i'm wrong, but it is not sorted... ;-)

Lilian

Offline Valodim

Junior Member




*sigh*


« Reply #5 - Posted 2005-03-15 14:30:31 »

well of course it's not...

Quote
sort out entries selectively by key, that is, only keys which start with ~ should remain
Offline c_lilian

Senior Member


Projects: 1


Java games will probably rock someday...


« Reply #6 - Posted 2005-03-15 15:08:55 »

Last thought (as we are in the performance tuning thread).

I don't know if it's usefull in your case, but if you iterate over
entries() instead of keySet(), you'd have the benefits of accessing the key's associated value in one more field access ((Map.Entry)).getKey() / getValue()) instead of a hashing, but you might already be aware of it.  

Lilian

Offline Valodim

Junior Member




*sigh*


« Reply #7 - Posted 2005-03-15 15:44:04 »

ah.... right. thanks Smiley

1  
2  
3  
4  
5  
6  
7  
        HashMap<String, String> timerdata = new HashMap<String, String>();
        Iterator<Entry<String, String>> keys = data.entrySet().iterator();
        while(keys.hasNext()){
            Entry<String, String> temp = keys.next();
            if(temp.getKey().charAt(0) == '~')
                timerdata.put(temp.getKey().substring(1), temp.getValue());
        }
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

SHC (28 views)
2014-07-12 17:50:04

Riven (31 views)
2014-07-10 20:20:18

CopyableCougar4 (29 views)
2014-07-10 02:26:14

CopyableCougar4 (32 views)
2014-07-09 02:55:38

Code Mage (33 views)
2014-07-08 23:57:00

Code Mage (21 views)
2014-07-08 23:49:08

AppleSauce (28 views)
2014-07-08 19:25:32

CopyableCougar4 (29 views)
2014-07-06 01:51:26

ipe369 (35 views)
2014-07-05 14:18:25

vastrolorde (44 views)
2014-07-04 18:45:44
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!