Dart学习笔记——Set

void main() {
  for (int i = 0; i < 5; i++) {
    print('hello ${i + 1}');
  }
  var dd = [1, 2, 3, 4];
  for (int i = 0; i < dd.length; i++) {
    print('数组+${i}');
  }
  var list = ["1", "2", "newbie"];
  /**Set*/
  var mySet = {}; //弱类型时必须指定类型,否则将会被动态为Map
  var oneSet = new Set();
  var doubleSet = {1.00, 2.0, 3.1, 4.0, 5.0};
  final constSet = const {1, 2, 3, 4, 5}; //字面量Set
  mySet.add("1");
  mySet.add("2");
  oneSet.addAll(mySet);
  mySet.add("newbie3");
  //添加重复值
  mySet.add("newbie");
  mySet.add("newbie");
  //迭代器取值
  loopPrintSet(mySet, "mySet");
  loopPrintSet(oneSet, "oneSet");
  differ2Set(mySet, doubleSet);
  differ2Set(mySet, oneSet);
  differ2Set(doubleSet, constSet);
  intersectionFromList(list, mySet);
  //移除元素
  removeElement(mySet, "mySet");
  /**Map*/
  var anMap = Map();
  var anMap2 = Map();
}

//判断哪些Set元素重复 总结:判重有类型区分,int与double可互转相等
void differ2Set(Set set1, Set set2) {
  print('---------differ2Set()---------\n【1->${set1}】\n【2->${set2}】');
  var iterator = set1.iterator;
  while (iterator.moveNext()) {
    var element = iterator.current;
    var ele;
    try {
      ele = int.parse(element);
    } catch (e, stack) {}

    var contain = set2.contains(element);
    print('2含?$contain->${element}    转含?${set2.contains(ele)}');
  }

  //对比返回仅返回左边Set不同的元素
  var differ = set1.difference(set2);
  var differ2 = set2.difference(set1);
  print('不同元素12>${differ}');
  print('不同元素21>${differ2}');
}

//循环打印Set元素
void loopPrintSet(Set mySet, String flag) {
  print('---------【${flag}】loopPrintSet()---------');
  var iterator = mySet.iterator;
  while (iterator.moveNext()) {
    var dd = iterator.current;
    print("${flag}->Set迭代器" + dd);
  }
  //for循环取值
  for (int i = 0; i < mySet.length; i++) {
    var element = mySet.elementAt(i);
    print('${flag}->for循环${element}');
  }
  
  mySet.forEach((item){
      print('forEach循环 $item');
  });
  
}

//移除元素  总结:只能移除具体内容不可按位置移除
void removeElement(Set mySet, var setFlag) {
  print('---------【${setFlag}】removeElement()---------');
  print('${setFlag.runtimeType}');
  print('${mySet.runtimeType}');
  mySet.remove("1");

  print('remove1数据->${mySet}');
  mySet.clear();
  print('清空后数据->${mySet}');
}

//求两个set交集
void intersectionFromList(List list, Set setParam) {
  print('---------【intersectionFromList】---------\n$list\n$setParam');
  Set setTrans = Set.from(list);
  var result = setParam.intersection(setTrans);
  var result2 = setTrans.intersection(setParam);
  print(
      'set求交集$result\nlist求交集$result2\n 含bie否?${result.last.contains("bie")}');
}

直接编译楼梯

日志打印:
数组+0
数组+1
数组+2
数组+3
---------【mySet】loopPrintSet()---------
mySet->Set迭代器1
mySet->Set迭代器2
mySet->Set迭代器newbie3
mySet->Set迭代器newbie
mySet->for循环1
mySet->for循环2
mySet->for循环newbie3
mySet->for循环newbie
---------【oneSet】loopPrintSet()---------
oneSet->Set迭代器1
oneSet->Set迭代器2
oneSet->for循环1
oneSet->for循环2
---------differ2Set()---------
【1->{1, 2, newbie3, newbie}】
【2->{1, 2, 3.1, 4, 5}】
2含?false->1    转含?true
2含?false->2    转含?true
2含?false->newbie3    转含?false
2含?false->newbie    转含?false
不同元素12>{1, 2, newbie3, newbie}
不同元素21>{1, 2, 3.1, 4, 5}
---------differ2Set()---------
【1->{1, 2, newbie3, newbie}】
【2->{1, 2}】
2含?true->1    转含?false
2含?true->2    转含?false
2含?false->newbie3    转含?false
2含?false->newbie    转含?false
不同元素12>{newbie3, newbie}
不同元素21>{}
---------differ2Set()---------
【1->{1, 2, 3.1, 4, 5}】
【2->{1, 2, 3, 4, 5}】
2含?true->1    转含?false
2含?true->2    转含?false
2含?false->3.1    转含?false
2含?true->4    转含?false
2含?true->5    转含?false
不同元素12>{3.1}
不同元素21>{3}
---------【intersectionFromList】---------
[1, 2, newbie]
{1, 2, newbie3, newbie}
set求交集{1, 2, newbie}
list求交集{1, 2, newbie}
 含bie否?true
---------【mySet】removeElement()---------
String
_LinkedHashSet
remove1数据->{2, newbie3, newbie}
清空后数据->{}

你可能感兴趣的:(Dart学习笔记——Set)