Created
August 16, 2020 18:21
-
-
Save kryvoboker/ad4b93f70565cd1905839eb932af7403 to your computer and use it in GitHub Desktop.
HomeWork8(Хэш таблицы Map)(3)
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
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Random; | |
import java.util.Set; | |
public class Main { | |
public static void main(String[] args) { | |
int[] array = generateArray(20); | |
Map<Integer, Integer> numbers = count(array); | |
Set<Integer> keys = numbers.keySet(); | |
for (Object key : keys) { | |
System.out.println(key + " -> " + numbers.get(key)); | |
} | |
System.out.println("---------------------"); | |
System.out.println(Arrays.toString(array)); | |
System.out.println("---------------------"); | |
String[] arrayText = new String[]{"aaa", "bbb", "ccc", "aaa", "bbb", "ccc", "AAA", "BBB", "AAA", "dog", "rebbit", "horse", "rebbit"}; | |
Map<String, Integer> text = countString(arrayText); | |
Set<String> keyss = text.keySet(); | |
for (Object key : keyss) { | |
System.out.println(key + " -> " + text.get(key)); | |
} | |
System.out.println("---------------------"); | |
System.out.println(Arrays.toString(arrayText)); | |
} | |
public static Map<String, Integer> countString(String[] array) { | |
Map<String, Integer> text = new HashMap<>(); | |
for (String i : array) { | |
if (text.keySet().contains(i)) { | |
text.put(i, text.get(i) + 1); | |
} else { | |
text.put(i, 1); | |
} | |
} | |
return text; | |
} | |
public static Map<Integer, Integer> count(int[] array) { | |
Map<Integer, Integer> number = new HashMap<>(); | |
for (int i : array) { | |
if (number.keySet().contains(i)) { | |
number.put(i, number.get(i) + 1); | |
} else { | |
number.put(i, 1); | |
} | |
} | |
return number; | |
} | |
public static int[] generateArray(int n) { | |
int[] array = new int[n]; | |
Random rn = new Random(); | |
for (int i = 0; i < array.length; i += 1) { | |
array[i] = rn.nextInt(10); | |
} | |
return array; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment