package com.codegym.task.task08.task0814;
import java.util.HashSet;
import java.util.Set;
import java.util.Iterator;
/*
Greater than 10? You're not a good fit for us
*/
public class Solution {
public static HashSet<Integer> createSet() {
// write your code here
int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
HashSet<Integer> set = new HashSet<Integer>();
for(int aa: a)
set.add(aa);
return set;
}
public static HashSet<Integer> removeAllNumbersGreaterThan10(HashSet<Integer> set) {
// write your code here
for(Iterator<Integer> iterator = set.iterator(); iterator.hasNext();)
{
Integer aa = iterator.next();
if (aa > 10)
set.remove(aa);
}
return set;
}
public static void main(String[] args) {
}
}
Can anyone tell me what doesn't work here? Thanks!
Under discussion
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
10 January 2019, 21:26
That code looks like it will cause an error, you can't remove items from a collection that is actively being iterated through, not like that at least. Try making a copy of the set, iterate through the copy but remove items from the original. Also, next time don't copy and paste code, just use the slider available:
![]()

0