четверг, 8 апреля 2021 г.

Collections

//List, Set, Map
void main() {

  //List
  var cities = []; // List<dynamic>
  var cities0 = <String>[]; // List<String>
  List<String> cities1 = ['London', 'Moscow', 'Minsk']; //explicit
  var cities2 = ['London', 'Moscow', 'Minsk'];  //implicit
  cities2[1] = 'Paris'; //change element
  cities2.add('Berlin'); //add element
  cities2.remove('Minsk'); //delete element
  print(cities2);

  //iterate list - access to elements
  for (var city in cities2) { //for-in loop
    print(city); //access by iterator
  }
  for (var i = 0; i < cities2.length; i++) { //for loop
    print(cities2[i]); //access by index
  }
  cities2.forEach((city) => print(city)); //forEach method
  cities2.forEach(print); //the same

  //properties and methods of lists
  var ints = [1, 3, 5, 7, 9];
  print(ints.length);
  print(ints.isEmpty);
  print(ints.isNotEmpty);
  print(ints.first);
  print(ints.last);
  ints.insert(1, 2);
  print(ints.removeAt(1));
  print(ints.contains(3));
  print(ints.indexOf(3));
  ints.clear();
  print(ints);

  //list mutability
  final vec1 = [1, 2, 3]; //runtime const
  //vec1 = [4, 5, 6]; //error!!!
  vec1[0] = 9; //OK!
  const vec2 = [4, 5, 6]; //compile time const
  //vec2[0] = 9; //error!!!
  final vec3 = const[4, 5, 6]; //the same
  //vec3[0] = 9; //error!!!
  final vac4 = List.unmodifiable([DateTime.now(), DateTime.now()]);
  //vec4[0] = 9; //error!!!

  //Sets - no duplicates, unordered
  Set<String> set = {};
  var set0 = <String>{}; //empty Set<String>
  var set1 = {'Italy', 'UK', 'Russia'}; //Set<String>
  set1.add('Italy');
  set1.addAll({'Spain', 'France'});
  print(set1);
  set1.remove('Italy');
  print(set1);
  //properties and methods
  print(set1.length);
  print(set1.first);
  print(set1.last);
  print(set1.isEmpty);
  print(set1.isNotEmpty);
  print(set1.elementAt(0));
  print(set1.contains('UK'));
  //set1.clear();
  var set2 = {'Russia', 'Belarus'};
  print(set1.union(set2));
  print(set1.intersection(set2));
  print(set1.difference(set2));
  //iterate
  for (var s in set1) {
    print(s);
  }
  set1.forEach(print);
  //...

  //Maps, 'as'-operator
  var map = {}; //Map<dynamic, dynamic>
  Map<String, int> map0 = {};
  var map1 = <String, int>{};
  var map2 = {'Tom': 34, 'Mike': 35,}; //key : value

  Map<String, Object> person = {
    'name': 'Vasili',
    'age': 48,
  };
  print(person['name']); //access to value by key
  person['name'] = 'Tom'; //change value
  person['man'] = true; //add new pair
  person.remove('man'); //delete pair
  print(person);
  print(person.keys);
  print(person.values);
  //iterate
  for (var e in person.entries) {
    print(e);
    print('key: ${e.key} - value: ${e.value}');
  }
  for (var k in person.keys) {
    print(k); //keys
  }
  for (var k in person.keys) {
    print(person[k]); //value by keys
  }
  for (var v in person.values) {
    print(v); //values
  }
  person.forEach((key, value) {print('$key -> $value'); });
  //properties and methods
  print(person.length);
  print(person['name'].runtimeType);
  var name = person['name'] as String; //casting Obj -> Str
  print(name.toUpperCase());
  print(person.isEmpty);
  print(person.isNotEmpty);
  print(person.containsKey('name'));
  print(person.containsValue(48));

  //Null - no value
  print('job: ${person['job']}'); //null

  //Nested Collections
  var students = [ //List<Maps<String, Object>>
    //list
    {
      //map
      'name': 'Mike',
      'marks': [5.0, 2.7, 3.6], //list
    },
    {
      'name': 'Tom',
      'marks': [3.0, 1.7, 0.6],
    },
    {
      'name': 'Fred',
      'marks': [7.0, 4.7, 5.6],
    },
  ];
  for (var student in students) {
    print(student.runtimeType);
    print(student.entries);
    final marks = student['marks'] as List<double>; //casting Obj -> List<dbl>
    print(marks.length);
  }

  //Collection-if
  const addBlue = false;
  const addRed = true;
  const colors = [
    'grey',
    'green',
    if (addBlue) 'blue',
    if (addRed) 'red',
  ];
  print(colors);

  const rates = [3.0, 4.0, 2.0];
  final students2 = {
    'name': 'Bob',
    if (rates.length > 3) 'rates': rates,
  };
  print(students2);

  //Collection-for
  const extraColors = ['yellow', 'black'];
  var colors1 = [
    'grey',
    'green',
    'red',
    for (var color in extraColors) color,
  ];
  print(colors1);
  colors1.addAll(extraColors);
  print(colors);

  //Spread operator (..., ...?) - expand elements
  final colors2 = [
    'grey',
    'green',
    'red',
    ...extraColors,
    if (true) ...['tmp1', 'tmp2']
  ];
  print(colors2);

  final students3 = {
    'name': 'Bob',
    if (true) ...{
      'rates': rates,
      'job': true,
    }
  };
  print(students3);

  //...?
  List<String>? pens;
  final draw = ['paper', ...?pens];

  //Copying = reference to collection
  final list = [1, 2, 3];
  final copy1 = list; //copy reference
  copy1[0] = 0;
  print('list: $list'); //list: [0, 2, 3]
  print('copy1: $copy1'); //copy1: [0, 2, 3]

  final copy2 = [
    for (var item in list) item, //copy values
  ];
  copy2[0] = 1;
  print('list: $list'); //list: [0, 2, 3]
  print('copy2: $copy2'); //list: [1, 2, 3]

  final copy3 = [...list];
  copy3[0] = 6;
  print('list: $list'); //list: [0, 2, 3]
  print('copy3: $copy3'); //list: [6, 2, 3]

}

Комментариев нет:

Отправить комментарий