HashMap Values() Trong Java Với Ví Dụ Cụ Thể - Deft Blog

HomeJavaHashMap values() trong java với ví dụ cụ thể Tags:Java Map

Mục lục

  • 1 Convert Collection sang ArrayList
  • 2 Kết bài

HashMap values() trong java trả về một collection value của HashMap. Chúng ta có thể thao tác trực tiếp với Collection đó hoặc có thể convert sang ArrayList, LinkedList etc.

Syntax

public Collection<V> values(); import java.util.Collection; import java.util.HashMap; public class Main { public static void main(String[] args) { // Creating an empty HashMap HashMap<Integer, String> hash_map = new HashMap<>(); hash_map.put(1, "hga"); hash_map.put(2, "asd"); hash_map.put(3, "sho"); Collection<String> collections = hash_map.values(); for(String value : collections) { System.out.println(value); } } }

Output

hgaasdsho

Convert Collection sang ArrayList

Sau khi dùng values() để lấy được Collection value của HashMap, chúng ta có thể convert sang ArrayList để dễ dàng xử lý.

import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; public class Main { public static void main(String[] args) { // Creating an empty HashMap HashMap<Integer, String> hash_map = new HashMap<>(); hash_map.put(1, "hga"); hash_map.put(2, "asd"); hash_map.put(3, "sho"); Collection<String> collections = hash_map.values(); ArrayList<String> arr = new ArrayList<>(collections); System.out.println(arr); } }

Output

[hga, asd, sho]

Kết bài

Mọi người cố gắng làm hết để ôn lại các kiến thức trong lập trình hướng đối tượng nghen.

Nếu làm được hết các bài này thì cũng đừng có mừng vội, vì đây chỉ là những thứ rất rất căn bản. Nhưng hãy thoải moái vì mình đã hoàn thành những bài tập này, chuẩn bị cho những thứ ghê ghớm hơn ở đằng sau.

Mình ngồi giải những bài tập này cũng đuối quá. Nếu có gì không hiểu hoặc không biết làm, hoặc tìm thấy bug thì mail cho mình nhé. Email ở dưới cuối màn mình =). Thanks!

Các bài viết liên quan

  • ArrayList trong java
  • LinkedList trong java
  • HashSet trong java
  • HashMap trong java
  • Java annotation trong java
  • Java regex – part 1
  • Generic trong java
  • Tổng hợp bài tập java căn bản
Prev Article Next Article 0 0 votes Article Rating Subscribe Login Notify of new follow-up comments new replies to my comments guest Label {} [+] Name* Email* guest Label {} [+] Name* Email* 0 Comments Inline Feedbacks View all comments Load More Comments Học lập trình online

Bài viết liên quan

  • Cách sử dụng keySet() vs. entrySet() vs. values() trong Java

  • HashMap replaceAll() trong java với ví dụ cụ thể

  • HashMap replace(key, oldvalue, newValue)

  • HashMap replace() trong java với ví dụ cụ thể

  • HashMap putIfAbsent trong java với ví dụ cụ thể

  • HashMap getOrDefault() trong java với ví dụ cụ thể

  • HashMap forEach() trong java với ví dụ cụ thể

  • HashMap computeIfPresent() trong java với ví dụ cụ thể

  • HashMap computeIfAbsent() trong java với ví dụ cụ thể

  • HashMap compute() trong java với ví dụ cụ thể

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More

In case of sale of your personal information, you may opt out by using the link Do Not Sell My Personal Information

Accept Decline Cookie Settings I consent to the use of following cookies: Necessary Marketing Analytics Preferences Unclassified Cookie Declaration About Cookies Necessary (0) Marketing (0) Analytics (0) Preferences (0) Unclassified (0) Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies. We do not use cookies of this type. Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers. We do not use cookies of this type. Analytics cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously. We do not use cookies of this type. Preference cookies enable a website to remember information that changes the way the website behaves or looks, like your preferred language or the region that you are in. We do not use cookies of this type. Unclassified cookies are cookies that we are in the process of classifying, together with the providers of individual cookies. We do not use cookies of this type. Cookies are small text files that can be used by websites to make a user's experience more efficient. The law states that we can store cookies on your device if they are strictly necessary for the operation of this site. For all other types of cookies we need your permission. This site uses different types of cookies. Some cookies are placed by third party services that appear on our pages. Cookie Settings

Do you really wish to opt-out? Close

Cancel Confirm wpDiscuz0Would love your thoughts, please comment.x()x| ReplyInsert

Từ khóa » Ví Dụ Về Hashmap Trong Java