5 回答
TA贡献1805条经验 获得超10个赞
public class User {
private String userName;
public String getUserName() {
return userName;
}
public User(String userName, String userNumber) {
this.userName = userName;
this.userNumber = userNumber;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserNumber() {
return userNumber;
}
public void setUserNumber(String userPassword) {
this.userNumber = userPassword;
}
private String userNumber;
}
主类
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainMethod {
private static HashMap<User, ArrayList<User>> maps;
private static ArrayList<User> user1list;
private static ArrayList<User> user2list;
public static void main(String...args) {
maps = new HashMap<>();
user1list = new ArrayList<>();
user2list = new ArrayList<>();
User user1 = new User("Ram", "1234");
User user2 = new User("Alish", "5678");
User user3 = new User("Ram", "91011");
User user4 = new User("Alish", "121314");
user1list.add(user1);
user1list.add(user3);
user2list.add(user2);
user2list.add(user4);
maps.put(user1, user1list );
maps.put(user2, user2list);
/// User one lists
for ( User userone: maps.get(user1)) {
System.out.println("userone Name:::"+userone.getUserName());
System.out.println("User one Number:::"+userone.getUserNumber());
}
System.out.println();
// USer tow list
for (User usertwo: maps.get(user2)) {
System.out.println("User two Name:::" +usertwo.getUserName());
System.out.println("User two Number :::" +usertwo.getUserNumber());
}
}
}
TA贡献1829条经验 获得超13个赞
Map<String, List<Users>> use = model.stream.collect(Collectors.groupinBy(Person::getUsername)) ;
您可以使用这种方法按组打印数据
TA贡献1816条经验 获得超6个赞
使用 Java Streams 尝试如下所示。
//group by user name
Map<String, Set<String>> map =
model.stream().collect(
Collectors.groupingBy(Users::getUserName,
Collectors.mapping(Users::getUserNumber, Collectors.toSet())
)
);
System.out.println(map);
确保在 Users 类中实现getter 方法getUserName()、getUserNumber() 。这里我假设userName和userNumber都是字符串类型的数据。
TA贡献1833条经验 获得超4个赞
完成您想要的操作的一种快速而简单的方法是使用HashMap
. 哈希映射是键值集合,在您的情况下,您将创建一个HashMap<String, List<String>>
存储username
键和userNumber
值列表的存储(因为您有多个值)。
那么你的代码将更改如下:
迭代您拥有的所有用户。
如果
username
hashmap 中存在,则将其添加usernumber
到其中。如果
username
不存在,则将其添加到哈希映射中,并将 用作usernumber
其列表的第一个元素。
最后,您将迭代哈希图,打印它所具有的键和值。
TA贡献1783条经验 获得超4个赞
请尝试下面的代码 -
public class Users {
public String userName;
public String userNumber;
public Users(String userName, String userNumber) {
this.userName = userName;
this.userNumber = userNumber;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserNumber() {
return userNumber;
}
public void setUserNumber(String userNumber) {
this.userNumber = userNumber;
}
public static void main(String ar[]) {
ArrayList<Users> model = new ArrayList<Users>();
model.add(new Users("abc", "123"));
model.add(new Users("def", "888"));
model.add(new Users("abc", "246"));
model.add(new Users("def", "999"));
model.add(new Users("abc", "456"));
Map<String, Set<String>> map = model.stream().collect(Collectors.groupingBy(Users::getUserName,
Collectors.mapping(Users::getUserNumber, Collectors.toSet())));
for (Map.Entry<String, Set<String>> entry : map.entrySet()) {
Set set = entry.getValue();
Iterator value = set.iterator();
while (value.hasNext()) {
System.out.println(entry.getKey() + " : " + value.next());
}
}
}
}
添加回答
举报