繁体   English   中英

如果键已经存在于HashMap中,则添加新值

[英]Add new value if key already exists in HashMap

我有一个带有某些条目的ArrayList >>。 每个条目都有一个“ ID”,它可以类似于其他条目。

我想检查两个(或多个)条目是否具有相同的ID,然后为它们分配颜色。 如果不是,则分配下一个颜色并在地图上添加分配的颜色值。

这是我的代码:

String[] colorPallete =new String[] {"#1F1A17", "#62934D", "#F9B03F", "#7959BC", "#74B8DE", "#E65641", "#7CC8BB", "#D7CE5D", "#D6BE95", "#B694D1"};



 map = new HashMap<String,String>();
 map.put(NEWSSOURCETITLE, title);
 map.put(DESCRIPTION, description);
 map.put(ID, newsId);
 myNewsList.add(map);

我该如何实现?

使用map ,它将ID映射到颜色。 然后遍历您的ArrayList并执行以下操作:

  • 如果地图已经包含ID,则从中获取颜色并分配给您的条目。
  • 如果地图不包含ID,则获取新颜色,将其分配给您的条目,然后将ID-color条目放入地图中。

伪代码可以做到这一点:

Map<String, String> idToColor = new HashMap<>();

for (...) {
  if (idToColor.contains(entry.id)) {
    entry.color = idToColor.get(id);
  } else {
    entry.color = generateNewColor();
    idToColor.put(entry.id, entry.color);
  }
}

也许这个测试代码会有用:

public class Test11 {
    static String[] colorPallete = new String[] {"#1F1A17", "#62934D", "#F9B03F", "#7959BC", "#74B8DE", "#E65641", "#7CC8BB", "#D7CE5D", "#D6BE95", "#B694D1"};

    public static void main(String[] args) {
        Map<String, String> idToColorMap = new HashMap<String, String>();

        List<News> newsList = new ArrayList<News>();
        newsList.add(new News("1", "title1", "description1"));
        newsList.add(new News("2", "title2", "description2"));
        newsList.add(new News("1", "title3", "description3"));
        newsList.add(new News("5", "title4", "description4"));
        newsList.add(new News("2", "title5", "description5"));
        newsList.add(new News("1", "title6", "description6"));
        newsList.add(new News("1", "title7", "description7"));
        newsList.add(new News("3", "title8", "description8"));
        newsList.add(new News("4", "title9", "description9"));
        newsList.add(new News("5", "title10", "description10"));
        newsList.add(new News("1", "title11", "description11"));
        newsList.add(new News("6", "title12", "description12"));

        int colorIndex = 0;
        for (int i = 0; i < newsList.size(); i++) {
            if (newsList.size() > 1 && !idToColorMap.containsKey(newsList.get(i).getId())) {
                News currentNews = newsList.get(i);
                currentNews.setColor(colorPallete[colorIndex]);
                idToColorMap.put(currentNews.getId(), colorPallete[colorIndex]);

                for (int j = i + 1; j < newsList.size(); j++) {
                    if (newsList.get(j).getId().equals(currentNews.getId())) {
                        newsList.get(j).setColor(colorPallete[colorIndex]);
                    }
                }

                if (++colorIndex == colorPallete.length) {
                    colorIndex = 0;
                }
            } else {
                newsList.get(0).setColor(colorPallete[0]);
                idToColorMap.put(newsList.get(0).getId(), colorPallete[0]);
            }
        }

        for (News news: newsList) {
            System.out.println("News id=" + news.getId() + ", title=" + news.getTitle() + ", description=" + news.getDescription() + ", color=" + news.getColor());
        }

        for (Map.Entry<String, String> entry: idToColorMap.entrySet()) {
            System.out.println("Id to color: id=" + entry.getKey() + ", color=" + entry.getValue());
        }
    }
}

class News {
    String id;
    String title;
    String description;
    String color;

    News(String id, String title, String description) {
        this.id = id;
        this.title = title;
        this.description = description;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

打印:

  • 新闻ID = 1,标题=标题1,描述=描述1,颜色=#1F1A17
  • 新闻ID = 2,标题= title2,描述= description2,颜色=#62934D
  • 新闻ID = 1,标题= title3,描述= description3,颜色=#1F1A17
  • 新闻ID = 5,标题= title4,描述= description4,颜色=#F9B03F
  • 新闻ID = 2,标题= title5,描述= description5,颜色=#62934D
  • 新闻ID = 1,标题= title6,描述= description6,颜色=#1F1A17
  • 新闻ID = 1,标题= title7,描述= description7,颜色=#1F1A17
  • 新闻ID = 3,标题= title8,描述= description8,颜色=#7959BC
  • 新闻ID = 4,标题= title9,描述= description9,颜色=#74B8DE
  • 新闻ID = 5,标题= title10,描述= description10,颜色=#F9B03F
  • 新闻ID = 1,标题= title11,描述= description11,颜色=#1F1A17
  • 新闻ID = 6,标题= title12,描述= description12,颜色=#E65641
  • 标识颜色:id = 3,颜色=#7959BC
  • 颜色ID:ID = 2,颜色=#62934D
  • 标识颜色:id = 1,颜色=#1F1A17
  • 颜色ID:ID = 6,颜色=#E65641
  • 颜色ID:ID = 5,颜色=#F9B03F
  • 颜色ID:ID = 4,颜色=#74B8DE

首先创建一个对象类来存储新闻数据。

public class NewsData {
      String newsSourceTitle;
      String description;
      String id;   
      public NewsData(String title, String desc, String id) {
          this.newsSourceTitle = title;
          this.description = desc;
          this.id = id;
      }      
   // write getter for fields
  }

根据您的要求,请使用以下代码段:

class NewsColour {
        String[] colorPallete = new String[] {"#1F1A17", "#62934D", "#F9B03F", "#7959BC", "#74B8DE", "#E65641", "#7CC8BB", "#D7CE5D", "#D6BE95", "#B694D1"};

        int colorIndex = 0;

        private String getNextColor() {
         return (colorIndex < colorPallete.length) ? colorPallete[colorIndex++] :      colorPallete[colorIndex++%colorPallete.length];
        }

     // Change with your method
     public void setNewsColor() {
        NewsData newsData1 = new NewsData("title1", "desc1", "xxxx");
        NewsData newsData2 = new NewsData("title2", "desc2", "xxxx");
        NewsData newsData3 = new NewsData("title3", "desc3", "xxxx2");
        NewsData newsData4 = new NewsData("title4", "desc4", "xxxx3");
        NewsData newsData5 = new NewsData("title5", "desc5", "xxxx2");

        List<NewsData> myNewsList = new ArrayList<NewsData>();

        myNewsList.add(newsData1);
        myNewsList.add(newsData2);
        myNewsList.add(newsData3);
        myNewsList.add(newsData4);
        myNewsList.add(newsData5);

        Map<String, String> newsNColorMap = new HashMap<String, String>();

        for (NewsData news : myNewsList) {
            String newsColor = newsNColorMap.get(news.getId());
            if (null == newsColor) {
                newsColor = getNextColor();
                newsNColorMap.put(news.getId(), newsColor);            
            }
            // Putting Syso just for printing color. Change this as per your requirement.
            System.out.println("News title/id: " + news.getTitle() + "/" + news.getId() + " Color: " + newsColor); 
    }
     }

上面代码的输出是

新闻标题/编号:title1 / xxxx颜色:#1F1A17

新闻标题/编号:title2 / xxxx颜色:#1F1A17

新闻标题/ id:title3 / xxxx2颜色:#62934D

新闻标题/ id:title4 / xxxx3颜色:#F9B03F

新闻标题/ id:title5 / xxxx2颜色:#62934D

要知道ArrayList是否包含元素,请使用contains

List<String> list = new ArrayList<String>();
list.add(...);

if (list.contains("a value")) {
   //do something
}

要知道Map是否containsKey元素,请使用containsKeycontainsValue

Map<String, String> map = new HashMap<String, String>();
map.put(...);

if (map.containsKey("a key")) {
   //do something
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM