Search data in an ArrayList of objects

13

I am creating an application where each user has their respective profile. These profiles are stored in a ArrayList . I have problems when creating a method to find registered or present users in ArrayList and I have to do it with 2 parameters Nombre and Apellido .

I have the following class:

import java.util.*;

public class Profilo {

    static String Nombre;
    static String Apellido;
    String Intereses;
    boolean visible;

    //contructor   
    public Profilo(String nombre, String apellido,String intereses,
            boolean visibilidad) {  
        this.Nombre= nombre;
        this.Apellido= apellido;
        this.Intereses= intereses;
        this.visible = visibilidad;

    }                  
    public String getNombre() {  
        return this.Nome;
    }            
    public String getApellido() {
        return this.Cognome;
    }

And another class that will contain the methods to login, register and search for users:

import java.util.ArrayList;
import java.util.Scanner;


public class PerfilManager {

    static ArrayList<Profilo> Users = new ArrayList();
    static Scanner sc = new Scanner(System.in);


     public static void Login() {
         //codigo
     }

     public static void Registrarse() {
        //codigo
     }

     public static void InizializaDati(){
         String nombre;
         String apellido;
         String intereses;
         boolean visibile;

         System.out.println("nombre:");
         nombre= sc.next();

         System.out.println("apellido:");
         apellido = sc.next();

         System.out.println("intereses:");
         intereses= sc.next();

         System.out.println("visible?(true/false)");  
         visibile = sc.nextBoolean();

         Perfil perfil= new Perfil(nombre,apellido,intereses,visibile);

         Users.add(perfil);
         System.out.println("peril creado!");

         ShowPerfil();
    }   

    public static void BuscaPerfil(String nome , String cognome) {
        for(int i = 0 ; i < Users.size();i++) {
            if(Users.get(i).visibile != false) {
                //muestra perfil
            } else {
                System.out.println("Este perfil es privado");    
            }
        }
    }
}
    
asked by Bryan Romero 10.05.2016 в 16:00
source

2 answers

8

The best way to search a List list is by iterating its elements and performing the search:

public static Profilo BuscaPerfil(String nome , String cognome) {
    Profilo resultado = null;
    for (Profilo profilo : Users) {
        if (profilo.isVisible() //esta condición estaba previamente
            && profilo.getNombre().equals(nome)
            && profilo.getApellido().equals(cognome)) {
            resultado = profilo;
            break;
        }
    }
    return resultado;
}

I have changed the method so that it returns a Profilo . In this way, the client of the method, that is, the place where the method is used, can perform the concrete action with the result of the method. The action can be: display the data of the object on the screen, use it for a massive process of updating data, use the object to be part of another object, etc.

From Java 8, the search method can be reduced to the following:

public static Profilo BuscaPerfil(String nome , String cognome) {
    Optional<Profilo> profilo = Users.stream()
        .filter(p -> p.isVisible()
            && p.getNombre().equals(nome)
            && p.getApellido().equals(cognome))
        .findFirst();
    return profilo.isPresent() ? profilo.get() : null;
}

Tips to improve your current code (it is not part of the main problem stated in the question).

In Java, static members (attributes and methods) belong to the class and not to the instances of the class (objects). If you declare an attribute as static, since the attribute belongs to the class and not to the object, then when assigning a value to that attribute, this value will be shared by all instances of the class. Therefore, in your class Profilo , the fields must not be static. In addition to this, all fields should have at least their getter method.

The class Profilo should look like this:

public class Profilo {
    private String nombre;
    private String apellido;
    private String intereses;
    private boolean visible;

    //contructor   
    public Profilo(String nombre, String apellido,String intereses,
            boolean visibilidad) {  
        this.nombre= nombre;
        this.apellido= apellido;
        this.intereses= intereses;
        this.visible = visibilidad;

    }                  
    public String getNombre() {  
        return nombre;
    }            
    public String getApellido() {
        return apellido;
    }
    private String getIntereses() {
        return intereses;
    }
    private boolean isVisible() {
        return visible;
    }
}
    
answered by 10.05.2016 / 16:22
source
2

Since Users is a ArrayList of objects profilo you can get the values in this way:

   public static void BuscaPerfil(String nome , String cognome) {
      for (Profilo profilo : Users) {
            if (profilo.getNombre().equals(nome)
                && profilo.getApellido().equals(cognome)) {
                 System.out.println("Este perfil existe: " + profilo.getNombre());    
            }else{
                System.out.println("Este perfil es privado");   
           } 
        }
    }
    
answered by 10.05.2016 в 16:27