How to create an element search method in an ArrayList? [closed]

-2

Assuming an ArrayList that stores objects, and these objects have attributes of various types (int, String, float, etc ...). Knowing this I want to create a method for finding an object in the ArrayList through a keyword or number that can be inside an Array element or not. Without being by the index of each element.

    
asked by anonymous 06.11.2017 / 05:32

3 answers

3

You can do something like:

public class Pessoa {

    private int id;
    private String nome;
    private int idade;

    public Pessoa(int id, String nome, int idade) {
        this.id = id;
        this.nome = nome;
        this.idade = idade;
    }

    public int getId() {
        return id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public int getIdade() {
        return idade;
    }

    public void setIdade(int idade) {
        this.idade = idade;
    }
}
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {

        ArrayList<Pessoa> pessoas = new ArrayList<Pessoa>();

        pessoas.add(new Pessoa(1, "José", 32));
        pessoas.add(new Pessoa(2, "João", 25));
        pessoas.add(new Pessoa(3, "Maria", 43));

        Pessoa pessoaPeloNome = buscaPeloNome(pessoas, "João");
        Pessoa pessoaPelaIdade = buscaPelaIdade(pessoas, 43);

        if(pessoaPeloNome != null) {
            System.out.println("Pessoa pelo nome: " + pessoaPeloNome.getId() + " | " + pessoaPeloNome.getNome() + " | " + pessoaPeloNome.getIdade());
        }

        if(pessoaPelaIdade != null) {
            System.out.println("Pessoa pela idade: " + pessoaPelaIdade.getId() + " | " + pessoaPelaIdade.getNome() + " | " + pessoaPelaIdade.getIdade());
        }
    }

    public static Pessoa buscaPeloNome(ArrayList<Pessoa> pessoas, String nome) {

        for(Pessoa pessoa : pessoas) {
            if(pessoa.getNome().equals(nome)) {
                return pessoa;
            }
        }
        return null;
    }

    public static Pessoa buscaPelaIdade(ArrayList<Pessoa> pessoas, int idade) {

        for(Pessoa pessoa : pessoas) {
            if(pessoa.getIdade() == idade) {
                return pessoa;
            }
        }
        return null;
    }
}

Output:

  

Person by name: 2 | João | 25
Person by age: 3 | Maria | 43

    
06.11.2017 / 12:00
2

The easiest way is:

Arrays.asList("Débora", "Luan").stream().filter(string -> string.equals("Luan"))
    .collect(Collectors.reducing((a, b) -> null)).get();

Or even, what would be more protected in case there is no item that resembles the given information:

Arrays.asList("Débora", "Luan").stream().filter(parametroFiltro -> parametroFiltro.equals("Luan")).findFirst().get();

I've used some tools available since version 1.8 of Java. With lambda expressions it is easier to write small functional anonymous classes with only one method, which takes a parameter (the Filter parameter), and defines it inside a method (so the arrow, -> is used to indicate how it will be its execution).

For example, the filter method used after stream is used to "filter" the collection from a given check.

The code .filter(parametroFiltro -> parametroFiltro.equals("valor") can be replaced by:

.filter(new Predicate<String>(){

    @Override
    public boolean test(String parametroFiltro) {
        return parametroFiltro.equals("Luan");
    }
})

In case of the filter, as it expects an object instantiated from the Predicate interface, it knows that it should execute the defined code is parametroFiltro -> parametroFiltro.equals("valor") as a method that has something like return and only receives a parameter type String. The second use of lambda was used to define as the parameter of reducing an instance of the BinaryOperator class, which has method apply as a method that receives two variables and returns a type T, defined in the creation of the anonymous class . in this case:

Collectors.reducing(new BinaryOperator<String>() {

    @Override
    public String apply(String a, String b) {
        return null;
    }
}

I do not know why I think it's cool to share more fun ways to handle it.

And in any case, please, if people who are more studied in the subject manage to complete the information, I am immensely grateful.

    
06.11.2017 / 13:23
0

You create a class that implements an ArrayList, and then you can implement a search method and overwrite it for each attribute you want. Collections already implement these standard methods. sort: Sorts the elements of a List. binarySearch: Finds an object in a List using the introduced high-performance binary search algorithm. To use them, the objects that you put in the ArrayList need to have the method compareTo that is declared in the Comparable interface and is sometimes called the natural method of comparison. The sort query can specify as a second argument a Comparator object, which determines an alternative order of the elements. Reference is Java - How to Program 10th ed.

    
06.11.2017 / 05:51