CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
nikhil singla
Level 9
Chandigarh
  • 27.07.2019
  • 797views
  • 1comment

What's wrong here? Last condition not satisfying with this..

Question about the task Greater than 10? You're not a good fit for us
Java Syntax,  Level 8,  Lesson 8
Under discussion

Create a set of numbers (Set<Integer>) and add 20 different numbers to it.
Remove from the set all numbers greater than 10 .

Requirements:
  • The program should not display text on the screen.
  • The program should not read values from the keyboard.
  • The Solution class must have only three methods.
  • The createSet() method must create and return a HashSet containing 20 different numbers.
  • The removeAllNumbersGreaterThan10() method must remove from the set all numbers greater than 10.
package com.codegym.task.task08.task0814; import java.util.HashSet; import java.util.Set; /* Greater than 10? You're not a good fit for us */ public class Solution { public static HashSet<Integer> createSet() { // write your code here HashSet<Integer> numbers = new HashSet<Integer>(); numbers.add(1); numbers.add(2); numbers.add(3); numbers.add(4); numbers.add(5); numbers.add(6); numbers.add(7); numbers.add(8); numbers.add(9); numbers.add(10); numbers.add(11); numbers.add(12); numbers.add(13); numbers.add(14); numbers.add(15); numbers.add(16); numbers.add(17); numbers.add(18); numbers.add(19); numbers.add(20); return numbers; } public static HashSet<Integer> removeAllNumbersGreaterThan10(HashSet<Integer> set) { // write your code here for(Integer num : set){ if(num > 10){ set.remove(num); } } return set; } public static void main(String[] args) { } }
0
Comments (1)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Nithin
Level 35 , Hyderabad, India
27 July 2019, 16:00useful
You cannot iterate over a set and remove elements from it simultaneously. Use iterator to iterate to do the task.
+1
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.