Contact Us
  
 
 
    SearchSearch   RegisterRegister   Log inLog in 

duplicate key element in java collection

duplicate key element in java collection

 
Post new topic   Reply to topic     Forum Index -> Java,Struts framework, Velocity, JSF, Hibernate, Servlet, JSP, javaBeans
View previous topic :: View next topic  
Author Message
tiffany



Joined: 08 Dec 2008
Posts: 1

PostPosted: Sat Dec 20, 2008 5:41 am    Post subject: duplicate key element in java collection Reply with quote

Hi to everyone,
In java collections, which collection can allow duplicate key or elements. In which add more element with same key with different elements.

I want basically to use key value pair collection of java with same key.

a=10
a=11
a=12
b=13
c=14
d=15

as my required output

a=10,11,12
b=13
c=14
d=15

but all collection return

a=12
b=13
c=14
d=15

Or any other method to pair key value to get with duplicate values

thanks in advance
Back to top
View user's profile Send private message
javatech



Joined: 19 Jan 2007
Posts: 39

PostPosted: Sat Dec 20, 2008 4:26 pm    Post subject: Reply with quote

Cannot use directly to use duplicate key or element in collection
but can make logic and use collection with duplication of key

Quote:

a=10,11,12
b=13
c=14
d=15


see in this

a category having 10, 11, 12

can put it in ArrayList 10, 11, 12
this ArrayList now use as element of any collection of java
Back to top
View user's profile Send private message
deepaksaluja



Joined: 04 Sep 2008
Posts: 1

PostPosted: Mon Dec 22, 2008 12:49 pm    Post subject: Reply with quote

Try also
MultiHashMap of apache collection

This also allow duplicate collection with key or value


http://commons.apache.org/collections/apidocs/org/apache/commons/collections/MultiHashMap.html

Example of MultiHashMap

Code:
import org.apache.commons.collections.MultiHashMap;
import java.util.Set;
import java.util.Map;
import java.util.Iterator;
import java.util.List;
public class MultiMapExample {

   public static void main(String[] args) {
      MultiHashMap mp=new MultiHashMap();
      mp.put("a", 10);
      mp.put("a", 11);
      mp.put("a", 12);
      mp.put("b", 13);
      mp.put("c", 14);
      mp.put("e", 15);
      List list = null;

      Set set = mp.entrySet();
      Iterator i = set.iterator();
      while(i.hasNext()) {
         Map.Entry me = (Map.Entry)i.next();
         list=(List)mp.get(me.getKey());
         
         for(int j=0;j<list.size();j++)
         {
          System.out.println(me.getKey()+": value :"+list.get(j));
         }
      }
   }
}



output of this example

Quote:
a: value :10
a: value :11
a: value :12
c: value :14
b: value :13
e: value :15
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Java,Struts framework, Velocity, JSF, Hibernate, Servlet, JSP, javaBeans All times are GMT
Page 1 of 1

 
Jump to:  
  Quick Post a Reply Topic
     
  Name (required)  
     
  Email Address (will not be published) (required)  
     
  Subject of topic (required)  
     
   
     
   
     
  Advertisement  
     
 
     


  More Links  
 
JSP Introduction
JSP Setup
Simple JSP Example
JSP Variable
Array Declaration in JSP
JSP Forms and User Input
Form Validation
JSP Cookies
JSP Session Object
Loop, content collection, iterator, conditional check in JSP
JSP Application Object
JSP Include
JSP Response Object
JSP Request Object
JSP Error Handling
JSP File Handling Object
JSP Action Tag
JSP Implicit Objects
JSP Declaration
JSP Directive
JDBC Introduction
JDBC Driver
JDBC Connection
JDBC Prepared/Statement
JDBC ResultSet
JDBC Close Connection object
JDBC Insert
JDBC Display Records
JDBC Update
JDBC Delete
JDBC Sorting
JDBC Query
JDBC Exception
JDBC Add Batch
JDBC Transaction/RollBack