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

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

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

問題描述

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

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.

我已經(jīng)玩了一段時間了,似乎無法跳過重復(fù)項.

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));            
        }
    }
}

推薦答案

要刪除重復(fù)項,請使用 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);
}

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

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

相關(guān)文檔推薦

Convert List of Strings into Map using Java-8 Streams API(使用 Java-8 Streams API 將字符串列表轉(zhuǎn)換為 Map)
Getting data from JSON(從 JSON 獲取數(shù)據(jù))
java linkedhashmap iteration(javalinkedhashmap迭代)
Converting a list of objects to Map(將對象列表轉(zhuǎn)換為 Map)
Create a HashMap with a fixed Key corresponding to a HashSet. point of departure(用一個固定的Key對應(yīng)一個HashSet創(chuàng)建一個HashMap.出發(fā)點)
HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found(HttpMessageConverter 異常:RestClientException:無法寫入請求:找不到合適的 HttpMessageConverter) - IT屋-程序員
主站蜘蛛池模板: 久久精品com| 欧美成人精品一区二区男人看 | 成人午夜影院 | 午夜婷婷激情 | 欧美日韩一区二区电影 | 在线免费国产 | 18gay男同69亚洲网站 | 国产精品www | 日韩午夜在线观看 | 欧美激情在线观看一区二区三区 | 国产999精品久久久 精品三级在线观看 | 99精品国产一区二区三区 | 日韩精品免费 | 性做久久久久久免费观看欧美 | 欧美日韩激情 | 免费一级网站 | 日韩免费看片 | 成人av一区二区亚洲精 | 毛片免费看的 | 九九99九九精彩46 | 欧美xxxx在线 | 国产激情在线播放 | 精品欧美一区二区三区久久久小说 | 香蕉久久久 | 天天综合网91 | 亚洲狠狠爱 | 成年人国产在线观看 | 日韩欧美理论片 | 日本精品一区二区三区在线观看视频 | 国产精品99 | 国产成人叼嘿视频在线观看 | 精品一二三区 | 国内精品一区二区三区 | 日韩在线免费视频 | 538在线精品| 日韩综合在线 | 国产情侣在线看 | 岛国av免费在线观看 | 精久久久久 | 亚洲欧美在线观看 | 午夜寂寞网站 |