标签: keyvaluepair

创建哈希的快捷方式

我是Ruby的新手.我正在阅读关于Rubymonk的教程,我正在学习如何创建哈希.你能告诉我为什么我不能在没有key_value_pairs变量的情况下创建哈希.我的代码抗性大脑似乎合乎逻辑,代码应该在没有它的情况下工作,但事实并非如此.

这不起作用

def artax
  a = [:punch, 0]
  b = [:kick, 72]
  c = [:stops_bullets_with_hands, false]

  Hash[a,b,c]
end
p artax
Run Code Online (Sandbox Code Playgroud)

这有效.

def artax
  a = [:punch, 0]
  b = [:kick, 72]
  c = [:stops_bullets_with_hands, false]
  key_value_pairs = [a, b, c]
  Hash[key_value_pairs]
end
p artax
Run Code Online (Sandbox Code Playgroud)

ruby hash keyvaluepair

6
推荐指数
1
解决办法
517
查看次数

Lambda表达式中的KeyValuePair

我正在尝试使用lambda表达式创建KeyValue对集合.

这是我的班级,下面是我的lambda代码.我没能创建KeyValuePair.

我想获得一部关于Id的KeyValuePair的集合,IsReleased用于 喜剧电影.我将这些KeyValuePair放在HashSet中以便快速搜索.

 public class Movie{
  public string Name{get;set;}
  public int Id{get;set;}
  public bool IsReleased{get;set;}
  //etc
 }

List<Movie> movieCollection=//getting from BL

var movieIdReleased= new 
HashSet<KeyValuePair<int,bool>>(movieCollection.Where(mov=> mov.Type== "comedy")
                                    .Select(new KeyValuePair<int,bool>(????));
Run Code Online (Sandbox Code Playgroud)

c# lambda keyvaluepair

5
推荐指数
1
解决办法
7478
查看次数

在c#中表示字符串对列表的最简单方法?

我想创建几个字符串对的短(有序)列表,并在代码中有一个简洁的文字表示.有一个很好的方法来做到这一点?我能想到的最好的是

     using Pairs = System.Collections.Specialized.OrderedDictionary;
     ...
            var z = new Pairs()
            {
                { "a", "b" },
                { "c", "d" }
            };
Run Code Online (Sandbox Code Playgroud)

但缺乏类型安全性有点可怕.似乎没有通用的有序字典.

还有哪些类具有文字初始化语法?是否可以为类定义自己的初始化语法?

c# literals keyvaluepair

5
推荐指数
2
解决办法
5268
查看次数

使用具有两个泛型字段的类覆盖hashCode

我正在实现一个类Pair,将它用作具有两个值的键HashMap.我使用泛型来保持字段的类型变量.我设法写了代码的最大部分:

public class Pair<L, R>
{
    private L left;
    private R right;


    Pair(L left, R right)
    {
        this.left = left;
        this.right = right;
    }


    public L getLeft()
    {
        return left;
    }


    public R getRight()
    {
        return right;
    }


    public void setLeft(L left)
    {
        this.left = left;
    }


    public void setRight(R right)
    {
        this.right = right;
    }


    @Override
    public boolean equals(Object obj)
    {
        if (obj instanceof Pair< ? , ? >)
        {
            Pair< ? , ? > …
Run Code Online (Sandbox Code Playgroud)

java hashmap hashcode keyvaluepair

5
推荐指数
1
解决办法
4561
查看次数

如何从列表内的 KeyValuePair 检索值?

我有以下列表,我需要在其上运行 Contains() 或类似方法来检索与特定键关联的值。我该怎么做?我到处搜索并尝试了很多东西,但我似乎无法弄清楚。

List<KeyValuePair<string, int>> shopInventory = new List<KeyValuePair<string, int>>();
Run Code Online (Sandbox Code Playgroud)

我认为它是这样的:

shopInventory.Contains((KeyValuePair<string, int>
Run Code Online (Sandbox Code Playgroud)

如果我忽略了任何重要信息,我会很乐意提供。

c# list contain keyvaluepair

5
推荐指数
1
解决办法
5485
查看次数

在 Jenkins 中使用键值对

这是我在 Jenkins 中有参数的要求:

1. User selects 3 Values from Dropdown: DEV, QA, PROD
2. Upon selection I need to return single value as parameter as like this:
 If DEV selected, return "Development http://dev.com 1"
 If QA selected, return "QA http://qa.com 2"
 If PROD selected, return "Production http://prod.com 3"
3. Once the value is returned in a variable, I will use that variable value in next step of 'Windows batch command'.
Run Code Online (Sandbox Code Playgroud)

在哪里以及如何定义键/值。我尝试使用扩展选择参数插件,但不确定如何执行此操作。

key-value-observing jenkins jenkins-plugins keyvaluepair

5
推荐指数
2
解决办法
1万
查看次数

c# List&lt;KeyValuePair&gt; 通过键或索引获取值

如何通过keyKeyValuePair的键获取

我有一个 List<KeyValuePair<string, string>>

var dataList = new List<KeyValuePair<string, string>>();

// Adding data to the list
dataList.Add(new KeyValuePair<String, String>("name", "foo"));
dataList.Add(new KeyValuePair<String, String>("name", "bar"));
dataList.Add(new KeyValuePair<String, String>("age", "24"));
Run Code Online (Sandbox Code Playgroud)

为该列表创建一个循环:

foreach (var item in dataList) {
    string key = item.Key;
    string value = item.Value;
}
Run Code Online (Sandbox Code Playgroud)

我想做的是以string name = item["name"].Value这样的方式进行:

foreach (var item in dataList) {
    // Print the value of the key "name" only
    Console.WriteLine(item["name"].Value);

    // Print the value of the key "age" only
    Console.WriteLine(item["age"].Value);
}
Run Code Online (Sandbox Code Playgroud)

或者也许可以通过 …

c# linq keyvaluepair

5
推荐指数
1
解决办法
2万
查看次数

爪哇。是否可以在优先级队列中使用一对,然后使用键作为优先级返回一个值

所以我想使用最小的键作为优先级,然后返回对应键的值:

import javafx.util.Pair;
import java.util.PriorityQueue;

public class Test
{
    public static void main (String[] args)
    {
        int n = 5;

        PriorityQueue <Pair <Integer,Integer> > l = new PriorityQueue <Pair <Integer,Integer> > (n);

        l.add(new Pair <> (1, 90));
        l.add(new Pair <> (7, 54));
        l.add(new Pair <> (2, 99));
        l.add(new Pair <> (4, 88));
        l.add(new Pair <> (9, 89));

        System.out.println(l.poll().getValue()); 
    }
}
Run Code Online (Sandbox Code Playgroud)

我寻找的输出是 90,因为 1 是最小的键。即使将该值用作优先级并返回键也很好,因为我可以在必要时交换数据。我想使用值/键作为优先级(在这种情况下是最小值)来显示键/值。我不知道在这种情况下如何做到这一点。这在 C++ 中工作正常。

java collections priority-queue keyvaluepair

5
推荐指数
2
解决办法
5550
查看次数

如何从ComboBox的SelectedItem获取密钥?

我想获得的关键SelectedItemComboBox,但不弄清楚如何让我做的是代码,

void CboBoxSortingDatagridview(ComboBox sender)
{
    foreach (var v in DictionaryCellValueNeeded)
    {
        if (!DictionaryGeneralUsers.ContainsKey(v.Key) && v.Value.RoleId == Convert.ToInt32(((ComboBox)sender).SelectedItem)) // here getting value {1,Admin} i want key value which is 1 but how?
        {
            DictionaryGeneralUsers.Add(v.Key, (GeneralUser)v.Value);
        }
    }
    dataGridViewMain.DataSource = DictionaryGeneralUsers.Values;
}  
Run Code Online (Sandbox Code Playgroud)

我用这种方式绑定了组合框,

cboRolesList.DataSource = new BindingSource(dictionaryRole, null);  
cboRolesList.DisplayMember = "Value";  
cboRolesList.ValueMember = "Key";
Run Code Online (Sandbox Code Playgroud)

c# combobox keyvaluepair

4
推荐指数
1
解决办法
2万
查看次数

字典:无法将属性或索引器分配给:它是只读的

我正在尝试更改字典中 Keys 的值,如下所示:

//This part loads the data in the iterator
List<Recommendations> iterator = LoadBooks().ToList();
//This part adds the data to a list 
List<Recommendations> list = new List<Recommendations>();

        foreach (var item in iterator.Take(100))
        {
            list.Add(item);
        }
        //This part adds Key and List as key pair value to the Dictionary
        if (!SuggestedDictionary.ContainsKey(bkName))
        {
            SuggestedDictionary.Add(bkName, list);

        }
       //This part loops over the dictionary contents 
  for (int i = 0; i < 10; i++)
        {
            foreach (var entry in SuggestedDictionary)
            {
                rec.Add(new Recommendations() …
Run Code Online (Sandbox Code Playgroud)

c# setter dictionary key keyvaluepair

4
推荐指数
1
解决办法
3747
查看次数