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 .