Note No. 1 "Counting and displaying duplicate values in an array" [Java]


public static void main(String[] args) {

// TODO auto-generated method stub

    String [] array = {"y","x","x","y","z","y","x","y","z","z"};
    List<String> list = new ArrayList<>(Arrays.asList(array));
    TreeSet<String> ts = new TreeSet<>(list);

    for(String s : ts) {
        int count = 0;
        for(String ss : list) {
            if(s.equals(ss)) {
        System.out.println(s + " " + count);

[Output result] x 3 y 4 z 3

[point] -The set of the collection was used so that the output result would not be displayed in duplicate. -I used TreeSet because I wanted to display in alphabetical order. -Arrays.asList () returned the array as a list.

Recommended Posts

Note No. 1 "Counting and displaying duplicate values in an array" [Java]
Get attributes and values from an XML file in Java
[Java] Declare and initialize an array
Map without using an array in java
[Java] Understand in 10 minutes! Associative array and HashMap
[Java] Program example to get the maximum and minimum values from an array
Reverse Enum constants from strings and values in Java
Gzip-compress byte array in Java and output to file
A program (Java) that outputs the sum of odd and even numbers in an array
Note No.5 "HashMap value has multiple values using class" [Java]
What happened in "Java 8 to Java 11" and how to build an environment
Generate AWS Signature V4 in Java and request an API
Generate Stream from an array of primitive types in Java
After switching between Java 8 and 11 versions, an error occurs in confirmation
[Java] Shallow copy and deep copy when converting an array to List
Cast an array of Strings to a List of Integers in Java
I sent an email in Java
Java learning memo (creating an array)
Encoding and Decoding example in Java
Java arguments, return values and overloads
Get Null-safe Map values in Java
Try an If expression in Java
StringBuffer and StringBuilder Class in Java
Write keys and values in Ruby
[Java] Difference between array and ArrayList
Understanding equals and hashCode in Java
I made an annotation in Java.
Hello world in Java and Gradle
Run an external process in Java
Note: Assigning and displaying values to multidimensional arrays, and practicing extended for statements
A note on the differences between interfaces and abstract classes in Java
I want to ForEach an array with a Lambda expression in Java