Pergunta

I have a ArrayList>> with certain entries. Each entry has a "ID" which can be similar to other entries.

I want to check if two(or more) entries have the same Id, then assign a color to them. If not then assign the next color and add the assigned color value the map.

Here is my code:-

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

how can I achieve this?

Foi útil?

Solução 2

Maybe this test-code will be useful:

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

Prints:

  • News id=1, title=title1, description=description1, color=#1F1A17
  • News id=2, title=title2, description=description2, color=#62934D
  • News id=1, title=title3, description=description3, color=#1F1A17
  • News id=5, title=title4, description=description4, color=#F9B03F
  • News id=2, title=title5, description=description5, color=#62934D
  • News id=1, title=title6, description=description6, color=#1F1A17
  • News id=1, title=title7, description=description7, color=#1F1A17
  • News id=3, title=title8, description=description8, color=#7959BC
  • News id=4, title=title9, description=description9, color=#74B8DE
  • News id=5, title=title10, description=description10, color=#F9B03F
  • News id=1, title=title11, description=description11, color=#1F1A17
  • News id=6, title=title12, description=description12, color=#E65641
  • Id to color: id=3, color=#7959BC
  • Id to color: id=2, color=#62934D
  • Id to color: id=1, color=#1F1A17
  • Id to color: id=6, color=#E65641
  • Id to color: id=5, color=#F9B03F
  • Id to color: id=4, color=#74B8DE

Outras dicas

Use map, which maps ID to color. Then iterate over your ArrayList and do the following:

  • if a map already contains an ID, then get color from it and assign to your entry.
  • if map doesn't contain and ID, get new color, assign it to your entry and put ID-color entry to the map.

Pseudocode to do that:

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

First of all create an object class to store news data. i.e.

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
  }

For your requirement please use below code snippet:

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

Output of above code is:

News title/id: title1/xxxx Color: #1F1A17

News title/id: title2/xxxx Color: #1F1A17

News title/id: title3/xxxx2 Color: #62934D

News title/id: title4/xxxx3 Color: #F9B03F

News title/id: title5/xxxx2 Color: #62934D

To know if an ArrayList contains an element use contains.

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

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

To know if a Map conatains an element use containsKey or containsValue.

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

if (map.containsKey("a key")) {
   //do something
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top