Home > Java > javaTutorial > body text

Collection processing exercises in Java

WBOY
Release: 2023-06-15 09:52:37
Original
1439 people have browsed it

Java is an extremely popular programming language that is widely used in various scenarios, including web development, mobile application development, desktop applications, etc. Java provides a rich collection class library to help developers deal with various data structures, including arrays, linked lists, stacks, queues, and maps.

In Java, a collection is a container for storing data items. The Java collection class library can be divided into two hierarchies: collection interfaces and collection implementation classes. A collection interface is a set of specifications that defines a series of methods for operating on elements in a collection. The collection implementation class is based on the reality of the interface and provides specific implementation details, such as ArrayList, LinkedList, HashSet, etc.

In this article, we will introduce some common Java collection processing exercises to help readers improve their mastery of Java collection operations.

  1. Find the duplicate elements in the array

Given an integer array, find the duplicate elements in it. For example, for the array {1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 4, 5}, 1, 4 and 5 should be output.

One way to solve this problem is to use a HashSet. We can iterate through each element in the array and add them to the HashSet. If an element already exists in the HashSet, it is a duplicate. The following is the code implementation:

int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 4, 5};
Set<Integer> set = new HashSet<>();
Set<Integer> duplicates = new HashSet<>();

for (int i : arr) {
    if (!set.add(i)) {
        duplicates.add(i);
    }
}

System.out.println("重复元素为:" + duplicates);
Copy after login
  1. Merge two sorted arrays

Given two sorted integer arrays, merge them into a sorted array. For example, for the arrays {1, 3, 5, 7} and {2, 4, 6, 8}, {1, 2, 3, 4, 5, 6, 7, 8} should be output.

One way to solve this problem is to create a new array to hold the merged results. Then, we can use two pointers to point to the elements in the two arrays and compare their sizes. Adds the smaller element to the new array and moves the pointer forward one position. The following is the code implementation:

int[] arr1 = {1, 3, 5, 7};
int[] arr2 = {2, 4, 6, 8};
int len1 = arr1.length;
int len2 = arr2.length;
int[] result = new int[len1 + len2];

int i = 0, j = 0, k = 0;
while (i < len1 && j < len2) {
    if (arr1[i] < arr2[j]) {
        result[k++] = arr1[i++];
    } else {
        result[k++] = arr2[j++];
    }
}

while (i < len1) {
    result[k++] = arr1[i++];
}

while (j < len2) {
    result[k++] = arr2[j++];
}

System.out.println(Arrays.toString(result));
Copy after login
  1. Count the number of times each character appears in a string

Given a string, count the number of times each character appears in it. For example, for the string "Java is a great language", the character 'J' should appear 1 time, the character 'a' should appear 4 times, etc.

One way to solve this problem is to loop through each character in the string and add them to a HashMap. Treat each character as a key and the number of occurrences as a value. The following is the code implementation:

String str = "Java is a great language";
Map<Character, Integer> map = new HashMap<>();

for (int i = 0; i < str.length(); i++) {
    char c = str.charAt(i);

    if (c == ' ') {
        continue;
    }

    if (map.containsKey(c)) {
        map.put(c, map.get(c) + 1);
    } else {
        map.put(c, 1);
    }
}

for (Map.Entry<Character, Integer> entry : map.entrySet()) {
    System.out.println("字符'" + entry.getKey() + "'出现" + entry.getValue() + "次。");
}
Copy after login

Java collections are an important part of Java programming. This article introduces some common Java collection processing exercises to help readers deepen their understanding and application of Java collection operations.

The above is the detailed content of Collection processing exercises in Java. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!