Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create an array of values from a list of objects

I cant find a java version of this anywhere. In js this is simple. I have a list of objects with attributes and I want to create arrays for each attribute. The user class simply initializes its own attributes. The main class initializes the list with user objects and then creates lists of usernames and passwords.

User:

public class User{

    String username, password;

    public User(){}

    public User(String myName, String myPwd){

        username = myName;
        password = myPwd;
    }
}

Main:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import java.util.stream.Collectors;

public class Main{

    public static void main(String[] args){

        List<User> users = new ArrayList<>();

        int i = 0;
        while(i<5){

            users.add(new User("Brandon" + i,"LetsGo" + i));
            i++
        }

        System.out.println("Enter your username.");
                String username = scanner.nextLine();
                System.out.println("Enter your password.");
                String password = scanner.nextLine();
        
        //how do I make these lists?
        List<String> names = users.stream().map(User::username).collect(Collectors.toList());
        List<String> pwds = users.stream().map(User::password).collect(Collectors.toList());

        if(names.contains(username)){
            if(pwds.contains(password)){
                System.out.println("Welcome back " + username);
            }else{
                System.out.println("The credentials do not match our records.");
            }
        }else{
            users.add(new User(username, password));
            System.out.println("Welcome " + username);
        }
    }
}   

>Solution :

  • First, create the user instance with the loop.
List<User> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
  System.out.println("Enter your username.");
  String username = scanner.nextLine();
  System.out.println("Enter your password.");
  String password = scanner.nextLine();
  list.add(new User(username, password));
}
  • Then stream the list like your doing.
List<String> names = users.stream().map(User::username).collect(Collectors.toList());
List<String> pwds = users.stream().map(User::password).collect(Collectors.toList());

But your using a method reference for a getter so you need to include a method username() in your class definition that returns a String. Same for username. These are normally prefixed with get like getUsername.

public String getUsername() {
    return username;
}

Then you can do what you want with the lists

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading