-2

I have looked several questions related sorting in SO , but I couldn't accomplish what I actually want .

I have a class like this

class User{
    String userName;
    String phoneNumber;

    public User(String userName, String phoneNumber) {
        this.userName = userName;
        this.phoneNumber = phoneNumber;
    }
}

Then I have added some data like this

ArrayList<User> users = new ArrayList<User>();
users.add(new User("Aca","223554"));
users.add(new User("Ada","223544"));
users.add(new User("Cda","323544"));
users.add(new User("Bbc","323544"));
users.add(new User("Bla","323544"));
users.add(new User("Aka","123554"));
users.add(new User("Cla","323544"));

I wanted to group data by username of User alphabetically , like

A-- Aca, Aka
B-- BBc , Bla
C-- Cda, Cla

And later I want to put that in HashMap Like

Map<String,ArrayList<User>> userMap = new HashMap<String, ArrayList<User>>();

userMap.put("A", usersLisA); //usersLisA is an ArrayList of user whose name starts with A
userMap.put("B", usersLisB); //usersLisB is an ArrayList of user whose name starts with B

and so on .

it is something like phone book , the way they sort and group their user alphabetically

One thing to be noted here , I want to accomplish in Java7.

Any help will be highly appreciated .

mSapps
  • 601
  • 9
  • 24
  • 2
    You clearly haven't looked enough. Plenty of literature around. – Mena Nov 23 '17 at 15:33
  • Try harder, you can sort by `userName` and then loop to populate your hash map – Joachim Huet Nov 23 '17 at 15:34
  • Implement equals hashCode and compareTo for User. Then use a TreeMap instead of a HashMap. – Joop Eggen Nov 23 '17 at 15:37
  • You're not using Java 8? That's a shame, it could be done in just one line: `Map> groups = users.stream().collect(Collectors.groupingBy(t -> t.getUsername().substring(0, 1)));` — assuming that each user has a non-empty name. – MC Emperor Nov 23 '17 at 15:51
  • @MCEmperor actually I am going to use it in android . it requires higher level of api than my minimum api level . – mSapps Nov 23 '17 at 16:08

1 Answers1

1

You have two options. Let's see the first:

  • Step 1: Loop over your ArrayList<User> users and put user.userName in a String[] usersArr
  • Step 2: Sort the array with Collections.sort(Arrays.asList(usersArr));
  • Step 3: Define an int startIndex = 0, an int endIndex, and loop over the sorted array and:

    if((int) previousString.charAt(0) < (int) currentString.charAt(0)) {
        endIndex = currentString_position-1;
        TreeMap.put(previousString.charAt(0), usersArr.subList(startIndex, endIndex));
        startIndex = currentString_position;
    }
    

Now, let's see the second option:

  • Step 1: Override hashCode() and compareTo() for your class Users so that you can later order it by userName
  • Step 2: Sort the ArrayList with Collections.sort(Arrays.asList(users));
  • Step 3: Same as Step 3 in previous option

Note that I changed your HashMap to TreeMap. If the order of the keys in your Map matters you should use TreeMap, since when iterating over the keys, they will be in order. Check this post.

Hope this answer has made things clearer for you.

LuisFerrolho
  • 417
  • 3
  • 13