我有一个像这样的String数组:
String[] a={"Hi","Hello","Dear","Jungle"};
我有一个像这样的HashMap:
Map<String, String> map=new HashMap<>();
map.put("Hi", "1");
map.put("Hello", "2");
map.put("Dear", "3");
map.put("Jungle", "4");
map.put("Sai", "5");
map.put("Surya", "6");
HashMap是我比较的基础。 如果我们看到,String数组错过了两个字符串 SAI 和 SURYA 。
我正在遍历hashmap并使用String数组检查,如下所示:
int c=0;
for(String key: map.keySet()){
c=0;
for (int i = 0; i < a.length; i++) {
if(key.equals(a[i])){
c++;
System.out.println("App :"+ key+"present in DB , SO it is passed.");
}
}
if(c==0){
System.out.println("App not present in DB, SO it might have failed");
}
}
这是我的完整计划:
import java.util.HashMap;
import java.util.Map;
public class TestingApplication {
public static void main(String[] args) {
String[] a={"Hi","Hello","Dear","Jungle"};
String[] status={"Passed","Passed","Passed","Passed"};
Map<String, String> map=new HashMap<>();
map.put("Hi", "1");
map.put("Hello", "2");
map.put("Dear", "3");
map.put("Jungle", "4");
map.put("Sai", "5");
map.put("Surya", "6");
int c=0;
for(String key: map.keySet()){
c=0;
for (int i = 0; i < a.length; i++) {
if(key.equals(a[i])){
c++;
System.out.println("App:"+ a[i]+" present in DB , SO it is passed.");
}
}
if(c==0){
System.out.println("App not present in DB, SO it might have failed");
}
}
}
}
我的输出是:
App not present in DB, SO it might have failed
App:Hi present in DB , SO it is passed.
App:Dear present in DB , SO it is passed.
App:Hello present in DB , SO it is passed.
App:Jungle present in DB , SO it is passed.
App not present in DB, SO it might have failed
1.。)我们如何知道数组中缺少哪个KEY? 2.)我们如何将缺少的String添加到数组中?
编辑:我的问题不是迭代HashMap。 我的问题是如何找出差异并将它们存储回数据结构中。 如果您愿意,请阅读我的问题,然后将其标记为重复。
答案 0 :(得分:1)
使用Guava库实现这一目标非常简单,请参阅文档以获取更多信息:https://github.com/google/guava/wiki/CollectionUtilitiesExplained
使用Java-8功能,我打印它但你可以对itemsNotFoundInArray进行迭代并添加到缺少项目的数组。
final Set<String> itemsFromArray = Sets.newHashSet(a);
final Set<String> itemsFromMap= map.keySet();
Sets.SetView<String> itemsNotInArray = Sets.difference(itemsFromMap, itemsFromArray);
itemsNotInArray.forEach(System.out::println);
答案 1 :(得分:1)
首先,如果你想在数组中添加一些内容,那么除非你事先创建一个大尺寸的数组,否则你需要动态数组。 ArrayList将是一个很好的候选人。然后,一旦你创建了一个ArrayList,那么实现你的任务将是2或3个班轮代码。
String[] staticArray = new String[] { "Hi","Hello","Dear","Jungle"};
ArrayList myList = Arrays.asList(staticArray);
for(String key: map.keySet()){
if(!myList.contains(key){
myList.add(key);
}
}
答案 2 :(得分:1)
您只需在代码中更改一些内容,请尝试以下代码
public class TestingApplication
{
public static void main(String[] args)
{
String[] a = { "Hi", "Hello", "Dear", "Jungle" };
Map<String, String> map = new HashMap<>();
map.put("Hi", "1");
map.put("Hello", "2");
map.put("Dear", "3");
map.put("Jungle", "4");
map.put("Sai", "5");
map.put("Surya", "6");
List<String> missedKeyList = new ArrayList<>();
for (String key : map.keySet())
{
int c = 0;
for (int i = 0; i < a.length; i++)
{
if (key.equals(a[i]))
{
c++;
System.out.println("App: " + a[i] + " present in DB , So it is passed.");
break;
}
}
if (c == 0)
{
System.out.println("App: " + key + " not present in DB, So it might have failed");
missedKeyList.add(key);
}
}
String[] allKey = Arrays.copyOfRange(a, 0, a.length + missedKeyList.size());
int counter = a.length;
for(String missedKey : missedKeyList)
{
allKey[counter] = missedKey;
counter++;
}
}
}