Tag: map

迭代在Java中的HashMap

我试图在Java中迭代hashmap,这应该是一件相当容易的事情。 但是,以下代码给了我一些问题: HashMap hm = new HashMap(); hm.put(0, “zero”); hm.put(1, “one”); Iterator iter = (Iterator) hm.keySet().iterator(); while(iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); System.out.println(entry.getKey() + ” – ” + entry.getValue()); } 首先,我需要在hm.keySet()。iterator()上强制转换Iterator,否则它会说“类型不匹配:无法从java.util.Iterator转换为Iterator”。 但后来我得到“方法hasNext()未定义类型Iterator”,并且“方法hasNext()未定义类型Iterator”。

Java – 使用Graphics2D矩形在面板中创建2D平铺贴图?

我正试图在一个非常基本的程序中模拟一场战斗,但由于这是我第一次使用Java编写一个大程序,所以我对如何继续进行操作几乎一无所知。 我以为我会有一个600×600的大面板并使用Graphics2D来绘制20×20矩形的地形……不幸的是,即使有几个教程,我也不知道该怎么做。 我有10种不同类型的地形可以循环,还有5种不同的景观轮廓。 基本上我想让程序做的是当我在combobox中选择一个特定的轮廓时,它描绘了战斗中的景观和两个相对的侧面(虽然我还没有完全在那里) 老实说,我在该计划中没有取得很大进展。 我应该只使用Graphics2D和矩形这样的东西,还是应该切换到OpenGL或类似的东西? 虽然凭借我目前的Java经验,我认为如果没有帮助我就不会对它有太大的帮助。 无论如何,这是我到目前为止所拥有的: public class Map extends JPanel { int n = 1; int x; int y; int Area = 750; public Color City = new Color(214,217,223); public Color Desert = new Color(255,204,102); public Color DirtRoad = new Color(153,102,0); public Color Forest = new Color(0,102,0); public Color Hills = new […]

将结果集值放入Collection对象,然后添加到ArrayList

我正在处理我的resultset以获取详细信息。 我需要返回一个ArrayList ,那么如何将resultset集中的键值放到任何集合对象中,然后将它们放到ArrayList ? 这是代码: public List addBikes() throws ClassNotFoundException, SQLException{ List bikeList = new ArrayList(); Class.forName(“com.mysql.jdbc.Driver”); Connection con = null; Statement stm = null; ResultSet rs = null; con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/spring_hibernate”,”root”,”root”); ; stm=con.createStatement(); String qry=”Select * from bike”; rs=stm.executeQuery(qry); while(rs.next()) { /* I need to put * rs.getString(“name”),rs.getString(“model”) * etc into any of the suitable collection […]

JSTL打印嵌套对象

如何在JSP页面上的地图中打印嵌套对象/属性的值? 那么让我们说Survey有一个名为Questions的属性(这是另一个对象),我想打印那些问题(survey.questions.getId()或survey.questions.getTitle()),那个foreach语句看起来怎么样? 编辑:调查是地图而不是集合

将MOXy的元数据与实现java.util.Map的类一起使用时的NPE

情况 我正在使用EclipseLink的MOXy,我正在尝试将外部OX映射XML与实现Map接口的类一起使用。 但是,每次我尝试创建一个JAXBContext时,我都会得到以下NPE: Caused by: javax.xml.bind.JAXBException – with linked exception: [java.lang.NullPointerException] at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:832) at org.eclipse.persistence.jaxb.JAXBContext.(JAXBContext.java:143) at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:142) at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:129) at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:93) at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:83) at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:210) at javax.xml.bind.ContextFinder.find(ContextFinder.java:336) at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574) at com.example.MOXyOXTest(MOXyOXTest.java:59) Caused by: java.lang.NullPointerException at org.eclipse.persistence.jaxb.compiler.XMLProcessor.processXML(XMLProcessor.java:202) at org.eclipse.persistence.jaxb.compiler.Generator.(Generator.java:145) at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:829) 细节 如果要映射的类实现java.util.Map接口,则只会出现此问题。 如果我映射的类没有实现该接口,那么一切正常。 这是我试图映射的类的简化示例: package com.example; import java.util.Map; // This class just wraps a java.util.HashMap […]

按键加入两张地图

我有两张地图: Map mapOne = {(1,”a”), (2, “b”)}; Map mapTwo = {(1,10.0), (2,20.0)}; 我希望通过Integer值将这些映射组合成一个,所以结果映射是 Map mapResult = {(“a”,10.0), (“b”,20.0)}; 有没有办法比迭代条目集更容易做到这一点?

从JSON获取数据

我正试图从这个JSON字符串中获取值,但我很难实现这一点。 {“DebugLogId”:”1750550″,”RequestId”:”17505503″,”Result”: {“Code”:””,”DebugLogId”:”1750550″,”Message”:””}, “Suggestions”:[ {“Ranking”:”1″,”Score”:”60″,”Title”:”This is a test message 1″}, {“Ranking”:”2″,”Score”:”60″,”Title”:”This is a test message 2″} ]} 在“建议”中访问数据最简单的方法是什么? 我正在使用GSON模块。 理想情况下,我想将它全部放在HashMap中。 感谢您的任何帮助和/或建议! 谢谢你的帮助!

如何在Java中交换arrayMap值和键

我在逆转给定地图并将其反转的键和值存储到另一个地图时遇到了一些麻烦。 我有一个方法原型如下: public static Map<String, Set> reverse (Map <String, Set> graph); 因此,如果我有有向图的样本密钥,那么: {c -> arraySet{f, e}} {b -> d} {a -> arraySet{c, b}} {d -> g} {e -> d} {f -> arraySet{g, d}} 我需要有效地反转这个图,以便代替b – > d我有d – > b。 我认为所有这些要求对我来说是交换原始图中的值和键并将它们添加到reverseMap。 我想我可以迭代图中给定键的每组值,然后将它们存储在列表中。 不幸的是,我在实现这个问题时遇到了麻烦。 我真的很感激在正确的方向上轻推。

使用@OneToMany Map的EclipseLink JPA“此上下文中的无效表”

我希望我在这里做些傻事…… 我正在尝试为Map设置JPA注释并获得以下堆栈跟踪。 Exception [EclipseLink-6069] (Eclipse Persistence Services – 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.QueryException Exception Description: The field [EMPLOYEE.PHONE_TYPE] in this expression has an invalid table in this context. Query: ReadAllQuery(name=”phones” referenceClass=Phone ) at org.eclipse.persistence.exceptions.QueryException.invalidTableForFieldInExpression(QueryException.java:739) at org.eclipse.persistence.internal.expressions.FieldExpression.validateNode(FieldExpression.java:281) at org.eclipse.persistence.expressions.Expression.normalize(Expression.java:3259) at org.eclipse.persistence.internal.expressions.DataExpression.normalize(DataExpression.java:369) at org.eclipse.persistence.internal.expressions.FieldExpression.normalize(FieldExpression.java:208) at org.eclipse.persistence.internal.expressions.SQLSelectStatement.normalize(SQLSelectStatement.java:1377) at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.buildNormalSelectStatement(ExpressionQueryMechanism.java:546) at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.prepareSelectAllRows(ExpressionQueryMechanism.java:1700) at org.eclipse.persistence.queries.ReadAllQuery.prepareSelectAllRows(ReadAllQuery.java:721) at org.eclipse.persistence.queries.ReadAllQuery.prepare(ReadAllQuery.java:657) at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:614) at org.eclipse.persistence.queries.ObjectLevelReadQuery.checkPrepare(ObjectLevelReadQuery.java:883) at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:575) […]

如何阅读hadoop顺序文件?

我有一个顺序文件,它是hadoop map-reduce作业的输出。 在此文件中,数据以键值对forms写入,值本身是映射。 我想将值作为MAP对象读取,以便我可以进一步处理它。 Configuration config = new Configuration(); Path path = new Path(“D:\\OSP\\sample_data\\data\\part-00000”); SequenceFile.Reader reader = new SequenceFile.Reader(FileSystem.get(config), path, config); WritableComparable key = (WritableComparable) reader.getKeyClass().newInstance(); Writable value = (Writable) reader.getValueClass().newInstance(); long position = reader.getPosition(); while(reader.next(key,value)) { System.out.println(“Key is: “+textKey +” value is: “+val+”\n”); } 程序输出:键是:[这是关键]值是:{abc = 839177,xyz = 548498,lmn = 2,pqr = 1} 在这里我获得了作为字符串的价值,但我希望它作为地图的对象。