-4

I frequently use Dictionary types to store key and value pairs but I've been working with XML namespaces and need to store properties of the same name but may have different namespaces.

Is there a type of Dictionary that contains two values instead of one? What would it be called?

If something like this doesn't exist what would be a cool alternative?

Previous solved problem:

var defaultNamespace:String = "http://www.domain.com";
var attributes:Array = ["property1", "property2"];
var dictionary:Dictionary = new Dictionary();
dictionary["property1"] = defaultNamespace;
dictionary["property2"] = defaultNamespace;

New problem is properties with namespaces:

var defaultNamespace:String = "http://www.domain.com";
var namespace2:String = "library://ns.fl.com/fl";
var attributes:Array = ["property1", "library://ns.fl.com/fl::property1", "property2"];
var dictionary:Dictionary = new Dictionary();
dictionary["property1"] = {name:"property1",defaultNamespace};
dictionary["library://ns.fl.com/fl::property1"] = {name:"property1",namespace2};
dictionary["property2"] = {name:"property2",defaultNamespace};

I'd rather not create objects to contain two values.

VC.One
  • 14,790
  • 4
  • 25
  • 57
1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
  • I don't know about cool but I would simply make a class for the values. I mean, you could do a Tuple also... – IsolatedThinker Sep 19 '16 at 23:44
  • Could you please provide a link to the version of the C standard which allows this syntax? And check the other tags yourself! Don't spam tags! – too honest for this site Sep 19 '16 at 23:45
  • How is this question relevant to both C# and Java at the same time? Collection types are closely coupled to the framework which is used with the language, and C# and Java don't share frameworks. – Peter Duniho Sep 20 '16 at 00:15
  • As far as the question itself goes (undeniably too broad, especially given the lack of valid context): _"need to store properties of the same name but may have different namespaces"_ -- if the key is the attribute name, and you need to deal with attribute names that are the same except for having different namespaces, then obviously your key should incorporate the namespace name. There's no reason you should need to share the same key when you're really dealing with completely different attribute names. – Peter Duniho Sep 20 '16 at 00:17
  • Duplicate Question for Java: http://stackoverflow.com/q/521171/642706 – Basil Bourque Sep 20 '16 at 00:20
  • I'm using ActionScript 3 which is sibling to Java. I mentioned other languages because I do not think ActionScript has this specific feature and want to learn if other languages have something similar to what I'm asking about. Not sure where the down votes are from but I have already learned much from the comments and answers so those trying to close the question before I learn something are too late. – 1.21 gigawatts Sep 20 '16 at 00:34
  • Someone posted a comment to use a Map or List and then deleted it right away. ActionScript 3 doesn't have maps so I learned something. I will probably use QName for now as the value (forgot about it until now) and use a value object in the future. – 1.21 gigawatts Sep 20 '16 at 00:41
  • 2
    Your assumption that somebody is out to hamper your personal Stack Overflow experience is presumptuous. Stack Overflow is a user-moderated sites and we generally upvote, downvote, and close vote on the merits of the questions and answers, not on the needs of those who posted them. To better familiarize yourself with this site, perhaps you want to take the [tour](//stackoverflow.com/tour)? – tripleee Sep 20 '16 at 04:57
  • "*I'm using ActionScript 3 which is sibling to Java.*" That's just plain wrong. As3 is based on EcmaScript. If it's a sibling to anything, that would be JavaScript. And **no** JavaScript is not Java. – null Sep 20 '16 at 08:01
  • @null, to be fair AS3 is too hybrid. It can _feel_ like whatever you want. It's part EcmaScript and part C-like language (compiled, virtual machine loading, etc). With AS3 you can do OOP just like Java and C# etc but really only AS2 feels more like JavaScript. Also JS needs an HTML container, whilst both AS3 & Java can create "standalone" applications (for example both can output **exe**'s and both can output **apk**'s). – VC.One Sep 20 '16 at 08:27

3 Answers3

2

The .NET framework has Hashtable and Dictionary, both of which only have 1 value per key. This page has a table of data structures for reference: https://msdn.microsoft.com/en-us/library/7y3x785f(v=vs.110).aspx

You could:

  • Build your own data structure class. You could even modify the .NET implementations since MS provides the source code.

  • Store an array or other list structure as the value.

  • Store a custom type as the value (your own struct or class)

Chris Rollins
  • 550
  • 3
  • 9
1

Just use a simple container object of the language.

In Python, I used tuples. In Clojure, vectors are the typical tuple replacement, so I'm using them.

Really, if there were an implementation that had 2 values, it would probably just wrap them behind the scenes. And what happens when you need 3 values per key?

You may want to consider just wrapping the 2 values in a POD class. A single value with 2 named fields will be a lot more readily understandable.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • Did I get down voted because someone disagrees with the content, or because the question was down voted after the fact? – Carcigenicate Sep 20 '16 at 00:01
  • SO has become a question and argue site. So you have drive by down voters. Don't take it personal. I appreciate you taking time to provide an answer. – 1.21 gigawatts Sep 20 '16 at 00:37
  • @1.21gigawatts Well, at least people used to *argue* why their down vote was appropriate. Now it seems people down vote without leaving a comment more than they used to. – Carcigenicate Sep 20 '16 at 13:18
1

In Java you could define your own class with a pair of member objects. The JavaFX already defines such a class, Pair.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154