久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在不打印重復項的同時從 HashMap 打印值?

How would I print values from a HashMap while not printing duplicates?(如何在不打印重復項的同時從 HashMap 打印值?)
本文介紹了如何在不打印重復項的同時從 HashMap 打印值?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試修復這段代碼,我正在從具有車牌號和所有者列表(該格式)的哈希圖中打印.我試圖通過 printOwners(); 打印出所有者.但我不能讓它不打印重復.

I'm trying to fix this piece of code where I'm printing from a hashmap having a list of plate numbers and owners (that format). I'm trying to print out just the owners via printOwners(); but I can't get it to not print duplicates.

我已經玩了一段時間了,似乎無法跳過重復項.

I've played around with it for a while, just can't seem to skip over duplicates.

這是我的代碼:

import java.util.ArrayList;
import java.util.HashMap;

public class VehicleRegister {

    private HashMap<RegistrationPlate, String> owners;

    public VehicleRegister() {
        owners = new HashMap<RegistrationPlate, String>();
    }

    public boolean add(RegistrationPlate plate, String owner) {
        //search for existing plate
        if (!(owners.containsKey(plate))) { // add if no plate
            owners.put(plate, owner);
            return true;
        }

        //if plate is found, check for owner
        else if (owners.keySet().equals(owner)) {
           return false;
        }

        return false;
    }

    public String get(RegistrationPlate plate) {
        return owners.get(plate);
    }

    public boolean delete(RegistrationPlate plate) {
        if (owners.containsKey(plate)) {
            owners.remove(plate);
            return true;
        }

        return false; 
    }

    public void printRegistrationPlates() {
        for (RegistrationPlate item : owners.keySet()) {
            System.out.println(item);
        }
    }

    public void printOwners() {

        for (RegistrationPlate item : owners.keySet()) {
            System.out.println(owners.get(item));            
        }
    }
}

推薦答案

要刪除重復項,請使用 HashSet:

To remove the duplicates, use a HashSet<String>:

public void printOwners() {
    for (String s : new HashSet<>(owners.values())) {
        System.out.println(s);            
    }
}

或者使用 Java 8 Streamdistinct() 方法:

Alternatively with Java 8 Stream and the distinct() method:

public void printOwners() {
    owners.values().stream().distinct().forEach(System.out::println);
}

這篇關于如何在不打印重復項的同時從 HashMap 打印值?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Convert List of Strings into Map using Java-8 Streams API(使用 Java-8 Streams API 將字符串列表轉換為 Map)
Getting data from JSON(從 JSON 獲取數據)
java linkedhashmap iteration(javalinkedhashmap迭代)
Converting a list of objects to Map(將對象列表轉換為 Map)
Create a HashMap with a fixed Key corresponding to a HashSet. point of departure(用一個固定的Key對應一個HashSet創建一個HashMap.出發點)
HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found(HttpMessageConverter 異常:RestClientException:無法寫入請求:找不到合適的 HttpMessageConverter) - IT屋-程序員
主站蜘蛛池模板: av网站免费看| 成人免费区一区二区三区 | www.欧美 | 九热在线| 国精产品一品二品国精在线观看 | 国产成人啪免费观看软件 | 999免费网站 | 操久久久| 国产精品视频一区二区三区四蜜臂 | 欧美国产精品一区二区三区 | 97伦理| 欧美成人精品一区二区三区 | 国产精品久久久久久久久久久久久久 | 国产精品视频一区二区三区, | 美女久久久久 | 日韩一区二区在线视频 | 日本久久一区二区三区 | 夜夜操天天干 | 色婷婷影院| 在线看片国产 | 成人久久网 | 亚洲欧美日韩网站 | 亚洲精品亚洲人成人网 | 亚洲欧美国产毛片在线 | 男女视频免费 | 久久51 | 国产一区二区三区久久久久久久久 | 亚洲性网| 婷婷综合色 | 国产精品不卡一区 | 成人在线不卡 | 一区二区三区影院 | www.99re5.com| 亚洲一区二区在线免费观看 | 免费的av| 亚洲视频一区在线 | 免费在线播放黄色 | 一级片在线免费播放 | 欧美黑人体内she精在线观看 | 欧美一页| 国产精品国产精品国产专区不片 |