LinkedList e Array List

5 respostas
Lucas_de_Almeida_Mar

Bom dia pessoal,

eu gostaria de saber como uma classe recebe dois objetos diferentes de diferentes classes, os tipos dos objetos são ArrayList e LinkedList

Abraço.

:smiley:

5 Respostas

thiago.correa

Você pode dizer que a tua classe recebe a superclasse/interface ex.:

class Pessoa {
    private List carros;

    private void setCarros(List carros) {
        this.carros = carros;
    }

    public static void main(String[] a) {
        //codigo sem efeito nenhum, apenas para demonstracao!
        Pessoa p = new Pessoa()
        p.setCarros(new ArrayList());
        p.setCarros(new LinkedList());

    }
}
Lucas_de_Almeida_Mar

muito obrigado meu amigo

me ajudou bastante

Lucas_de_Almeida_Mar

Mais uma duvida e se no lugar de linked list eu pudesse usar um vetor??

POWERED BY PUBLICONS.DE

thiago.correa

Lucas de Almeida Marciano:
Mais uma duvida e se no lugar de linked list eu pudesse usar um vetor??

POWERED BY PUBLICONS.DE

Não vejo muita vantagem, mas, caso seja realmente necessário, você pode fazer uma sobrecarga do método setXXX (que no meu exemplo foi setCarros)
Ficaria algo do tipo

class Pessoa {   
    private List carros;   
  
    public void setCarros(List carros) {   
        this.carros = carros;   
    }   
    public void setCarros(Object[] a) {   
        this.carros = Arrays.asList(a);   
    }   
  
    public static void main(String[] a) {   
        //codigo sem efeito nenhum, apenas para demonstracao!   
        Pessoa p = new Pessoa()   
        p.setCarros(new ArrayList());   
        p.setCarros(new LinkedList());   
        p.setCarros(new String[]{"Mercedes", "Audi", "Fusca"});   
  
    }   
}
1

um testezinho basico de Collections (List + Set) e Map :D

public class TesteCollections {

	@SuppressWarnings("unchecked")
	public void adiciona(Object object, int n, boolean isCollection) {
		long inicio = System.currentTimeMillis();
		for (int i = 0; i < n; i++) {
			if(isCollection) {
				((Collection<Integer>) object).add(i);
			}else {
				((Map<Integer, Integer>) object).put(i, i);
			}
		}
		long fim = System.currentTimeMillis();
		double tempo = (fim - inicio)/1000.0 ;
		System.out.print(tempo);
	}
	@SuppressWarnings("unchecked")
	public void busca(Object object, int n, boolean isCollection) {
		long inicio = System.currentTimeMillis();
		for (int i = 0; i < n; i++) {
			if(isCollection) {
				((Collection<Integer>) object).contains(i);
			}else {
				((Map<Integer, Integer>) object).containsKey(i);
			}
		}
		long fim = System.currentTimeMillis();
		double tempo = (fim - inicio)/1000.0 ;
		System.out.print(" + " + tempo);
	}
	public void testar(String tipo, TesteCollections teste, Object object, int total, boolean isCollection) {
		System.out.print(tipo + ": ");
		long inicio = System.currentTimeMillis();
		
		teste.adiciona(object,total,isCollection);
		teste.busca(object,total,isCollection);
				
		long fim = System.currentTimeMillis();
		double tempo = (fim - inicio)/1000.0 ;		
		System.out.println(" = " + tempo);
	}
	
	public static void main(String[] args) {
		int total = 30000;
		TesteCollections teste = new TesteCollections();
		System.out.println("Iniciando teste...");
		
		Collection<Integer> testeHashSet = new HashSet<Integer>();
		Collection<Integer> testeTreeSet = new TreeSet<Integer>();
		Collection<Integer> testeArray = new ArrayList<Integer>();
		Collection<Integer> testeLinkedList = new LinkedList<Integer>();
		Collection<Integer> testeVector = new Vector<Integer>();
		Map<Integer, Integer> testeHashMap = new HashMap<Integer, Integer>();
		Map<Integer, Integer> testeTreeMap = new TreeMap<Integer, Integer>();
		Map<Integer, Integer> testeHashTable = new Hashtable<Integer, Integer>();

		teste.testar("HashSet",teste,testeHashSet,total,true);
		teste.testar("TreeSet",teste,testeTreeSet,total,true);
		teste.testar("ArrayList",teste,testeArray,total,true);
		teste.testar("LinkedList",teste,testeLinkedList,total,true);
		teste.testar("Vector",teste,testeVector,total,true);
		teste.testar("HashMap",teste,testeHashMap,total,false);
		teste.testar("TreeMap",teste,testeTreeMap,total,false);
		teste.testar("Hashtable",teste,testeHashTable,total,false);
		
		System.out.println("Concluido!");
	}
}
Criado 27 de abril de 2010
Ultima resposta 27 de abr. de 2010
Respostas 5
Participantes 3