473,382 Members | 1,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Need help with creating a hashset and selecting distinct sorted data

2
I read in a text file to created a hashmap based on the users id which is unique. This works fine, however, I also need to extract the users name, but I only need the name once and sorted. I am trying to do this with a hashset with some success. I can get the data but there are duplicates and it's not sorting. I thought the hashset would do this automatically.

Is it possible to extract this data into a hashset from the hashmap as describe above? I've put a sample of hashmap.

Expand|Select|Wrap|Line Numbers
  1. HashMap value = new HashMap();
  2.     /** Creates new form TestHashMap */
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   public void fileRead() throws Exception
  9.   {
  10.  
  11.                 FileReader fr = new FileReader("C://Documents and Settings//epeak//My Documents//test.txt");
  12.         BufferedReader br = new BufferedReader(fr);
  13.         String s;
  14.         while((s = br.readLine()) != null) {
  15.         Pattern p = Pattern.compile("[\\s]+");
  16.         String[] str = p.split(s);
  17.  
  18. //        System.out.println(str[0] + " "+str[1] +"   "+str[2]+"   "+str[3]);
  19.                  Propertyunbuf prop = new Propertyunbuf();
  20.          prop.setPropertyId(str[0]);
  21.          prop.setPropertyName(str[1]);
  22.                  prop.setPropertyValue( new Double(str[2]));
  23. //         prop.setPropertyValue(new Double(str[2]).doubleValue());
  24.          prop.setAgendId(str[3].trim());
  25. //                 System.out.println(str[1]);
  26.  
  27.  
  28.  
  29.  HashSet ref = new HashSet(); // create a HashSet
  30.          Iterator i = ref.iterator(); // get iterator
  31.  
  32.          System.out.println( "\nNonduplicates are: " );
  33.          while ( i.hasNext() )
  34.              System.out.print( i.next() + " " );
  35.  
  36.          System.out.println();
Sep 1 '10 #1
1 1813
Oralloy
987 Expert 512MB
@elpeak,

First off, please use code tags. There's a FAQ page that might help you here.

Second, not to nit pick, but you really should qualify your generic containers - it really helps keep things straight when projects grow. There is little worse than trying to figure out what exactly is in that map you just got from a method with no other significant documentation.

Here is a method that I wrote to give a sorted list of the keys from a HashMap, which compiles...
Expand|Select|Wrap|Line Numbers
  1.   private void merple()
  2.   {
  3.     java.util.HashMap<String, Object> map = new java.util.HashMap<String, Object>();
  4.     java.util.ArrayList<String> array = new java.util.ArrayList<String>();
  5.     array.addAll(map.keySet());
  6.     java.util.Collections.sort(array);
  7.   }
  8.  
Sep 1 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Michel | last post by:
Hi, I like to build a small simple dinamic website: Point-of-sale (detail-shop), So entering stock, logging all sales, print out invoices and download the logs to be imported into Excel. I...
0
by: Andreas Håkansson | last post by:
I've been using the Muenchian method to group XML data, however I have run into a situation where I am unsure how to solve it. What I need to do is select distinct groups of items. I have an XML...
4
by: Phil | last post by:
k, here is my issue.. I have BLOB data in SQL that needs to be grabbed and made into a TIF file and placed on the client (could be in temp internet dir). The reason we need it in TIF format is...
4
by: monomaniac21 | last post by:
hi! is it possible to do the aforementioned query - selecting only distinct in 1 col but retrieving all other cols at the same time. regards marc
3
by: wolverines1 | last post by:
Problems with creating a form from two tables, in access. The data won't come from the on of the tables. Need guidance. All data is under A, but when I make a form B, none of the data comes over...
3
by: Thembi | last post by:
I wanna know if ,the map stores sorted data or not.
3
by: dieselfuelonly | last post by:
<?php //Connect to the database and select the data from the table $username="asdfasdf"; $password="asdfasdf"; $database="asdfasdf"; mysql_connect(localhost,$username,$password);...
3
by: teephish | last post by:
Hello, I'm currently in the process of creating a small access database and I'm having some problems with creating a customized search. I would like the user to be able to search a record by last...
1
by: sampalmer21 | last post by:
Hi, I want to join two tables together and only select the rows that don't have the same customer first name and last name (see my code below). When I run my code with the DISTINCT keyword, it...
1
by: Harold Howe | last post by:
Howdy all, The msdn help says this about SorteList<k,v>: "If the list is populated all at once from sorted data, SortedList is faster than SortedDictionary." My question is this: how do I...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.