Java数据库国家

时间:2016-01-22 03:30:26

标签: java arrays for-loop duplicates nested-loops

我需要创建一个java程序,它接受一个国家/地区的字符串数组并打印出国家/地区的名称以及它们在数组中的次数。

示例:

static void Main(string[] args)
{
    Stopwatch sw = new Stopwatch();
    sw.Start();
    bool output1 = false;
    bool output2 = false;
    bool output3 = false;

    //9 seconds
    //output1 = FetchData1();
    //output2 = FetchData2();
    //output3 = FetchData3();

    Parallel.Invoke(() => output1 = FetchData1(), () => output2 = FetchData2(), () => output3 = FetchData3());

    //Process(output1, output2, output3);
    Console.WriteLine(sw.ElapsedMilliseconds);
    sw.Stop();
    Console.ReadLine();
}

static bool FetchData1()
{
    //db call 
    System.Threading.Thread.Sleep(5000);
    Console.WriteLine("FetchData1");
    return true;
}

static bool FetchData2()
{
    //db call 
    System.Threading.Thread.Sleep(1000);
    Console.WriteLine("FetchData2");
    return true;
}

static bool FetchData3()
{
    //db call 
    System.Threading.Thread.Sleep(3000);
    Console.WriteLine("FetchData3");
    return true;
}

我需要输出:

 String[] countries = {"Mexico","Russia","USA","Russia","Mexico",
                       "USA","Germany", "Mexico", "Canada" };

这是我到目前为止所拥有的:

Mexico 3
Russia 2
USA 2
Germany 1
Canada 1

我得到的输出是:

public class TestingTwo {

 public static void main (String args[]) {

    String[] countries = {"Mexico","Russia","USA", "Russia","Mexico", "USA","Germany", "Mexico", "Canada" };
    int i;
    int j;


    for(i=0;i<countries.length-1;i++) {
        int count=0;

        for(j=i+1;j<countries.length;j++) {
            if ((countries[i]==countries[j]) && (i != j)) {
                count++;
                System.out.println(countries[j]+ " " + count);
            }
        }
    }
  }//end of main method
}//end of class

问题面临:我无法找到一种方法只打印一次国家/地区名称。我能够将它打印到打印具有重复项的国家/地区,但它会打印几次。我也遇到了int计数问题,以及如何正确校准重复数量。

3 个答案:

答案 0 :(得分:3)

试试这个

    String[] countries = {"Mexico","Russia","USA","Russia","Mexico",
            "USA","Germany", "Mexico", "Canada" };
    Map<String, Integer> map = new LinkedHashMap<>();
    for (String country : countries) {
        map.compute(country, (k, v) -> v == null ? 1 : v + 1);
    }
    for (Map.Entry<String, Integer> e : map.entrySet()) {
        System.out.println(e.getKey() + " " + e.getValue());
    }

答案 1 :(得分:0)

如果你有可能使用Java 8,那么我认为接受这个问题的答案会对你有所帮助:

Group by counting in Java8 stream API

将它应用于您的问题我们可以像这样解决:

import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.Arrays;

public class TestingTwo{
    public static void main (String[] args){
        String[] countries = {"Mexico","Russia","USA","Russia","Mexico",
                       "USA","Germany", "Mexico", "Canada" };

        Map<String, Long> counted = Arrays.stream(countries)
            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

        for(String country: counted.keySet()){
            System.out.println(country+" "+counted.get(country));
        }
    }
}

答案 2 :(得分:0)

如果您不使用Java 8,则可以尝试以下代码:

Set<String> uniqueCountries = new HashSet<String>(Arrays.asList(countries));

Iterator iterator = newset.iterator();
while (iterator.hasNext()){
  int count = 0;
  String c = iterator.next(); 
  for(int i=0;i<countries.length;i++) {
    if(c.equals(countries[i])) {
      count++;
    }
  }
  System.out.println(countries[i]+ " " + count);
}

此代码仍未优化,因为对于每个唯一的国家/地区,我们都在遍历输入数组。