package com.trifulcas.colecciones;
import java.time.LocalDate;
import java.util.LinkedList;
import java.util.List;
public class EjemplosLinkedList {
public static void main(String[] args) {
LinkedList<String> lista = new LinkedList<String>();
// Lo mismo pero no especifico tipo
LinkedList<String> lista2 = new LinkedList<>();
// Lo mismo pero usando la clase abstracta
List<String> lista3 = new LinkedList<String>();
// Muy importante: el tipo tiene que ser una clase
// LinkedList<int> enteros=new LinkedList<int>();
// Utilizar unas clases 'wrapper' (envolventes)
// Que son lo mismo que los tipos primitivos pero en clase
LinkedList<Integer> enteros = new LinkedList<Integer>();
LinkedList<Boolean> boleanos = new LinkedList<Boolean>();
LinkedList<Double> decimales = new LinkedList<Double>();
// No especificamos tamaño, porque es dinámico
// Yo añado elementos con add, los que quiera
enteros.add(5);
enteros.add(9);
enteros.add(13);
System.out.println(enteros);
// Puedo recorrer con un for :
for (Integer i : enteros) {
System.out.println(i);
}
// Con un for normal usando el size (tamaño)
for (int i = 0; i < enteros.size(); i++) {
// con get obtenemos el elemento en esa posición
System.out.println(enteros.get(i));
}
// Podemos usar el foreach
enteros.forEach(x -> System.out.println(x));
LinkedList<LocalDate> fechas = new LinkedList<LocalDate>();
fechas.add(LocalDate.now());
// Añadir en un índice determinado
enteros.add(1, 666);
System.out.println(enteros);
// Eliminamos el elemento de la posición 2
enteros.remove(2);
System.out.println(enteros);
// Si un elemento está dentro el LinkedList
System.out.println(enteros.contains(5)); // true
System.out.println(enteros.contains(90)); // false
// Buscar la posición de un elemento
System.out.println(enteros.indexOf(13)); // 2
System.out.println(enteros.indexOf(130)); // -1 porque no lo encuentra
// Tenemos una serie de funciones que nos van a recordar a JS
// Yo puedo añadir al principio
enteros.push(7);
System.out.println(enteros);
// Recuperar del principio (y se va de la lista)
int r = enteros.pop();
System.out.println(enteros);
// ver sin eliminar
System.out.println(enteros.peekFirst());
System.out.println(enteros.peekLast());
System.out.println(enteros.getFirst());
System.out.println(enteros.getLast());
while (enteros.size() > 0) {
System.out.println(enteros.pop());
}
for (int i = 2; i < 40; i += 3) {
enteros.push(i);
}
System.out.println(enteros);
// Eliminar
System.out.println(enteros.poll());
System.out.println(enteros.pollFirst());
System.out.println(enteros.pollLast());
System.out.println(enteros);
}
}