Przejdź do głównej zawartości

Java Collection - Kolekcje w Java

Podział kolekcji w Java-ie:

Implementowane są interfejsy kolor zielony w klasach kolor fiolet




Kolekcje mogą być generyczne i niegeneryczne
  • generyczne - zdefiniowany typ jaki będą przechowywać
  • niegeneryczne - przechowują wszystko co do nich wpadnie co w nich umieścimy
ArrayList:
  • wolniejsze w manipulacji
  • elementy sa obok siebie
  • wolne dodawanie elementów
LinkedList:
  • szybkie umieszczanie obiektów
  • wolniejsze odczytywanie szukanie
Linkedlist – dodawanie elementów tam gdzie jest miejsce w pamięci i jednoczesne dodawanie referencji gdzie ten element się znajduje, to jest plus w kontekście ArrayList która musi przenieść cały ArrayList jeśli braknie miejsca. LinkedList pamięta referencje do lokowanych obiektów.

Obiekt w LinkedList jest jakby opakowywany tj zawiera informacje nie tylko o sobie ale również posiada referencje do poprzednika i następnika. 

Obiekt w LinkedList zawiera: 
  • Obiekt sam w sobie 
  • Ref do następnika 
  • Ref do poprzednika 
Wrappery - obiekt, który ma otoczyć dany obiekt i rozszerzyć jego funkcjonalność.


Dzięki temu że odwołujemy się do typu wyższego otrzymujemy elastyczność jeśli będzie trzeba zamienić ArrayList na LinkedList lub odwrotnie: 


private List<Book> bookList = new LinkedList<>();

ArrayList-y można ze sobą łączyć z wykorzystaniem metody .add


Generycznosć w ArrayLilst dotyczy typów obiektowych nie można zrobić czegoś takiego jak  

List<int> trzeba stosować List<Integer> 

int typu małe literały

Iteger – typ obiektowy

public final class Integerextends Numberimplements Comparable<Integer> 

The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int. 

In addition, this class provides several methods for converting an int to a String and a String to an int, as well as other constants and methods useful when dealing with an int. 


Komentarze

Popularne posty z tego bloga

Java -Arrays

Array - data structure that allows us to store multiple values of the same type into a single variable Default value - zero Arrays are zero indexed - Array with ten elements will be indexed from 0 to 9 Array[] How to create array: int[] array = new int[5]; (boxes have value zero - secured slot in memory to hold values) int[] myArray = {1,2,3,4,5}; this type of initializing an array is also known as an anonymous array boolean type arrays - default value is false Errors while working with arrays ArrayIndexOutOfBoundsExeption int[] my Array = {10,35,45,78,55} myArray[5] = 55; out of bound index 5 does not exist int[] myArray = {10,15,20,17,18}; for (int i=1 ; i < myArray.lenght; i++)  System.out.println("value= " + myArray[i] it will return values from index 1 How to print out simple int array? example: int [] myArray = new int [ 2 ] ; myArray[ 0 ]= 0 ; myArray[ 1 ]= 1 ; System. out .println( "My array: " + ...

SQL - Podstawy

SELECT * FROM branch; CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, .... ); Typy danych w kolumnach Text data types: VARCHAR (40) - string z ograniczoną liczbą znaków TEXT - string BLOB - Binary Larg Objects - 65000 bytes of data ENUM('X','Y','Z') - list of values in order we enter them SET - values are ENUM up to 64 list items    Number data types: INT (size) - integer FLOAT(size,d) - small number with floating decimal point, d - decimal point DOUBLE(size,d) large number with floating decimal point Date data types: DATE() - YYYY-MM-DD TIMESTAMP() TIME() SQL Keywords: ADD - add column ALTER - add delete modify columns in table or change data types of column in table CREATE - create database, table, index, view, procedure CREATE DATABASE CREATE TABLE CREATE INDEX CREATE VIES DELETE - delete rows from table DESC - sort results descending order DROP - delet...

Sorting algor

BubbleSort // BubbleSort System . out . println ( "BubbleSort" ); for ( int i = 0 ; i < data . length - 1 ; i ++){ for ( int j = 0 ; j < data . length - 1 - i ; j ++){ if ( data [ j ] > data [ j + 1 ]) { int temp = data [ j ]; data [ j ]= data [ j + 1 ]; //swap data [ j + 1 ]= temp ; } } } mało efektywny z uwagi na podwójny loop tylko do małych data sets efektywność O(n^2) SelectionSort raczej wolny algorytm z uwagi na podwójny loop małe data set efektywność O(n^2) System . out . println ( "SelectionSort" ); int i , j , minV , minI , temp = 0 ; for ( i = 0 ; i < data . length ; i ++){ minV = data [ i ]; minI = i ; for ( j = i ; j < data . length ; j ++){ if ( data [...