[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


Java 7 HashMap of Generics

Posted on 2014-03-30
Medium Priority
Last Modified: 2014-04-01
I have a situation where I think a HashMap of Generic types is the best data structure, but having a challenge implementing it...which of course makes me think I either did to learn Java better or am choosing the wrong data structure

I have 50+ networks of the form Network <S,T> where S & T are classes of Agent, Knowledge, Resource, Location (and others)...all of which extend Node

Each network has a String name of the form "agent x knowledge" where each portion of the label corresponds to the class of the vertex.

I think I want a HashMap<String, Network<S,T>>...

I'm having a challenge using a nested for loop to run through the list of Class types and create the networks...pseudo code below
for each source node class {
     for each sink node class {
        Network <Source, Sink> = new Network <Source, Sink>()

Open in new window

I had thought an array of Class[] = {Agent.class, Resource.class} would work as a source in a foreach loop, like this
for (Class src : nodeClassesOfInterest){
  for (Class sink : nodeClassesOfInterest){
  Network<src, sink> = new Network<src,sink> ();

Open in new window

but eclipse reports an error that I forget at the moment. Is the above legal? If so, anything obvious that leaps out at you (the reader) to help me fix?  If illegal by language syntax or conventions, what are the alternatives to hand encoding 50+ networks?  Is there a better choice in STL or elsewhere for storing this set of networks of varying node types?
Question by:michaellanham
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 35

Accepted Solution

girionis earned 800 total points
ID: 39965971
for (Class src : nodeClassesOfInterest){
  for (Class sink : nodeClassesOfInterest){
  Network<src, sink> = new Network<src,sink> ();

Open in new window

The above is not legal. You should do

for (Class src : nodeClassesOfInterest){
  for (Class sink : nodeClassesOfInterest){
  Network<Class, Class> network= new Network<Class,Class> (); // or Network<Class, Class> network= new Network<Class,Class> (src, sink); if you have the proper constructor

Open in new window

LVL 36

Assisted Solution

mccarl earned 400 total points
ID: 39966306
The important thing to remember is that Generics are a "compile time only" construct, ie. at runtime, all of those Network objects are essentially the same. So if you need to store runtime information about the Network objects, than you need to do something like what gironis has posted.

What might be a good idea is to tell us what you intend to use these Network objects for? That will better guide us in recommending the best data structure to use.

Author Comment

ID: 39966399
Admin...thank you. I missed that option when generating the question. not sure why.

My intended use, to possibly guide suggestions for other (better?) data structure, follows...
Used to capture "Dynamic meta-networks" that are models of, in this case, of socio-technical organizations. Node types include agent, organization, resource, task, knowledge, belief, action, location, and event. Generally these get used with networks of the form src x sink (can also be thought of as sparse matrices of src x sink). The counting of links and matrix algebra help generate social network analysis (SNA) metrics (e.g., betweenness centrality, and when using >=2 matrices, task-knowledge congruence for agents). That is the general abstracted purpose...make sense?

In my particular case right now, I need to generate many artificial 'organizations' comprised of these multiple vertice types for ingestion by an application called ORA, written at Carnegie Mellon University (CMU)'s Center for Computational Analysis of Social and Organizational Systems (CASOS). I have briefly looked at JUNG to see if I could use different vertex types in the same graph, and I do not see that as feasible--room for education though! The random organization generation is in support of a SNA measure development effort and sensitivity analysis of the measure.  Is that sufficient detail?

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question