Institute of Computer Science
  1. Courses
  2. 2019/20 spring
  3. Object-Oriented Programming (Narva College) (LTAT.NR.003)
ET
Log in

Object-Oriented Programming (Narva College) 2019/20 spring

  • Home
  • Materials
  • Grading
  • Java Glossary
  • Cheat sheet (S1-S6)
  • Source Example
  • Links
Chapter 12

Useful tips

Combine data structures

Sometimes it is useful to combine different data structures. For example, if we want to get to know which words follow the other words, we have to use a map whose keys are strings and their values are sets:

Map<String, Set<String>> wordSequences = new HashMap<>();
wordSequences.put("fish", new HashSet<>());
wordSequences.get("fish").add("swims");
wordSequences.get("fish").add("fingers");

Equals and Hashcode

Study the following code:

public class Client {
  private int clientNumber;
  public Client(int clientNumber) {
    this.clientNumber = clientNumber;
  }
}

public class Test {
  public static void main(String[] args) {
    Client myClient = new Client(123);
    Client theSameClient = new Client(123);
    List<Client> clients = Arrays.asList(myClient);
    System.out.println(clients.contains(theSameClient)); // false

    Integer clientNumber = new Integer(123);
    Integer theSameClientNumber = new Integer(123);
    List<Integer> clientNumber = Arrays.asList(clientNumber);
    System.out.println(clientNumber.contains(theSameClientNumber)); // true
  }
}

Why does the line 13 return the false value, and the line 18 - true? Aren't they analogical examples? The issue will be explained during the Algorithms and Data Structure course in detail. At the moment, keep in mind that Java data structures cannot compare the content of objects; however, they can compare the primitive data types (including String and generic data types).

Chapter 12
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment