Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart

The HashSet.skipwhile() method in Dart

Abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The skipWhile() method accepts a boolean test function as a parameter. Every element in the hash set is passed through the boolean test function. The elements that pass the test function (the function returns true) are skipped. In contrast, the elements that fail the test function are retained (the function returns false).

The iteration over the elements of the set is stopped once an element for which the test function fails. The remaining elements in the set are returned as is.

Syntax

hashset.skipWhile(test_function)

Parameters

  • test_function: A boolean function.
  • skipWhile: Skips an element until it passes the function.

Return value

The method returns an iterable value.

Example

import 'dart:collection';

void main() {
  var educativeSet = HashSet<String>();
   
  educativeSet.add("courses"); 
  educativeSet.add("answers");
  educativeSet.add("educative");
  educativeSet.add("assessments");
  educativeSet.add("projects");

  print('1.\tThe elements of the set are:\n\t $educativeSet');
  var iterator = educativeSet.skipWhile((element) => element.length > 7);
  print('2.\tAfter skipping the elements with length greater than 7: \n\t $iterator');
}  
skipWhile() method

Explanation

  • Line 4: An instance of a HashSet class of type String is created.
  • Lines 6–10: Using the add() method, elements are added to the hash set.
  • Line 12: The elements of the hash set are printed.
  • Line 13: The initial elements whose length is greater than 7 are skipped using the skipWhile() method.
  • Line 14: The returned iterable values are printed.

RELATED TAGS

dart

CONTRIBUTOR

Abhilash
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring