Вы находитесь на странице: 1из 3

Laborator Java

(citire fisier, egalitatea obiectelor si


HashMaps)
1. Citire din fisier
Pentru a citi date din fisiere de pe disc, folosim clasele FileReader si
BufferedReader din pachetul java.io ; FileReader citeste datele dintr-un fisier, iar
BufferedReader ne ajuta sa adunam/grupam bitii cititi, astfel incat sa putem citi linie
cu linie:
String currentWorkingDir = System.getProperty("user.dir");
String filePath = currentWorkingDir + File.separator + "date" +
File.separator + "fileName.txt";
FileReader fr = new FileReader(filePath);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
while(line != null){
// proceseaza lina - foloseste datele la ceva...

// citeste urmatoarea linie


line = br.readLine();

// inchide mecanismele de citire


br.close();
fr.close();

Mecanismul de citire poate sa arunce exceptii de tipul FileNotFoundException sau


IOException

2. Egalitatea obiectelor
obj1 == obj2 compara referintele celor doua obiecte, si va returna true doar daca
cele doua obiecte au aceeasi referinta (reprezinta aceeasi instanta).
Pentru a verifica daca doua obiecte sunt egale dupa continutul lor, trebuie sa
suprascriem metoda equals din clasa Object;

Proprietati metoda equals (in engleza):

It is reflexive: for any non-null reference value

It is symmetric: for any non-null reference values


if y.equals(x) returns true.

It is transitive: for any non-null reference values x, y, and z,


if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.

It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or
consistently return false, provided no information used in equals comparisons on the objects is modified.

For any non-null reference value

x, x.equals(x) should return true.


x and y, x.equals(y) should return true if and only

x, x.equals(null) should return false.

Metoda hashCode returneaza un cod numeric (intreg) generat pe baza


continutului obiectului. Implementarea din Object a metodei hashCode
returneaza adresa din memorie a obictului respectiv.
O clasa care suprascrie metoda hashCode trebui sa suprascrie si metoda
equals!
Alte proprietati ale metodei hashCode (in engleza):

Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method
must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This
integer need not remain consistent from one execution of an application to another execution of the same application.

If two objects are equal according to the equals(Object) method, then calling the
the two objects must produce the same integer result.

It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling
the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be
aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

hashCode method on each of

Metoda hashCode este folosita in special de structurile de date indexabile


(HashSet, HashTable, HashMap).
3. HashSet, HashMap
Colectiile de date HashSet si HashMap tin obiecte indexate dupa o cheie unica.
HashSet e o colectie de chei, iar HashMap o colectie de elemente cheie
valoare. Elementele sunt salvate in HashMap la locatia explicitata de hashCode.

Cand mai multe elemente cu cheie diferita au acelasi hashCode, apare o


coliziune (colision), caz in care datele nu se pierd, ci sunt salvate pornind de la
aceeasi locatie in HashMap:

Experimentare in cod

Вам также может понравиться