2017-02-13 6 views
-1

を使用するのに最適なデータ構造がある私のcsvファイルの数を記録CSVファイルの行を比較します。続き

Server1, Database, Oracle, 5.5 
Server2, Database, Oracle, 6.2 
Server3, OS, Ubuntu, 10.04 
Server1, OS, Ubuntu, 10.04 
Server2, OS, Ubuntu, 12.04 
Server3, Language, Jav, 2.6.3 

は、このファイルにあるServer1には、オラクルのバージョン5.5がインストールされていることを示し、Server2が、バージョン6.2がインストールされている、そしてサーバ3は、バージョンを持っています10.04のUbuntuがインストールされています。

少なくとも2つの異なるサーバーに古いバージョン(つまり、最新バージョンではないバージョン)がインストールされているソフトウェアパッケージ名のリストが必要です。したがって、この場合には、プログラムの出力:

Ubuntu 

私は、CSVファイルのArrayList上記解析を試みたが、問題の更なるロジックを処理することが困難見つけます。

誰かが上記の問題で使用するのに最適なデータ構造を提案できますか?また、上記の問題へのいくつかの指針を提供してください。

+1

おそらく単純なMySQLのテーブル内のデータスティック。その後、データに対して簡単なクエリを簡単に実行できます。あなたが純粋なJavaに固執する必要があるなら、私は4つの列を保持し、各クラスをArrayListまたはHashMapに入れ、コレクション内の要素を反復してマッチを引き出すシンプルなクラスを作成します。 – mba12

答えて

0

は、使用するのに最適なデータ構造であるサンプルコード

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.*; 

public class Test { 
    private static final int MAX_LIMIT = 2; 

    public static void main(String[] args) throws Exception { 
     ArrayList<Package> packages = new ArrayList<>(); 

//change the path name 
     String path = "G:/data.csv"; 

     parseCSVFile(path, packages); 


     updateProductVersion(packages); 

    } 

    private static void updateProductVersion(ArrayList<Package> packages) { 
     HashMap<String, String> latestVersionOfProducts = new HashMap<>(packages.size()); 
     for (Package p : packages) { 
      String currentProduct = p.product; 
      String currentVCode = Package.computeVCode(p.version); 

      if (!latestVersionOfProducts.containsKey(currentProduct)) { 
       latestVersionOfProducts.put(currentProduct, p.version); 
      } else { 
       String setVersion = latestVersionOfProducts.get(currentProduct); 
       if (currentVCode.compareTo(Package.computeVCode(setVersion)) > 0) { 
        latestVersionOfProducts.put(currentProduct, p.version); 
       } 
      } 
     } 
     showLatestVersionsOfProducts(latestVersionOfProducts); 

     detectOutdatedSystems(packages, latestVersionOfProducts); 
    } 

    private static void detectOutdatedSystems(ArrayList<Package> packages, HashMap<String, String> latestVersionOfProducts) { 
     Set<Map.Entry<String, String>> products = latestVersionOfProducts.entrySet(); 
     boolean allNew = true; 
     for (Map.Entry<String, String> product : products) { 
      String productName = product.getKey(); 
      String productVersion = product.getValue(); 

      ArrayList<Package> outdates = new ArrayList<>(); 
      for (Package p : packages) { 
       if (p.product.equalsIgnoreCase(productName) && !p.version.equalsIgnoreCase(productVersion)) { 
        outdates.add(p); 
       } 
      } 
      if (outdates.size() >= MAX_LIMIT) { 
       displayOutdates(outdates, productName); 
       allNew = false; 
      } 
     } 
     if (allNew) { 
      System.out.println("All systems upto date"); 
     } 
    } 

    private static void displayOutdates(ArrayList<Package> outdates, String productName) { 
     System.out.println(outdates.size() + " systems using outdated version of " + productName); 
     for (Package aPackage : outdates) { 
      System.out.println(aPackage); 
     } 
     System.out.println("---------------"); 
    } 

    private static void showLatestVersionsOfProducts(HashMap<String, String> latestVersionOfProducts) { 
     System.out.println("-----------------------------------------"); 
     System.out.println("latest versions detected are"); 
     Set<Map.Entry<String, String>> entries = latestVersionOfProducts.entrySet(); 
     System.out.println("\nVersion\t\tProduct"); 
     for (Map.Entry<String, String> entry : entries) { 
      System.out.format("%-7s\t\t%s\n", entry.getValue(), entry.getKey()); 
     } 
     System.out.println("-----------------------------------------"); 
    } 


    private static void parseCSVFile(String path, ArrayList<Package> packages) throws FileNotFoundException { 
     Scanner scanner = new Scanner(new File(path)); 
     while (scanner.hasNext()) 
      packages.add(new Package(scanner.nextLine())); 
    } 


    static class Package { 
     String machine;//Server 
     String type;//Database or OS or other 
     String product;//Oracle or other 
     String version;//version number 


     public Package(String line) { 
      String[] contents = line.split(","); 
      machine = contents[0].trim(); 
      type = contents[1].trim(); 
      product = contents[2].trim(); 
      version = contents[3].trim(); 
     } 

     public static String computeVCode(String version) { 
      return version.replace(".", "").replaceAll(" ", "").toLowerCase().trim(); 
     } 

     @Override 
     public String toString() { 
      return product + ' ' + type + " version:" + version + " is installed on " + machine; 
     } 
    } 
} 

+0

共有いただきありがとうございます。ユーザーからの入力はできません。あなたが作成したリストから古いバージョン/ Productを探す必要があります。要件が述べたように、それは2つの異なるサーバーにインストールする必要があり、最新のバージョンも別のレコードとして利用できるはずです。したがって、 "Ubuntu"はServer 1とServer 3に10.04を持っています.Ubuntuもサーバ2に12.04として存在します。したがって、10.04は古くなってしまいます。 – Amol

0

からラフなアイデアを取りますか?

答えは主観的である可能性があります。 List<String[]>を使用することをお勧めします。 Listはファイル内の行のリストで、String arrayはカンマで区切られた単語の配列です。

Path filePath = new File("resources/file.csv").toPath(); 
List<String[]> info = new ArrayList<String[]>(); 

try{ 
     Files.lines(filePath).forEach(line -> info.add(line.split(","))); 

     List<String[]> oldSoftware = info.stream().filter(line -> Integer.parseInt(line[3].trim().replaceAll("\\.", "")) < 
         info.stream().filter(line2 -> line2[2].equalsIgnoreCase(line[2])).map(line3 -> Integer.parseInt(line3[3].trim().replaceAll("\\.", ""))).max(Integer::compare).get() 
         ).collect(Collectors.toList()); 
} 
catch (IOException e) { 
     System.out.println("Can't read the file"); 
} 
0

メインメソッドから以下のメソッドを追加しました。これは完全に効率的だとは思わないが、CSVファイルを読み込んで多くのテストケースを渡すことができる。

private void findDuplicates(List<Inventory> inventoryList){ 

     Collections.sort(inventoryList, new SoftwareComparator()); 

     int size = inventoryList.size(); 
     int softwareCount=0; 

     for(int i=0; i <size-1 ; i++){ 
      Inventory inv1 = inventoryList.get(i); 
      Inventory inv2 = inventoryList.get(i+1); 

      if(inv1.getSoftwareName().equals(inv2.getSoftwareName())){ 
       softwareCount++; 
       if(inv1.getVersionNum().equals(inv2.getVersionNum()) || softwareCount==2){ 
        if(!inv1.getServerName().equals(inv2.getServerName()) && softwareCount==2){ 
         System.out.println(inv1.getSoftwareName() +" "+ inv1.getVersionNum()); 
        } 
       } 
      }else{ 
       softwareCount=0; 
      } 
     } 

    } 

class SoftwareComparator implements Comparator<Inventory>{ 

@Override 
public int compare(Inventory obj1, Inventory obj2) { 
    return obj1.getSoftwareName().compareTo(obj2.getSoftwareName()); 
} 

}

関連する問題