1 2 Previous Next 15 Replies Latest reply: Mar 26, 2007 2:09 PM by jschellSomeoneStoleMyAlias RSS

    string index

    807606
      Hello everybody....
      Which way do you think is better for emulating string array index, like in php, where you can have : bar["foo"]="value"..

      1) Making a class which have 2 string arrays : one for the keys , one for the values, and make get and set. methods (these will loop in the arrays to locate the index) ..

      2) Using hashmap.

      3) Other way???
        • 1. Re: string index
          807606
          You can use java.util.Properties

          http://java.sun.com/javase/6/docs/api/java/util/Properties.html
          • 2. Re: string index
            807606
            Hi,
            You can look into the Collections and pick correspondingly based on your need. For eg..You can try out Hashtable, if you are not much worry about performance since it is synchronized.

            I feel HashMap is the better implementation. But still it depends on your requirement like duplicates, null, sorted etc..

            Thanks

            Regards,
            Nanda
            • 3. Re: string index
              807606
              You can use java.util.Properties

              http://java.sun.com/javase/6/docs/api/java/util/Proper
              ties.html
              I've checked Properties and it seems to be appropriate for me....i just need the match between string index and it's value....i'm trying to do a generic class for database objects, so i want to have the fields and it's values in an array....

              Good performance is important in my case
              • 4. Re: string index
                807606
                I've checked Properties and it seems to be appropriate for me....i just need the >match between string index and it's value....i'm trying to do a generic class for >database objects, so i want to have the fields and it's values in an array....
                Good performance is important in my case
                If performance is your concern then I don't think Properties, a best option as it has extended Hashtable. Please check the same url for details.

                Please let me know if I have mistaken.

                Regards,
                Nanda
                • 5. Re: string index
                  807606
                  If performance is your concern then I don't think
                  Properties, a best option as it has extended
                  Hashtable.
                  I don't know any other options :( ....

                  do you that think that by using objects as indexes, properties(hashtable) could affect my performance?
                  • 6. Re: string index
                    807606
                    Hi,
                    Do you have any specific concern to use HashMap? If not you can very well go about it.

                    Thanks

                    Regards,
                    Nanda
                    • 7. Re: string index
                      807606
                      Hi,
                      Do you have any specific concern to use HashMap?
                      If not you can very well go about it.
                      But if properties is a subclass of HashMap that only uses string, and i only want strings, why not Propoerties?

                      I started first with HashMap but it bothers me that it has Objects as indexes and Objects as values....
                      • 8. Re: string index
                        807606
                        I started first with HashMap but it bothers me that it has Objects as indexes and Objects as values....
                        Upgrade to the current version of Java (6) or even the previous version (5),
                        and you can use generics:
                        void f(Map < String, String > m ) {
                            for(String key : m.keySet()) {
                                String value  = m.get(key);
                                //etc...
                            }
                        }
                        • 9. Re: string index
                          807606
                          I started first with HashMap but it bothers me that
                          it has Objects as indexes and Objects as values....

                          Upgrade to the current version of Java (6) or even
                          the previous version (5),
                          and you can use generics:
                          void f(Map < String, String > m ) {
                          for(String key : m.keySet()) {
                          String value  = m.get(key);
                          //etc...
                          }
                          }
                          I'm not familiar with generics, i'll have to learn about them, i see you are using Map, is this lighter than hashmap?? i mean, using this technique, will i consume less resources?
                          • 10. Re: string index
                            807606
                            'm not familiar with generics, i'll have to learn about them,

                            http://java.sun.com/docs/books/tutorial/extra/generics/index.html

                            i see you are using Map, is this lighter than hashmap?? i mean, using this technique, will i consume less resources?

                            http://java.sun.com/docs/books/tutorial/collections/interfaces/index.html

                            Map is the key interface that HashMap implements. It's an object-oriented dictum
                            that variables should be of the most general type for their use.
                            • 11. Re: string index
                              807606
                              Upgrade to the current version of Java (6) or even
                              the previous version (5),
                              and you can use generics:
                              void f(Map < String, String > m ) {
                              for(String key : m.keySet()) {
                              String value  = m.get(key);
                              //etc...
                              }
                              }
                              Isn't this the same effect Properties provides? String indexes in the object indexed hashmap???
                              • 12. Re: string index
                                jschellSomeoneStoleMyAlias
                                i'm trying to do a
                                generic class for database objects, so i want to have
                                the fields and it's values in an array....
                                Generally a bad idea.
                                Good performance is important in my case
                                Then why are you using a mapping scheme at all?
                                • 13. Re: string index
                                  807606
                                  Isn't this the same effect Properties provides? String indexes in the object indexed hashmap???
                                  Using Map < String, String > means you are properly working in the
                                  generic collections framework. For legacy reasons,
                                  Properties is derived from Hashtable < Object, Object > and has the ad hoc
                                  methods getPoperty and setProperty.
                                  • 14. Re: string index
                                    807606
                                    i'm trying to do a
                                    generic class for database objects, so i want to
                                    have
                                    the fields and it's values in an array....
                                    Generally a bad idea.
                                    You think so? I just thought it could be better if i don't have to write the sql queries every time i want to get, insert, update data..So i came up with a general structure so i just have to specify the fields names and the data types and have functions that automatically make the sql queries....
                                    Then why are you using a mapping scheme at all?
                                    Cause i wanted string indexes so i could have something like:
                                    values["fieldname"]=value (of course in java this won't be the sintax)
                                    1 2 Previous Next