android Java打印阵列列出具有组名的组

qybjjes1  于 2023-03-21  发布在  Android
关注(0)|答案(5)|浏览(124)

我只是一个初学者,学习Java对我自己!我只是有一个数组列表的对象的用户:

class Users{
   public userName;
   public userNumber;
}

然后我创建一个模型

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"));

我想要做的是以这种方式打印模型,应该显示类似的分组数据,组名如下:

for(int i=0;i:i<users.size;i++){
    System.out.printlin("users:::"+users.get(i).userNumber);
 }

但它也应该按名称分组和打印组,即:

abc ->
       users:::123
       users:::246
       users:::456

 def ->
       users:::888
       users:::999

所以我想分组数据,然后打印数据在分组格式与每个组名也!
我怎么能做到呢,我只是在自己学习Java。提前感谢

fkaflof6

fkaflof61#

一个快速简单的方法就是使用HashMap.哈希Map是键-值集合,在你的例子中,你可以创建一个HashMap<String, List<String>>,它把username存储为键,把userNumber存储为值列表(因为你有不止一个值).
然后,代码将更改如下:
1.迭代您拥有的所有用户。
1.如果散列表中存在username,则将usernumber添加到其中。
1.如果username不存在,则将其添加到散列表中,并将usernumber作为其列表的第一个元素。
最后,您将迭代哈希Map,打印它所具有的键和值。

57hvy0tb

57hvy0tb2#

使用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);

确保在用户类中实现getter方法 getUserName(),getUserNumber()。这里我假设 userNameuserNumber 都是字符串类型的数据。

kfgdxczn

kfgdxczn3#

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());
    }
  }
 }
yquaqz18

yquaqz184#

请尝试以下代码-

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());
        }
    }

}

}

oknrviil

oknrviil5#

使用=模型.流.收集(收集器.分组依据(个人::获取用户名));
您可以使用此方法按组打印数据

相关问题