List etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
List etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

3 Eylül 2023 Pazar

List.of metodu - Unmodifiable List Döndürür

Giriş
Java 9 ile geliyor. JEP 269 ile gelen List, Set, Map arayüzlerinin of() static factory metodu.

Örnek
Şöyle yaparız.
List<String> TRAIN = List.of("...","...");
Örnek - record
Şöyle yaparız
record Employee(int id){ }

var empList = List.of(new Employee(1),
                new Employee(2),
                new Employee(3));

System.out.println(empList);

Örnek
Listede null değer olamaz.
List<Integer> list = List.of(1, 2, null); // Fails with a NullPointerException
Örnek
Listeye null eklemesi yapılamaz.
List<Integer> list = List.of(1, 2, 3);
list.contains(null); // Throws NullPointerException
Örnek
Döndürülen liste değiştirilemez.
List<Integer> list = List.of(1, 2, 3);
list.set(1, 10); // Fails
Collections.unmodifiableList Karşılaştırması
Java 8 kullanıyorsak bu metodu kullanmak gerekir.

Arrays.asList Karşılaştırması
Bu metod Arrays.asList() metodu ile karşılaştırılabilir. Arrays.asList() eski bir metod, Java 2'den beri var.  

1. Arrays.asList() array backed list döndürür. List.of() ise array backed list yerine yeni bir liste döndürür.
2. Her iki yöntemde de listeye ekleme çıkarma yapılamaz
3. Array backed list set işlemini destekler. List.of() ise desteklemez

Açıklaması şöyle
It takes an array or a series of elements and returns a List backed by the original array. This means any changes made to the list will directly affect the original array, and vice versa.
Dolayısıyla listeye ekleme çıkarma yapılamaz. Açıklaması şöyle
You can change the value of existing elements in a list but since it has a fixed size. You cannot add or remove elements from the list. Any attempt to add/remove will result in an UnsupportedOperationException.

Şöyle yaparız. Burada List.of() metodunun array backed list döndürmediği görülebilir.
Integer[] array = {1,2,3};
List<Integer> list = List.of(array);
array[1] = 10;
System.out.println(list); // Prints [1, 2, 3]
Şöyle yaparız. List.of() metodunun set işlemini desteklemediği ancak array backed list'in desteklediği görülebilir.
List<Employee> empList = List.of(new Employee(1),
                new Employee(2),
                new Employee(3));

empList.set(0, new Employee(50));// Not Supported
System.out.println(empList);


List<Employee> empList1 = Arrays.asList(new Employee(1),
                new Employee(2),
                new Employee(3));

empList1.set(0, new Employee(50));// Supported
System.out.println(empList1);

18 Şubat 2020 Salı

List Arayüzü

hashCode metodu
Açıklaması şöyle.
hashCode of Lists (for example, see the implementation of AbstractList) is a function of all the elements of the List. The same is true for the equals implementation.
Metodun içi şöyledir
int hashCode = 1;
for (E e : list)
  hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
Örnek
Elimizde şöyle bir kod olsun. Bu kod sonsuz döngüye girer ve StackOverflowError fırlatır.
ArrayList<ArrayList> list = new ArrayList();
list.add(list);
list.hashCode();
of metodu
List.of metodu yazısına taşıdım

sublist metodu
List.subList metodu yazısına taşıdım

sort metodu
Java 8 ile geldi ve in-place sıralama yapıyor.

replaceAll metodu
Java 8 ile geldi.