Example usage for java.util TreeMap keySet

List of usage examples for java.util TreeMap keySet

Introduction

In this page you can find the example usage for java.util TreeMap keySet.

Prototype

public Set<K> keySet() 

Source Link

Document

Returns a Set view of the keys contained in this map.

Usage

From source file:eval.dataset.ParseWikiLog.java

public static void main(String[] ss) throws FileNotFoundException, ParserConfigurationException, IOException {
    FileInputStream fin = new FileInputStream("data/enwiki-20151201-pages-logging.xml.gz");
    GzipCompressorInputStream gzIn = new GzipCompressorInputStream(fin);
    InputStreamReader reader = new InputStreamReader(gzIn);
    BufferedReader br = new BufferedReader(reader);
    PrintWriter pw = new PrintWriter(new FileWriter("data/user_page.txt"));
    pw.println(/*from   w w w  .  j  av  a2s.  com*/
            "#list of user names and pages that they have edited, deleted or created. These info are mined from logitems of enwiki-20150304-pages-logging.xml.gz");
    TreeMap<String, Set<String>> userPageList = new TreeMap();
    TreeSet<String> pageList = new TreeSet();
    int counterEntry = 0;
    String currentUser = null;
    String currentPage = null;
    try {
        for (String line = br.readLine(); line != null; line = br.readLine()) {

            if (line.trim().equals("</logitem>")) {
                counterEntry++;
                if (currentUser != null && currentPage != null) {
                    updateMap(userPageList, currentUser, currentPage);
                    pw.println(currentUser + "\t" + currentPage);
                    pageList.add(currentPage);
                }
                currentUser = null;
                currentPage = null;
            } else if (line.trim().startsWith("<username>")) {
                currentUser = line.trim().split(">")[1].split("<")[0].replace(" ", "_");

            } else if (line.trim().startsWith("<logtitle>")) {
                String content = line.trim().split(">")[1].split("<")[0];
                if (content.split(":").length == 1) {
                    currentPage = content.replace(" ", "_");
                }
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(ParseWikiLog.class.getName()).log(Level.SEVERE, null, ex);
    }
    pw.println("#analysed " + counterEntry + " entries of wikipesia log file");
    pw.println("#gathered a list of unique user of size " + userPageList.size());
    pw.println("#gathered a list of pages of size " + pageList.size());
    pw.close();
    gzIn.close();

    PrintWriter pwUser = new PrintWriter(new FileWriter("data/user_list_page_edited.txt"));
    pwUser.println(
            "#list of unique users and pages that they have edited, extracted from logitems of enwiki-20150304-pages-logging.xml.gz");
    for (String user : userPageList.keySet()) {
        pwUser.print(user);
        Set<String> getList = userPageList.get(user);
        for (String page : getList) {
            pwUser.print("\t" + page);
        }
        pwUser.println();
    }
    pwUser.close();

    PrintWriter pwPage = new PrintWriter(new FileWriter("data/all_pages.txt"));
    pwPage.println("#list of the unique pages that are extracted from enwiki-20150304-pages-logging.xml.gz");
    for (String page : pageList) {
        pwPage.println(page);
    }
    pwPage.close();
    System.out.println("#analysed " + counterEntry + " entries of wikipesia log file");
    System.out.println("#gathered a list of unique user of size " + userPageList.size());
    System.out.println("#gathered a list of pages of size " + pageList.size());
}

From source file:org.openestate.io.openimmo.OpenImmoJavadocBindings.java

public static void main(String[] args) {
    String[] feedbackNames = new String[] { "fehlerliste", "interessent", "objekt", "openimmo_feedback",
            "sender", "status", };

    TreeSet<String> elementNames = new TreeSet<String>();
    TreeMap<String, String> typeNames = new TreeMap<String, String>();
    for (Class clazz : ClassFinder.find(OpenImmoUtils.PACKAGE)) {
        XmlRootElement element = (XmlRootElement) clazz.getAnnotation(XmlRootElement.class);
        if (element != null) {
            elementNames.add(element.name());
            continue;
        }/*from ww w.ja  v  a2 s  .  c  o m*/

        XmlType type = (XmlType) clazz.getAnnotation(XmlType.class);
        if (type != null) {
            typeNames.put(type.name(), clazz.getSimpleName());
            continue;
        }
    }

    System.out.println(StringUtils.repeat("-", 50));
    System.out.println("XML elements in openimmo.xsd");
    System.out.println(StringUtils.repeat("-", 50));
    for (String name : elementNames) {
        if (ArrayUtils.contains(feedbackNames, name))
            continue;
        System.out.println("\n" + "<jaxb:bindings node=\"/xsd:schema/xsd:element[@name='" + name + "']\">\n"
                + "  <jaxb:class>\n" + "    <jaxb:javadoc><![CDATA[Java class for &lt;" + name
                + "&gt; element.]]></jaxb:javadoc>\n" + "  </jaxb:class>\n" + "</jaxb:bindings>");
    }
    System.out.println("");

    System.out.println(StringUtils.repeat("-", 50));
    System.out.println("XML elements in openimmo_feedback.xsd");
    System.out.println(StringUtils.repeat("-", 50));
    for (String name : elementNames) {
        if (!ArrayUtils.contains(feedbackNames, name))
            continue;
        System.out.println("\n" + "<jaxb:bindings node=\"/xsd:schema/xsd:element[@name='" + name + "']\">\n"
                + "  <jaxb:class>\n" + "    <jaxb:javadoc><![CDATA[Java class for &lt;" + name
                + "&gt; element.]]></jaxb:javadoc>\n" + "  </jaxb:class>\n" + "</jaxb:bindings>");
    }
    System.out.println("");

    System.out.println(StringUtils.repeat("-", 50));
    System.out.println("XML types");
    System.out.println(StringUtils.repeat("-", 50));
    for (String name : typeNames.keySet()) {
        System.out.println("\n" + "<jaxb:bindings node=\"/xsd:schema/xsd:complexType[@name='" + name + "']\">\n"
                + "  <jaxb:class name=\"" + typeNames.get(name) + "\">\n"
                + "    <jaxb:javadoc><![CDATA[Java class for \"" + name + "\" complex type.]]></jaxb:javadoc>\n"
                + "  </jaxb:class>\n" + "</jaxb:bindings>");
    }
    System.out.println("");
}

From source file:org.grouplens.lenskit.eval.metrics.predict.LastFmWithTags.java

public static void main(String[] args) {

    //        String trainFile = "/home/user/Desktop/LastFM_exps/reresultset1of5fold/Fold_" + fold + "_train.csv";
    //        String testFile = "/home/user/Desktop/LastFM_exps/reresultset1of5fold/Fold_" + fold + "_test.csv";
    //        String userTagsFile = "/home/user/Desktop/LastFM_exps/reresultset1of5fold/user_taggedartists.csv";
    String trainFile = "/home/user/Desktop/LastFM_exps/reresultset1of5fold/train.csv";
    String testFile = "/home/user/Desktop/LastFM_exps/reresultset1of5fold/Fold_" + fold + "_test.csv";
    String userTagsFile = "/home/user/Desktop/LastFM_exps/reresultset1of5fold/tags.csv";
    BufferedReader brTrain = null;
    BufferedReader brTest = null;
    BufferedReader brTags = null;
    String sCurrentLine = "";
    String splitBy = ",";

    try {//  ww w.j av a 2 s .c om
        PrintWriter writeEnhancedTrain = new PrintWriter(
                "/home/user/Desktop/LastFM_exps/reresultset1of5fold/EnhancedTrain_fold " + fold + ".txt",
                "UTF-8");

        TreeMap<Integer, ArrayList<Integer[]>> UserArtist = new TreeMap<>();
        TreeMap<Integer, ArrayList<Integer[]>> UserTag = new TreeMap<>();
        Set<Integer> ArtistArtist = new HashSet<>();
        Set<Integer> UserUser = new HashSet<>();
        //            TreeMap<Integer, Integer> ArtistArtist = new TreeMap<>();
        //            TreeMap<Integer, ArrayList<Integer[]>> ArtistTag = new TreeMap<>();

        brTrain = new BufferedReader(new FileReader(trainFile));
        brTest = new BufferedReader(new FileReader(testFile));
        brTags = new BufferedReader(new FileReader(userTagsFile));

        while ((sCurrentLine = brTrain.readLine()) != null) {

            String[] cols = sCurrentLine.split(splitBy);

            int userId = Integer.parseInt(cols[0]);
            int artistId = Integer.parseInt(cols[1]);
            double actualValue = Double.parseDouble(cols[5]);

            Integer[] ArtistValue = new Integer[cols.length - 4];

            for (int i = 1; i < cols.length - 3; i++) {
                if (i != 1) {
                    ArtistValue[i - 1] = Integer.parseInt(cols[i + 3]);
                } else {
                    ArtistValue[i - 1] = Integer.parseInt(cols[i]);
                }
            }

            // Create User --> Artist --> Hits TreeMap
            if (!UserArtist.containsKey(userId)) {
                UserArtist.put(userId, new ArrayList<Integer[]>());
                UserArtist.get(userId).add(ArtistValue);
            } else {
                UserArtist.get(userId).add(ArtistValue);
            }

            // Create User --> User
            //                UserUser.add(userId);
            // Create Artist --> Artist
            ArtistArtist.add(artistId);

        }

        // Print User --> Artist
        for (Integer uid : UserArtist.keySet()) {
            for (Integer[] rows : UserArtist.get(uid)) {
                writeEnhancedTrain.print(uid + " ");
                for (Integer col : rows) {
                    writeEnhancedTrain.print(col.toString() + " ");//rows.toString()
                }
                writeEnhancedTrain.println();
            }
        }

        // Print -Artist --> Artist
        for (Integer art : ArtistArtist) {
            writeEnhancedTrain.println("-" + art + " " + art + " 1");
        }

        // Print -User --> User
        //            for (Integer usr : UserUser) {
        //                writeEnhancedTrain.println("-" + usr + " " + usr + " 1");
        //            }
        // Create Artist --> Tag
        String line = "";
        HashMap<Integer, HashMap<Integer, Integer>> atMap = new HashMap();
        HashMap<Integer, Integer> tcMap = null;// = new HashMap<>();
        HashMap<Integer, HashMap<Integer, Integer>> utMap = new HashMap();
        HashMap<Integer, Integer> hsMap = null;
        String headerLine = brTags.readLine();
        while ((line = brTags.readLine()) != null) {
            String[] cols = line.split(splitBy);
            int userId = Integer.parseInt(cols[0]);
            int artistId = Integer.parseInt(cols[1]);
            int tagId = Integer.parseInt(cols[2]);

            tcMap = atMap.get(artistId);
            if (tcMap == null) {
                tcMap = new HashMap<>();
                tcMap.put(tagId, 1);
                atMap.put(artistId, tcMap);
            } else {
                //                    atMap.put(artistId, tcMap);
                //                    Integer count = atMap.get(artistId).get(tagId);
                //                    count++;
                if (atMap.get(artistId).get(tagId) != null) {
                    tcMap.put(tagId, atMap.get(artistId).get(tagId) + 1);
                    atMap.put(artistId, tcMap);
                } else {
                    System.out.println("To tag den exei ksanaemfanistei!!");
                }
            }

            // Create User --> TagHits
            hsMap = utMap.get(userId);
            if (hsMap == null) {
                hsMap = new HashMap<>();
                hsMap.put(tagId, 0);
                utMap.put(userId, hsMap);
            } else {
                for (Integer uid : UserArtist.keySet()) {
                    for (Integer[] rows : UserArtist.get(uid)) {
                        int aid = rows[0];
                        int hits = rows[1];
                        HashMap tags = atMap.get(artistId);
                        Set alltags = tags.keySet();
                        for (Object tid : alltags) {
                            try {
                                Integer a = utMap.get(uid).get(tagId);
                                if (a == null) {
                                    utMap.get(uid).put((Integer) tid, hits);
                                } else {
                                    utMap.get(uid).put((Integer) tid, hits + a);
                                }
                            } catch (java.lang.NullPointerException er) {
                                System.out.println(er);//er.printStackTrace();
                            }
                        }
                    }
                }
            }

        }

        // Print Artist --> Tag
        for (Integer atMapEntry : atMap.keySet()) {
            Integer artid = atMapEntry;
            for (Integer tcMapEntry : tcMap.keySet()) {
                Integer tagid = tcMapEntry;
                Integer tagCounter = tcMap.get(atMapEntry);
                writeEnhancedTrain.println(artid + " " + tagid + " " + tagCounter);
            }
        }

        // Print User --> TagHits
        for (Integer userTagHits : utMap.keySet()) {
            Integer user = userTagHits;
            for (Integer hsCounter : hsMap.keySet()) {
                Integer tag = hsCounter;
                Integer hits = hsMap.get(userTagHits);
                writeEnhancedTrain.println(user + " " + tag + " " + hits);
            }
        }

        //            System.out.println(ArtistArtist.size());
        //            System.out.println(UserUser.size());
        writeEnhancedTrain.flush();
        writeEnhancedTrain.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (brTrain != null) {
            try {
                brTrain.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    System.out.println("Done");
}

From source file:org.grouplens.lenskit.eval.metrics.predict.LastFmEvaluation.java

public static void main(String[] args) {

    String csvFile = "/home/user/Desktop/LastFM_exps/newresultssets/LastFM_1_of_5_20140613-1938.csv";
    //        String csvFile = "/home/user/Desktop/LastFM_exps/reresultset1of5fold/LastFM_1_of_5_20140606-0338.csv";
    BufferedReader br = null;// w w  w.j av a 2 s . c  o m
    String sCurrentLine = "";
    String cvsSplitBy = ",";

    try {

        TreeMap<Integer, ArrayList<Double>> golden = new TreeMap<Integer, ArrayList<Double>>();
        TreeMap<Integer, ArrayList<Double>> methodA = new TreeMap<Integer, ArrayList<Double>>();
        TreeMap<Integer, ArrayList<Double>> methodB = new TreeMap<Integer, ArrayList<Double>>();
        TreeMap<Integer, ArrayList<Double>> methodC = new TreeMap<Integer, ArrayList<Double>>();

        br = new BufferedReader(new FileReader(csvFile));
        //            String header = br.readLine();
        //            System.out.println(header);

        while ((sCurrentLine = br.readLine()) != null) {

            String[] cols = sCurrentLine.split(cvsSplitBy);
            int userId = Integer.parseInt(cols[0]);
            double goldenValue = Double.parseDouble(cols[5]);
            double methodAValue = Double.parseDouble(cols[2]);
            double methodBValue = Double.parseDouble(cols[3]);
            double methodCValue = Double.parseDouble(cols[4]);

            //Actual Values TreeMap creation
            if (!golden.containsKey(userId)) {
                golden.put(userId, new ArrayList<Double>());
                golden.get(userId).add(goldenValue);
            } else {
                golden.get(userId).add(goldenValue);
            }
            //Method A TreeMap creation
            if (!methodA.containsKey(userId)) {
                methodA.put(userId, new ArrayList<Double>());
                methodA.get(userId).add(methodAValue);
            } else {
                methodA.get(userId).add(methodAValue);
            }
            //Method B TreeMap creation
            if (!methodB.containsKey(userId)) {
                methodB.put(userId, new ArrayList<Double>());
                methodB.get(userId).add(methodBValue);
            } else {
                methodB.get(userId).add(methodBValue);
            }
            //Method C TreeMap creation
            if (!methodC.containsKey(userId)) {
                methodC.put(userId, new ArrayList<Double>());
                methodC.get(userId).add(methodCValue);
            } else {
                methodC.get(userId).add(methodCValue);
            }

        }

        uniqueUsers = golden.size();
        System.out.println("To synolo twn monadikwn xristwn sto dataset einai: " + uniqueUsers);

        nusers = 0;
        sumPearsonA = 0;
        sumPearsonB = 0;
        sumPearsonC = 0;
        for (Integer i : golden.keySet()) {

            Double[] ADoubles = new Double[methodA.get(i).size()];
            methodA.get(i).toArray(ADoubles);
            Double[] BDoubles = new Double[methodB.get(i).size()];
            methodB.get(i).toArray(BDoubles);
            Double[] CDoubles = new Double[methodC.get(i).size()];
            methodC.get(i).toArray(CDoubles);
            Double[] goldenDoubles = new Double[golden.get(i).size()];
            golden.get(i).toArray(goldenDoubles);

            if (goldenDoubles.length > 1) {

                corA = p.correlation(ArrayUtils.toPrimitive(ADoubles), ArrayUtils.toPrimitive(goldenDoubles));
                corB = p.correlation(ArrayUtils.toPrimitive(BDoubles), ArrayUtils.toPrimitive(goldenDoubles));
                corC = p.correlation(ArrayUtils.toPrimitive(CDoubles), ArrayUtils.toPrimitive(goldenDoubles));

                if (Double.isNaN(corA)) {

                    corA = 1;
                }
                if (Double.isNaN(corB)) {

                    corB = 1;
                }
                if (Double.isNaN(corC)) {

                    corC = 1;
                }

                sumPearsonA += corA;
                sumPearsonB += corB;
                sumPearsonC += corC;
                nusers++;
                //                for (int arrayCounter = 0; arrayCounter < goldenDoubles.length; arrayCounter++) {
                //
                //                    sumA += ADoubles[arrayCounter];
                //                    sumB += BDoubles[arrayCounter];
                //                    sumC += CDoubles[arrayCounter];
                //                    sumGolden += goldenDoubles[arrayCounter];
                //                    
                //                }
                //
                //                avgA = sumA / ADoubles.length;
                //                avgB = sumB / BDoubles.length;
                //                avgC = sumC / CDoubles.length;
                //                avgGolden = sumGolden / goldenDoubles.length;

                //                    System.out.println(sumPearsonA);
                //                    System.out.println(sumPearsonB);
                //                    System.out.println(sumPearsonC);
                //                    System.out.println(nusers);
                //                    System.out.println("----------------------------------");
                //                sumGolden = 0; sumA = 0; sumB = 0; sumC = 0;
            } else {
                System.out.println("User has only ONE rating!!!!! ");
                System.out.println("----------------------------------");
            }
        }

        avgA = sumPearsonA / nusers;
        avgB = sumPearsonB / nusers;
        avgC = sumPearsonC / nusers;

        System.out.println(avgA);
        System.out.println(avgB);
        System.out.println(avgC);
        System.out.println("----------------------------------");

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    System.out.println("Done");
}

From source file:com.garyclayburg.BootVaadin.java

public static void dumpSystemProperties() {
    log.info("system properties dump");
    Properties systemProperties = System.getProperties();
    TreeMap tm = new TreeMap(systemProperties);
    for (Object o : tm.keySet()) {
        String key = (String) o;
        log.info(key + ": " + tm.get(o));
    }/* w ww . j ava  2s  .  c o  m*/
    Map<String, String> getenv = new TreeMap<>(System.getenv());
    log.info("system environment dump");
    for (String key : getenv.keySet()) {
        log.info("env " + key + ": " + getenv.get(key));
    }
}

From source file:com.bluexml.tools.miscellaneous.Translate.java

public static void writeBackValues(File values, File propertiesFile) throws IOException {
    List<String> readLines = FileUtils.readLines(values, "UTF-8");
    Properties props = new Properties();
    TreeMap<String, String> propsMap = loadProperties(propertiesFile);
    Set<String> keySet = propsMap.keySet();
    int index = 0;
    for (String key : keySet) {
        String value = readLines.get(index);
        System.out.println("before trans :" + value);

        System.out.println("after trans :" + value);
        System.out.println();// w  ww .  j  av  a2  s. c o  m
        props.setProperty(key, value);
        index++;
    }

    FileOutputStream out = new FileOutputStream(propertiesFile);
    props.store(out, null);
    out.close();

}

From source file:com.magnet.mmx.util.DatabaseExport.java

public static void doWork(Connection conn, TreeMap<String, String> tableQueryMap) throws Exception {
    IDatabaseConnection connection = new DatabaseConnection(conn);

    // partial database export
    QueryDataSet partialDataSet = new QueryDataSet(connection);
    for (String table : tableQueryMap.keySet()) {
        partialDataSet.addTable(table, tableQueryMap.get(table));
    }//w ww . jav a2 s.c  o  m

    FlatXmlDataSet.write(partialDataSet, System.out);
}

From source file:uk.ac.bbsrc.tgac.miso.integration.util.SignatureHelper.java

private static String createSortedUrl(String url, TreeMap<String, String> headersAndParams) {
    // build the url with headers and params sorted
    String params = "";
    for (String key : headersAndParams.keySet()) {
        if (params.length() > 0) {
            params += "@";
        }//from   ww  w .  j  a v a 2  s . c o m
        params += key + "=" + headersAndParams.get(key).toString();
    }
    if (!url.endsWith("?") && !"".equals(params))
        url += "?";

    log.debug("COMPLETE URL: " + url + params);

    return url + params;
}

From source file:tor.HiddenService.java

public static OnionRouter[] findResposibleDirectories(String onionb32) {
    Consensus con = Consensus.getConsensus();

    // get list of nodes with HS dir flag
    TreeMap<String, OnionRouter> routers = con.getORsWithFlag("HSDir");
    Object keys[] = routers.keySet().toArray();
    Object vals[] = routers.values().toArray();

    ArrayList<OnionRouter> rts = new ArrayList<>();

    for (int replica = 0; replica < 2; replica++) {
        // Get nodes just to right of HS's descID in the DHT
        int idx = -Arrays.binarySearch(keys, Hex.encodeHexString(getDescId(onionb32, (byte) replica)));

        for (int i = 0; i < 3; i++) {
            rts.add((OnionRouter) vals[(idx + i) % vals.length]);
        }//from w ww.  j a  va 2s  .c  om
    }

    // return list containing hopefully six ORs.
    return rts.toArray(new OnionRouter[0]);
}

From source file:com.nubits.nubot.trading.TradeUtils.java

public static String buildQueryString(TreeMap<String, String> args, String encoding) {
    String result = new String();
    for (String hashkey : args.keySet()) {
        if (result.length() > 0) {
            result += '&';
        }//from www .j  a v  a2s .  co m
        try {
            result += URLEncoder.encode(hashkey, encoding) + "="
                    + URLEncoder.encode(args.get(hashkey), encoding);
        } catch (Exception ex) {
            LOG.severe(ex.toString());
        }
    }
    return result;
}