2
respostas

Teste

Porque retorna false?

List a4 = c1.getListaAulas();

System.out.println(a4==c1.getListaAulas());

2 respostas

Oi Alexandre

Manda o código completo para entendermos o contexto, por favor.

Depois que deixei de passar como Collections.unmodifiableList passou a retornar true, segue o código completo da classe abaixo.

import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set;

public class Curso {

private String nome;
private String instrutor;
private List<Aula> aulas = new LinkedList<Aula>();
private Set<Aluno> alunos = new HashSet<>();
private Map<Integer, Aluno> matriculaParaAluno = new HashMap<>();

public Curso(String nome, String instrutor) {
    this.nome = nome;
    this.instrutor = instrutor;
}

public String getNome() {
    return nome;
}

public String getInstrutor() {
    return instrutor;
}

public List<Aula> getAulas() {
    return Collections.unmodifiableList(aulas);
}

public void adiciona(Aula aula) {
    this.aulas.add(aula);
}

public Set<Aluno> getAlunos() {
    return Collections.unmodifiableSet(alunos);
}

public void matricula(Aluno a) {
    this.alunos.add(a);
    this.matriculaParaAluno.put(a.getNumeroMatricula(), a);
}

public boolean estaMatriculado(Aluno aluno) {
    return this.alunos.contains(aluno);
}

public Aluno buscaMatriculado(int matricula) {
    if (this.matriculaParaAluno.get(matricula) == null) {
        throw new NullPointerException("Matricula nao cadastrada!"); 
    }
    return this.matriculaParaAluno.get(matricula);
}

public int getTempoTotal() {
    return this.aulas.stream().mapToInt(Aula::getTempo).sum();
}

@Override
public String toString() {
    return "Curso [nome=" + nome + ", instrutor=" + instrutor + ", aulas=" + aulas + ", alunos=" + alunos + "]";
}

}

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software