首页 \ 问答 \ 如何声明Map的通用映射(How to declare generic Map of Map)

如何声明Map的通用映射(How to declare generic Map of Map)

如何声明这样一个Map而不是Object我有特定的类型:

Map<Class, Map<String, ClassInstance>> map;

这样可以用作:

Map<String, new Type()) valueMap = new HashMap();
map.put(Type.class, valueMap);

问题是我无法弄清楚如何声明'Class'和'ClassInstance'的泛型类型。


How to declare such a Map where instead of Object I have specific type:

Map<Class, Map<String, ClassInstance>> map;

Such that could be used as:

Map<String, new Type()) valueMap = new HashMap();
map.put(Type.class, valueMap);

The problem is I can't figure out how to declare generic type of both 'Class' and 'ClassInstance'.


原文:https://stackoverflow.com/questions/48670939
更新时间:2023-09-12 08:09

最满意答案

如果JTextFields不在同一个容器中,这将是一种可能的方法:

private List<JTextField> allTextFields = new ArrayList<JTextField>();

private JTextField createNewTextField(String text) {
    JTextField textField = new JTextField(text);
    allTextFields.add(textField);
    return textField;
}

private void resetAllTextFields(){
    for (JTextField textField : allTextFields) {
        textField.setText("");
    }
}

..然后,而不是使用构造函数JTextField myTextField = new JTextField("content")使用JTextField myTextField = createNewTextField("content");


If the JTextFields are not all in the same container, this would be a possible approach:

private List<JTextField> allTextFields = new ArrayList<JTextField>();

private JTextField createNewTextField(String text) {
    JTextField textField = new JTextField(text);
    allTextFields.add(textField);
    return textField;
}

private void resetAllTextFields(){
    for (JTextField textField : allTextFields) {
        textField.setText("");
    }
}

..and then instead of using the constructor JTextField myTextField = new JTextField("content") use JTextField myTextField = createNewTextField("content");

相关问答

更多
  • 为什么不用这个: Artisan::call('migrate:reset', ['--force' => true]); Why not use this: Artisan::call('migrate:reset', ['--force' => true]);
  • 如果你想在快速评估中获得良好的精度(但不是很高),你应该使用Chebyshev多项式的展开式:将这些系数列表(对于0.1%的准确性,你需要非常少的)并且用这些多项式的递归关系来评估扩展(这真的很简单)。 参考文献: 表格系数: http ://www.ams.org/mcom/1980-34-149/S0025-5718-1980-0551302-5/S0025-5718-1980-0551302-5.pdf chebyshev扩展评估: https ://en.wikipedia.org/wiki/Che ...
  • “Swing程序应该覆盖paintComponent()而不是重写paint() 。” - 在AWT和Swing中绘制:绘制方法 。 JPanel或JComponent是常用选项。 您可以使用合适的布局来控制展示位置。 附录: 这与Canvas什么关系? 类java.awt.Canvas是一个AWT组件; 而是使用Swing组件javax.swing.JPanel 。 以下是您的程序的一个变体,它只是选择一种随机颜色,但它可能会让您知道如何解决其他属性。 这里有一个相关的例子。 import java.aw ...
  • 使用适当的权限,您可以执行以下操作: mysql> system rm -f /tmp/get_show_grants.sql; mysql> SELECT CONCAT('system rm -f /tmp/show_grants.sql; SELECT CONCAT(\'SHOW GRANTS FOR \'\'\', `user`, \'\'\'@\'\'\', `host`,\'\'\';\') INTO OUTFILE \'/tmp/show_grants.sql\' ...
  • 我不知道Notepad ++的RegEx搜索和替换选项(这就是为什么这是一个注释),但是,如果它可用,像搜索: <([^<>/]+)/>和替换: <\1>应该这样做。 I don't know Notepad++'s RegEx search and replace options (which is why this was a comment), but, if it is available, something like Search:<([^<>/]+)/> and Replace:<\ ...
  • 您的DetailPanel没有与之关联的布局管理器,这意味着当您向其添加子项时它不会展开,这意味着JScrollPane没有可以滚动的任何位置。 在DetailPanel上调用setLayout()或覆盖getPreferredSize()以添加其子级的高度并返回有意义的值。 Your DetailPanel has no layout manager associated with it, which means it doesn't expand when you add children to it, ...
  • 这两个问题源于对GridBagLayout的错误理解。 更多的阅读和实验应该有所帮助)回答手头的问题: 1)这里的问题是你想要一个GridBagLayout,而是添加2个独立的面板。 结果:顶部网格袋中的列独立于底部网格袋中的列。 要纠正这个问题,您可以尝试两件事: 将两个标签和两个文本字段添加到单个GridBag面板。 这样,列将对齐。 为标签设置最小和首选大小,使其宽度匹配并将其权重x设置为0(文本字段的权重x为非零)。 这样你就可以让GridBags为标签和文本字段分配相同的空间。 第一种方法是首选方 ...
  • 我不知道是否可以给出完整的回溯,但你可以给'bt'一个数字参数来获得更多的帧: (gdb) bt 10 #0 x () at test.c:4 #1 0x080483bf in x () at test.c:4 #2 0x080483bf in x () at test.c:4 #3 0x080483bf in x () at test.c:4 #4 0x080483bf in x () at test.c:4 #5 0x080483bf in x () at test.c:4 #6 0x08 ...
  • 我不相信你可以通过一个查询获得所有照片。 这将是我觉得的一个非常大的光标对象,加上我读到的关于实际将照片存储在数据库中的所有内容都很麻烦。 相反,您可以轻松查询所有联系人ID并通过获取照片 public static Bitmap loadContactPhoto(ContentResolver cr, long id) { Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id); InputStrea ...
  • Q1 让我们首先在O(n)解决这个更容易的问题:找到i0 < j0 ,使A[j0] - A[i0]最大化。 对于每个j0 ,我们需要找到最小值0, 1, ..., j0 - 1并将A[j0] - this minimum与全局最大值进行比较。 到目前为止,只需计算最小值即可轻松完成。 现在,对于您的原始问题,我们还需要i1 < j1 ,使A[j1] - A[i1]最大化。 或者,对于每个i1 ,我们需要找到j1 > i1 ,使A[j1] - A[i1]最大化。 让: min[i] = minimum in ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。