0

My goal here is get an object that I can iterate over and grab my User's firstName and his favColor.

I have this:

for (Map user : userListing){
  String firstName    = (String) user.get(User.FIRST_NAME);
  String favColor     = (String) user.get(User.FAVORITE_COLOR);
  // Build up some Arrayish object add "Bob", "red"
  // 
  // what do i do here?
}

I'm unsure if I need to create, say an Array of Arrays?

My thought is that way I know the outer level of the Array is representative of each User, then once I'm the next level deep, item[0] would be the first name and item[1] would be the color.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mario Zigliotto
  • 8,315
  • 7
  • 52
  • 71
  • 1
    Why do you need to have a copy of all firstnames/favcolors? Why are you using a `Map` instead of a normal `User` javabean class in first place? – BalusC Feb 11 '11 at 01:42
  • Are you responsible for defining how users are represented? If not, can you give more details about how they're represented? If so, it's very unlikely you want to use arrays at all in this situation. – rlibby Feb 11 '11 at 01:46
  • you have a map of type { ["name","Bob"] , ["color","red" ] }, you just want another map as ["Bob","red"]. Create the new HashMap and *newMap.put(firstName,favColor);* Duplicate names will clash, if its undesired, just use a bean – guido Feb 11 '11 at 01:49

3 Answers3

1

I'm not sure what would be the best solution here. Using a Map to represent an user is already wrong in first place. I'd create a javabean class which represents an User.

public class User {
    private String firstName;
    private String favoriteColor;

    public String getFirstName() {
        return firstName;
    }

    public String getFavoriteColor() {
        return favoriteColor;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setFavoriteColor(String favoriteColor) {
        this.favoriteColor = favoriteColor;
    }

    // Add if necessary other javabean boilerplate like Serializable,
    // default c'tor, full c'tor, equals(), hashCode(), toString().
}

Then just collect them in a List<User> and pass that around instead.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Appreciate if you could please post the entire working example.Im finding it confusing what to do next after writing the javbean,how to manipulate the `List`,If you can edit and show a working example.it would be appreciated. – Deepak Feb 11 '11 at 13:20
  • @Deepak: see my answer on [this question](http://stackoverflow.com/questions/1727603/places-where-java-beans-used) – BalusC Feb 11 '11 at 13:25
0

Two ways to do it that are pretty simple

  1. Map<String,Map<String,Double>>  map2d = new HashMap<String,Map<String,Double>>();
    
    For each new "x-coordinate", you'd have to instantiate
    a new sub-HashMap, and put it into map2d. This could all be
    wrapped in some new class.
    
    to retrieve an element, you just use:
    map2d.get(xKey).get(yKey)
    
  2. Create a pair type and use that as your map key

http://www.velocityreviews.com/forums/t390520-2d-lookup-table.html

Nick
  • 3,096
  • 3
  • 20
  • 25
0

I would recommend:

  1. create a java bean like object:

    class Preferences{ //properties //getters //setters }

  2. then have a array of Preferences

    Preferences[] userPrefs = new Preferences[N]

  3. iterate by for (Preferences p : userPrefs) { //do the stuff}

Alvin
  • 10,308
  • 8
  • 37
  • 49