Here you can find the source of computeAverageGeneIdsPerName()
public static void computeAverageGeneIdsPerName() throws SQLException
//package com.java2s; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; public class Main { public static void computeAverageGeneIdsPerName() throws SQLException { Connection connection = DriverManager.getConnection("jdbc:mysql://wanaheim:3306/user_cplake", "cplake", "cplake"); Map<String, Set<Integer>> name2geneIdCount = new HashMap<String, Set<Integer>>(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery( "SELECT gene.ID, gene.name, protein.name FROM GR_Names gene, GR_ProteinNames protein"); //ResultSet resultSet = statement.executeQuery("SELECT gene.ID, gene.name FROM GR_Names gene"); while (resultSet.next()) { Integer geneId = resultSet.getInt(1); String geneName = resultSet.getString(2); String proteinName = resultSet.getString(3); Set<Integer> geneIds = name2geneIdCount.get(geneName); if (geneIds == null) { geneIds = new HashSet<Integer>(); name2geneIdCount.put(geneName, geneIds); }//from w w w . ja v a 2 s . c om geneIds.add(geneId); geneIds = name2geneIdCount.get(proteinName); if (geneIds == null) { geneIds = new HashSet<Integer>(); name2geneIdCount.put(proteinName, geneIds); } geneIds.add(geneId); } resultSet.close(); statement.close(); connection.close(); int averageIdsPerName = 0; for (String name : name2geneIdCount.keySet()) { Integer idCount = name2geneIdCount.get(name).size(); averageIdsPerName += idCount; } System.out.println( "Average Gene IDs per Name: " + ((double) averageIdsPerName / (double) name2geneIdCount.size())); System.out.println("finished"); } }