010-SET-MAP data structure (SET/WEAKSET/MAP/WEAKMAP)

Source: Internet
Author: User
Tags foreach data structures map data structure new set set set
/** * * SET-MAP data structure (SET/WEAKSET/MAP/WEAKMAP) * Set is similar to array, but the elements in the collection cannot be duplicated * Map is similar to object, but the key value can be any data type * * Weakset and set differences * 1. Supported data types are different * weakset elements can only be objects * 2.WeakSet objects are a weak reference (does not detect whether the object is used elsewhere, is an address reference) * 3 . Weakset compared to set * No. Clear (). Size () method * and cannot traverse * 4. Usage is basically consistent with set * * Weakmap and map differences * with Weakset 
  Consistent with SET/*/******************************************************************* set ********/{//set Common definition method
  Let list = new Set ();  List.add (5);  Add a value of 5 list.add (7); Add a value of 7 console.log (' Size ', list.size);
  Get Set Data length} {//2nd define Set data method Let arr = [1,2,3,4,5];

  Let list = new Set (arr); Console.log (' size ', list.size);
  Gets the data length of the set} {//set element in the collection, must be unique (attribute applied: deduplication) Let list = new Set ();
  List.add (1);
  List.add (2); List.add (1); Repeat (Unable to add successfully) Console.log (' list ', list);
  1 2 Let arr=[1,2,3,1, ' 2 ']; Let List2=new Set (arr); The data type Console.log (' unique ', List2) is not converted during the feature deduplication process; 1 2 3 ' 2 '
} {//set Common method let arr=[' add ', ' delete ', ' clear ', ' have '];

  Let List=new Set (arr); Console.log (' has ', List.has (' Add ')); Determines if there is an element in the set set {return Boolean} console.log (' Delete ', List.delete (' Add '), list); Describes an element {return Boolean} list.clear () in the set set.
Empties the elements in the set set Console.log (' list ', list);
  } {//set data traversal (similar to array) let arr=[' add ', ' delete ', ' clear ', ' have '];

  Let List=new Set (arr);
  For (Let Key of List.keys ()) {//.keys () gets the SET Data key value Console.log (' Keys ', key);
  } for (Let value of List.values ()) {//.valuse () gets the set data values value Console.log (' value ', value);
  } for (let [key,value] of list.entries ()) {//.entries () gets the set data key+values value Console.log (' entries ', key,value);
    } List.foreach (function (item) {Console.log (item); })}/******************************************************************* WeakSet ********/{let WeakLis

  T=new WeakSet ();

  Let arg={};

  Weaklist.add (ARG);   Weaklist.add (2); Adding non-object data types will directly error Console.log (' WeakliSt ', weaklist);  }/******************************************************************* Map ********/{//BASE usage let map = new
  Map ();

  Let arr=[' 123 ']; Map.set (arr,456); add element (with arr array as key) Console.log (' Map ', Map,map.get (arr)); . Get () Gets the values of a key} {//2nd defined in the way let map = new map ([[' A ', 123],[' B ', 456]]);//define Map Console.log (' map args ', map)
  ;  Console.log (' size ', map.size); Gets the length of the map data console.log (' delete ', Map.delete (' a '), map); Delete {return Boolean} console.log (' Clear ', map.clear (), map); Empty {return Boolean}}/** * The traversal of the map data is consistent with the set data (no longer enumerated) *//*************************************************************

  Weakmap ********/{//weakmap Key value must be object let Weakmap=new weakmap ();
  Let o={};
  Weakmap.set (o,123);
Console.log (Weakmap.get (o)); }/******************************************************************* comparison of map and array ********/{//map and array
  (conclusion, map is simpler than array)//Data structure cross-contrast, increase, check, change, delete let map=new map ();
  Let array=[]; //Increased map.set (' t ', 1);

  Array.push ({t:1});

  Console.info (' Map-array ', map,array);
  Check let Map_exist=map.has (' t ');
  Let Array_exist=array.find (ITEM=>ITEM.T);

  Console.info (' Map-array ', map_exist,array_exist);
  Change Map.set (' t ', 2);
  Array.foreach (item=>item.t?item.t=2: ");

  Console.info (' map-array-modify ', map,array);
  Delete map.delete (' t ');
  Let Index=array.findindex (ITEM=>ITEM.T);
  Array.splice (index,1);
Console.info (' Map-array-empty ', map,array); }/******************************************************************* set vs. Array ********/{//Set and ARRA
  The comparison of Y (conclusion: both set and map operations are relatively complex) let set=new set ();

  Let array=[];
  Add Set.add ({t:1});

  Array.push ({t:1});

  Console.info (' Set-array ', set,array);
  Check let Set_exist=set.has ({t:1});
  Let Array_exist=array.find (ITEM=>ITEM.T);

  Console.info (' Set-array ', set_exist,array_exist);
  Change Set.foreach (item=>item.t?item.t=2: ");
  Array.foreach (item=>item.t?item.t=2: "); Console.info ('Set-array-modify ', Set,array);
  Delete Set.foreach (Item=>item.t?set.delete (item): ");
  Let Index=array.findindex (ITEM=>ITEM.T);
  Array.splice (index,1);
Console.info (' Set-array-empty ', set,array); }/******************************************************************* Map,set,object vs. ********/{//Map,
  Set,object vs Let item={t:1};
  Let map=new map ();
  Let Set=new set ();

  Let obj={};
  Increased map.set (' t ', 1);
  Set.add (item);

  obj[' t ']=1;

  Console.info (' Map-set-obj ', obj,map,set); Check Console.info ({map_exist:map.has (' t '), Set_exist:set.has (item), Obj_exist: ' t ' in obj})//change map
  . Set (' t ', 2);
  item.t=2;
  obj[' t ']=2;

  Console.info (' map-set-obj-modify ', obj,map,set);
  Delete map.delete (' t ');
  Set.delete (item);
  Delete obj[' t '];
Console.info (' Map-set-obj-empty ', obj,map,set);    }/** ********************************************************************************************** * Draw a conclusion by comparison: * When manipulating array objects: Map is relatively simple compared to Set/array *    When manipulating objects: Map is less expensive than set/object (not much) * * In comparison with the object and array data structures, the map data structure should be preferred for storage, * but if the data storage is considered Uniqueness, you should use the set data structure for storage * */

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.