
Play Store Application link – Java to Dart in 10 Steps – App on Google Play
Welcome to the world of collections and generics in Dart! If you’re coming from a Java background, you’ll find many similarities but also some interesting differences. In this post, we’ll dive into Dart’s Lists, Sets, Maps, and Generics, drawing parallels with Java to make the learning curve easier.
1. Lists
Lists in Dart are similar to arrays in Java, but more flexible. They allow you to store a collection of items.
Creating and Manipulating Lists
In Dart, lists are dynamic and can grow or shrink in size. Here’s how you can create and manipulate them:
- Java (using
ArrayList
):import java.util.ArrayList;
import java.util.List;
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.remove("Apple");
System.out.println(fruits);
- Dart:
void main() {
var fruits = <String>['Apple', 'Banana'];
fruits.remove('Apple');
print(fruits); // [Banana]
}
In Dart, lists are created using square brackets ([]
), and you can manipulate them with methods like add()
and remove()
.
List Methods and Properties
Dart’s lists come with a variety of useful methods:
- Java:
System.out.println(fruits.size()); // Size of the list System.out.println(fruits.contains("Banana")); // Check if list contains"Banana"
- Dart:
print(fruits.length); // Size of the list print(fruits.contains('Banana')); // Check if list contains "Banana"
Dart uses properties like length
to get the size of the list, and methods like contains()
to check for the presence of an item.
2. Sets
Sets in Dart are similar to HashSet
in Java. They store unique items and are useful when you need to ensure that no duplicates are present.
Creating and Using Sets
- Java:
import java.util.HashSet;
import java.util.Set;
Set<String> colors = new HashSet<>();
colors.add("Red");
colors.add("Green");
colors.add("Red"); // Duplicate, will not be added System.out.println(colors);
- Dart:
void main() {
var colors = <String>{'Red', 'Green'};
colors.add('Red'); // Duplicate, will not be added
print(colors); // {Red, Green} }
In Dart, sets are created using curly braces ({}
) and automatically handle duplicates.
Set Methods and Properties
Dart sets also have methods for common operations:
- Java:
System.out.println(colors.size()); // Size of the set System.out.println(colors.contains("Green")); // Check if set
contains "Green"
- Dart:
print(colors.length); // Size of the set print(colors.contains('Green')); // Check if set contains "Green"
The length
property in Dart provides the size of the set, while contains()
checks for the presence of an item.
3. Maps
Maps in Dart are similar to Java’s HashMap
. They store key-value pairs, where each key is unique.
Creating and Using Maps
- Java:
import java.util.HashMap;
import java.util.Map;
Map<String, String> capitals = new HashMap<>();
capitals.put("USA", "Washington, D.C.");
capitals.put("Japan", "Tokyo");
System.out.println(capitals.get("USA"));
- Dart:
void main() {
var capitals = <String, String>{
'USA': 'Washington, D.C.',
'Japan': 'Tokyo'
};
print(capitals['USA']); // Washington, D.C. }
In Dart, maps are created using curly braces with key-value pairs separated by a colon (:
).
Map Methods and Properties
Dart’s maps provide a range of methods and properties for working with key-value pairs:
- Java:
System.out.println(capitals.size()); // Size of the map System.out.println(capitals.containsKey("Japan")); // Check if map contains the key "Japan"
- Dart:
print(capitals.length); // Size of the map print(capitals.containsKey('Japan')); // Check if map contains the key "Japan"
Dart uses length
to get the size of the map, and containsKey()
to check if a particular key exists.
4. Generics
Generics in Dart are quite similar to Java, allowing you to create classes and functions that can operate on different types while providing compile-time type safety.
Generic Classes and Functions
- Java:
public class Box<T> {
private T item;
public void setItem(T item) {
this.item = item;
}
public T getItem() { return item; }
}
- Dart:
class Box<T> {
T? item;
void setItem(T value) {
item = value;
}
T? getItem() {
return item;
}
}
In Dart, you use generic classes and functions by specifying the type in angle brackets (<T>
). Dart also uses ?
to indicate that a type can be nullable.
Constraints on Generics
Java allows you to constrain generics to specific types. Dart provides similar functionality with extends
:
- Java:
public class Box<T extends Number> {
// T must be a subclass of Number
}
- Dart:
class Box<T extends num> {
// T must be a subtype of num
}
In Dart, extends
is used to constrain generics to subclasses of num
, similar to Java’s extends Number
.