Created
August 6, 2017 13:12
-
-
Save P0huber/20848227ea887028c12c4068bbb4c922 to your computer and use it in GitHub Desktop.
Creation HashSet and removal elements according to the condition. Создание HashSet и удаление элементов в нем [Java]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task08.task0814; | |
import java.util.HashSet; | |
import java.util.Iterator; | |
/* OPTION 2 | |
Больше 10? Вы нам не подходите | |
*/ | |
public class WorkWithHashSet2 { | |
public static HashSet<Integer> createSet() { | |
HashSet<Integer> set = new HashSet<>(); | |
for(int i = 0; i < 20; i++) | |
set.add(i);//the creation Set and adding 20 integers | |
return set; | |
} | |
public static HashSet<Integer> removeAllNumbersMoreThan10(HashSet<Integer> set) { | |
Iterator<Integer> i = set.iterator(); | |
while (i.hasNext()) { | |
Integer a = i.next(); | |
if (a > 10) | |
i.remove();}//the removal iterator according to the condition | |
//System.out.println(set);//for check | |
return set; | |
} | |
public static void main(String[] args) { | |
removeAllNumbersMoreThan10(createSet()); | |
} | |
} | |
/*Больше 10? Вы нам не подходите | |
Создать множество чисел(Set<Integer>), занести туда 20 различных чисел. | |
Удалить из множества все числа больше 10. | |
Требования: | |
1. Программа не должна выводить текст на экран. | |
2. Программа не должна считывать значения с клавиатуры. | |
3. Класс Solution должен содержать только три метода. | |
4. Метод createSet() должен создавать и возвращать множество HashSet состоящих из 20 различных чисел. | |
5. Метод removeAllNumbersMoreThan10() должен удалять из множества все числа больше 10.*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment