{"id":12900,"date":"2023-03-31T11:00:00","date_gmt":"2023-03-31T02:00:00","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=12900"},"modified":"2023-03-31T11:25:56","modified_gmt":"2023-03-31T02:25:56","slug":"dart-object-list%e3%81%aekey-value%e3%81%a7%e9%87%8d%e8%a4%87%e3%82%92%e5%89%8a%e9%99%a4%e3%81%99%e3%82%8b%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/12900","title":{"rendered":"Dart Object List\u306eKey-value\u3067\u91cd\u8907\u3092\u524a\u9664\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"\n<p>Dart\u3067Key-Value\u306b\u3088\u308bObject\u306e\u30ea\u30b9\u30c8\u304b\u3089\u91cd\u8907\u3092\u524a\u9664\u3059\u308b\u306b\u306f\u3001toSet()\u304c\u5229\u7528\u3067\u304d\u307e\u3059\u3002\u6700\u521d\u306btoSet\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u3001\u305d\u306e\u5f8c\u3001\u518d\u3073\u30ea\u30b9\u30c8\u306b\u5909\u63db\u3059\u308b\u3060\u3051\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u306b\u4f8b\u3092\u793a\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class _Product {\n  final name;\n  final type;\n  final price;\n  _Product({\n    required this.name,\n    required this.type,\n    required this.price,\n  });\n\n  String toJson() {\n    return \"{ name: $name, type: $type, price: $price}\";\n  }\n}<\/code><\/pre>\n\n\n\n<p>toSet\u3092\u547c\u3073\u51fa\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>final products = &#091;\n  _Product(name: \"USB\", type: \"A\", price: 10), \/\/ same\n  _Product(name: \"USB\", type: \"A\", price: 10), \/\/ same\n  _Product(name: \"USB\", type: \"B\", price: 12),\n  _Product(name: \"USB\", type: \"C\", price: 11),\n  _Product(name: \"Mouse\", type: \"A\", price: 10),\n  _Product(name: \"Mouse\", type: \"B\", price: 12), \/\/ same\n  _Product(name: \"Mouse\", type: \"B\", price: 12), \/\/ same\n  _Product(name: \"Mouse\", type: \"C\", price: 10),\n  _Product(name: \"Laptop\", type: \"A\", price: 100),\n  _Product(name: \"Laptop\", type: \"B\", price: 120), \/\/ same\n  _Product(name: \"Laptop\", type: \"B\", price: 120), \/\/ same\n];\n\nprint(products.length); \/\/ 11\nprint(products.toSet().length); \/\/ 11<\/code><\/pre>\n\n\n\n<p>toSet()\u3067\u547c\u3073\u51fa\u3057\u3066\u3082\u3001\u540c\u3058\u9577\u3055\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u305d\u306e\u6642\u3001hashCode\u3092\u78ba\u8a8d\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>products.forEach((element) {\n    print(element.hashCode);\n    \/\/ 257416280\n    \/\/ 1045932166\n    \/\/ 730517725\n    \/\/ 586146378\n    \/\/ 215518337\n    \/\/ 471982393\n    \/\/ 775971279\n    \/\/ 335683510\n    \/\/ 844714385\n    \/\/ 633234047\n    \/\/ 1021163746\n});<\/code><\/pre>\n\n\n\n<p>\u305d\u308c\u305e\u308c\u306ehashCode\u306f\u7570\u306a\u3063\u3066\u3044\u308b\u305f\u3081\u3001hashCode\u3068==\u6f14\u7b97\u5b50\u3092\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class _Product {\n  ...\n\n  @override\n  bool operator ==(Object other) {\n    return other is _Product &amp;&amp; other.hashCode == hashCode;\n  }\n\n  @override\n  int get hashCode =&gt; Object.hash(name, type, price);\n}<\/code><\/pre>\n\n\n\n<p>\u3053\u306e\u30af\u30e9\u30b9\u306f\u30ea\u30b9\u30c8\u3092\u6301\u3061\u307e\u305b\u3093\u304b\u3089\u3001Object.hash\u95a2\u6570\u3092\u4f7f\u7528\u3057\u307e\u3059\u304c\u3001\u30ea\u30b9\u30c8\u5909\u6570\u3092\u542b\u3080\u5834\u5408\u306f\u3001\u4ee3\u308f\u308a\u306bObject.hashAll\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3082\u3046\u4e00\u5ea6\u3001toSet\u3092\u547c\u3073\u51fa\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(products.length); \/\/ 11\nprint(products.toSet().length); \/\/ 8\nproducts.toSet().forEach((element) =&gt; print(element.toJson()));\n\/\/ { name: USB, type: A, price: 10}\n\/\/ { name: USB, type: B, price: 12}\n\/\/ { name: USB, type: C, price: 11}\n\/\/ { name: Mouse, type: A, price: 10}\n\/\/ { name: Mouse, type: B, price: 12}\n\/\/ { name: Mouse, type: C, price: 10}\n\/\/ { name: Laptop, type: A, price: 100}\n\/\/ { name: Laptop, type: B, price: 120}<\/code><\/pre>\n\n\n\n<p>\u306a\u304a\u3001\u91cd\u8907\u3059\u308b\u3082\u306e\u306f\u3053\u3053\u3067\u306f\u8868\u793a\u3057\u307e\u305b\u3093\u3067\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u91d1\u66dc\u62c5\u5f53 \u2013 Ami<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/12900\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/12900\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Dart Object List\u306eKey-value\u3067\u91cd\u8907\u3092\u524a\u9664\u3059\u308b\u65b9\u6cd5\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/12900\" data-text=\"Dart Object List\u306eKey-value\u3067\u91cd\u8907\u3092\u524a\u9664\u3059\u308b\u65b9\u6cd5\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/12900\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/12900\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Dart\u3067Key-Value\u306b\u3088\u308bObject\u306e\u30ea\u30b9\u30c8\u304b\u3089\u91cd\u8907\u3092\u524a\u9664\u3059\u308b\u306b\u306f\u3001toSet()\u304c\u5229\u7528\u3067\u304d\u307e\u3059\u3002\u6700\u521d\u306btoSet\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u3001\u305d\u306e\u5f8c\u3001\u518d\u3073\u30ea\u30b9\u30c8\u306b\u5909\u63db\u3059\u308b\u3060\u3051\u3067\u3059\u3002 \u4ee5\u4e0b\u306b\u4f8b\u3092\u793a\u3057\u307e\u3059\u3002 toSet\u3092\u547c [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/12900"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=12900"}],"version-history":[{"count":1,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/12900\/revisions"}],"predecessor-version":[{"id":12901,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/12900\/revisions\/12901"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=12900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=12900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=12900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}