我有一个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);
我怎样才能做到这一点?
答案 0 :(得分:2)
使用 map ,将ID映射到颜色。然后遍历您的ArrayList并执行以下操作:
伪代码:
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;
}
}
打印:
答案 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
}
要知道地图是否包含使用containsKey
或containsValue
的元素。
Map<String, String> map = new HashMap<String, String>();
map.put(...);
if (map.containsKey("a key")) {
//do something
}