如果密钥已存在于HashMap中,则添加新值

时间:2014-04-25 06:57:49

标签: java arraylist 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);

我怎样才能做到这一点?

4 个答案:

答案 0 :(得分:2)

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

  • 如果地图已包含ID,则从中获取颜色并分配给您的条目。
  • 如果地图不包含和ID,请获取新颜色,将其指定给您的条目并将ID颜色条目放入地图。

伪代码:

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

答案 1 :(得分:0)

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

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,title = title1,description = description1,color =#1F1A17
  • 新闻ID = 2,title = title2,description = description2,color =#62934D
  • 新闻ID = 1,title = title3,description = description3,color =#1F1A17
  • 新闻ID = 5,title = title4,description = description4,color =#F9B03F
  • 新闻ID = 2,title = title5,description = description5,color =#62934D
  • 新闻ID = 1,title = title6,description = description6,color =#1F1A17
  • 新闻ID = 1,title = title7,description = description7,color =#1F1A17
  • 新闻ID = 3,title = title8,description = description8,color =#7959BC
  • 新闻ID = 4,title = title9,description = description9,color =#74B8DE
  • 新闻ID = 5,title = title10,description = description10,color =#F9B03F
  • 新闻ID = 1,title = title11,description = description11,color =#1F1A17
  • 新闻ID = 6,title = title12,description = description12,color =#E65641
  • ID为颜色:id = 3,color =#7959BC
  • ID为颜色:id = 2,颜色=#62934D
  • ID为颜色:id = 1,color =#1F1A17
  • ID为颜色:id = 6,color =#E65641
  • ID为颜色:id = 5,color =#F9B03F
  • ID为颜色:id = 4,color =#74B8DE

答案 2 :(得分:0)

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

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

以上代码的输出

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

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

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

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

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

答案 3 :(得分:-1)

要知道ArrayList是否包含使用contains的元素。

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

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

要知道地图是否包含使用containsKeycontainsValue的元素。

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

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