473,432 Members | 1,737 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,432 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 1815
Oralloy
988 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.