Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Introduction to Data Structures | Basic Data Structures
course content

Зміст курсу

Java Data Structures

Introduction to Data StructuresIntroduction to Data Structures

Attention! It will be quite challenging for you to take this course if you haven't completed the three previous Java courses on our platform! Therefore, I strongly recommend completing these three courses before starting the Java Data Structures course.
  1. Java Basics
  2. Java Extended
  3. Java OOP

What are Collections, and Why Are They Needed?

Collections in Java are one of the data structures that are used very frequently. A data structure, in turn, is a way of storing various data types.

In simple terms, a collection is a list of data or variables of a specific type.
While arrays are static, meaning they have a fixed size defined during initialization, collections can be thought of as dynamic arrays. They expand as elements are added to them. So, when you add an element to the list, the size of the list increases until it can accommodate all the elements.

In Java, a collection refers to a group of objects that are used to store, process, and manipulate data. Collections provide convenient and efficient data structures, such as lists, sets, queues, and maps.

Collections can help us understand how a database works because, with collections, we can also store a large amount of data. In collections, we can store objects of different classes. We can even store arrays in collections, which is a very convenient way to store a large amount of data.

Wrappers

To start working with collections, it's worth noting that collections cannot operate with primitive data types. They specifically work with objects. To store a simple number or letter, we need to use a wrapper class for the primitive data type.

In Java, wrapper classes provide an object-oriented representation for primitive data types. In Java, primitive data types (such as int, char, float, boolean, and others) are not objects, and sometimes there's a need to work with objects, for example, in the context of collections or when dealing with classes that expect objects. Wrapper classes offer an object-oriented wrapper for each primitive type.

Here's a list of wrapper classes for primitive types:

  1. Integer: Wrapper for the int type;
  2. Long: Wrapper for the long type;
  3. Float: Wrapper for the float type;
  4. Double: Wrapper for the double type;
  5. Character: Wrapper for the char type;
  6. Boolean: Wrapper for the boolean type;
  7. Byte: Wrapper for the byte type;
  8. Short: Wrapper for the short type.

These classes provide methods for converting between primitive types and objects, and they offer various methods for working with values, such as comparison, arithmetic operations, and more.

Let's consider a few examples of using wrapper classes:

java

main.java

In the code above, we created an object of the Integer class and initialized it with a regular number. This way, we used autoboxing. Next, we created a primitive int and initialized it with the value of the wrapper. This is called unboxing.

From this, it can be understood that autoboxing is the automatic conversion of a value from a primitive data type to an object of the wrapper class. Unboxing is the automatic conversion of a value from an object of the wrapper class to a primitive data type.

You can also see how we used the comparison method. This comparison method returns 0 if the data are equal, 1 if the left value is greater than the right, and -1 if the left value is less than the right.

Wrapper classes have many different methods; we won't cover all of them now, but you can explore them locally in your IDE.

I suggest practicing a bit and modifying the compareTo() method so that instead of numeric indicators, we get full-text messages telling us which number is greater:

Завдання

Your task is to handle the value returned by the compareTo() method so that instead of 0, information indicates that the values are equal. Instead of 1, information indicates that the left value is greater than the right, and instead of -1, information indicates that the right value is greater than the left.

Note

Implement the upgrade_comparing method to enhance everything correctly and return a value with the String type.

To pass the task check, you must display "The right value is greater" if the value of right_value is greater; "The values are equal" if the values are equal; "The left value is greater" if the value of left_value is greater. The console outputs should be exactly as specified.

Все було зрозуміло?

Секція 1. Розділ 1
toggle bottom row
course content

Зміст курсу

Java Data Structures

Introduction to Data StructuresIntroduction to Data Structures

Attention! It will be quite challenging for you to take this course if you haven't completed the three previous Java courses on our platform! Therefore, I strongly recommend completing these three courses before starting the Java Data Structures course.
  1. Java Basics
  2. Java Extended
  3. Java OOP

What are Collections, and Why Are They Needed?

Collections in Java are one of the data structures that are used very frequently. A data structure, in turn, is a way of storing various data types.

In simple terms, a collection is a list of data or variables of a specific type.
While arrays are static, meaning they have a fixed size defined during initialization, collections can be thought of as dynamic arrays. They expand as elements are added to them. So, when you add an element to the list, the size of the list increases until it can accommodate all the elements.

In Java, a collection refers to a group of objects that are used to store, process, and manipulate data. Collections provide convenient and efficient data structures, such as lists, sets, queues, and maps.

Collections can help us understand how a database works because, with collections, we can also store a large amount of data. In collections, we can store objects of different classes. We can even store arrays in collections, which is a very convenient way to store a large amount of data.

Wrappers

To start working with collections, it's worth noting that collections cannot operate with primitive data types. They specifically work with objects. To store a simple number or letter, we need to use a wrapper class for the primitive data type.

In Java, wrapper classes provide an object-oriented representation for primitive data types. In Java, primitive data types (such as int, char, float, boolean, and others) are not objects, and sometimes there's a need to work with objects, for example, in the context of collections or when dealing with classes that expect objects. Wrapper classes offer an object-oriented wrapper for each primitive type.

Here's a list of wrapper classes for primitive types:

  1. Integer: Wrapper for the int type;
  2. Long: Wrapper for the long type;
  3. Float: Wrapper for the float type;
  4. Double: Wrapper for the double type;
  5. Character: Wrapper for the char type;
  6. Boolean: Wrapper for the boolean type;
  7. Byte: Wrapper for the byte type;
  8. Short: Wrapper for the short type.

These classes provide methods for converting between primitive types and objects, and they offer various methods for working with values, such as comparison, arithmetic operations, and more.

Let's consider a few examples of using wrapper classes:

java

main.java

In the code above, we created an object of the Integer class and initialized it with a regular number. This way, we used autoboxing. Next, we created a primitive int and initialized it with the value of the wrapper. This is called unboxing.

From this, it can be understood that autoboxing is the automatic conversion of a value from a primitive data type to an object of the wrapper class. Unboxing is the automatic conversion of a value from an object of the wrapper class to a primitive data type.

You can also see how we used the comparison method. This comparison method returns 0 if the data are equal, 1 if the left value is greater than the right, and -1 if the left value is less than the right.

Wrapper classes have many different methods; we won't cover all of them now, but you can explore them locally in your IDE.

I suggest practicing a bit and modifying the compareTo() method so that instead of numeric indicators, we get full-text messages telling us which number is greater:

Завдання

Your task is to handle the value returned by the compareTo() method so that instead of 0, information indicates that the values are equal. Instead of 1, information indicates that the left value is greater than the right, and instead of -1, information indicates that the right value is greater than the left.

Note

Implement the upgrade_comparing method to enhance everything correctly and return a value with the String type.

To pass the task check, you must display "The right value is greater" if the value of right_value is greater; "The values are equal" if the values are equal; "The left value is greater" if the value of left_value is greater. The console outputs should be exactly as specified.

Все було зрозуміло?

Секція 1. Розділ 1
toggle bottom row
some-alt