我想在key上对TreeMap进行排序.Key是一些具有int,List,String等的自定义DataStructure.我期望排序的成员有一些重复.假设成员是Rank.超过1个对象可以具有相同的排名.
简化版示例:
注意:在CompareTo方法中,0不会故意返回0而不是忽略重复.(如果这不是避免重复的正确方法,请纠正我)
import java.util.TreeMap;
public class TreeTest {
public static void main(String[] args) {
TreeMap<Custom,String> t = new TreeMap<Custom,String>();
Custom c1 = new Custom();
c1.setName("a");
c1.setRank(0);
Custom c2 = new Custom();
c2.setName("b");
c2.setRank(1);
Custom c3 = new Custom();
c3.setName("c");
c3.setRank(0);
t.put(c1, "first");
t.put(c2, "Second");
t.put(c3, "Third");
System.out.println(t.keySet());
for(Custom c:t.keySet()){
System.out.println(t.get(c));
}
}
}
Run Code Online (Sandbox Code Playgroud)
和自定义对象
package com.example.ui;
public class Custom implements Comparable<Custom>{
int rank;
String name;
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = …Run Code Online (Sandbox Code Playgroud) public int compareTo(Object o) {
Doctor d = (Doctor)o;
return (doctor_name.compareTo(d.doctor_name));
}
Run Code Online (Sandbox Code Playgroud)
这是我在博士课上的可比性
jTextArea.setText("");
int doctor_id = Integer.parseInt(jTextField2.getText());
String doctor_name = jTextField3.getText();
String hospitalName = jTextField4.getText();
Doctor d = new Doctor(doctor_id,doctor_name,hospitalName);
hmDoctor.put(doctor_id, d);
Run Code Online (Sandbox Code Playgroud)
这是我的hashmap
这是我的树图
TreeMap<Integer,Doctor> tmDoctor = new TreMap<Integer,Doctor>(hmDoctor);
jTextArea.setText("");
Set keys = tmDoctor.keySet();
Iterator<Integer> ite = keys.iterator();
while(ite.hasNext())
{
int doctorID = ite.next();
Doctor d = tmDoctor.get(doctorID);
tmDoctor.put(doctorID, d);
jTextArea1.append(d.toString());
}
Run Code Online (Sandbox Code Playgroud)
它没有对医生姓名进行排序.为什么?
我正在尝试编写一个程序,用户可以:1)向联系人添加人员(姓名,电话,电子邮件),2)从联系人中删除一个人,3)从联系人中读取所有人.
我这样做的方式是我要求用户选择并分别做任何事情.对于写入,我只是将一个对象写入该文件.为了删除,我想我会问用户"姓"将用作KEY(因为我正在使用TreeMap)并将删除键上的值(对象).
所以我在这里读书时遇到了问题.我试图像这样读取对象:
public void readContact()
{
TreeMap<String, Contact> contactMap = new TreeMap<String, Contact>();
try
{
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(
new FileInputStream(file)));
while( in.available() > 0 ) //This line does NOT read
{
Contact c = (Contact)in.readObject();
contactMap.put(c.getLastName(), c);
}
for(Map.Entry contact : contactMap.entrySet() )
{
Contact con = contactMap.get( contact.getKey() );
System.out.println( con.getLastName() + ", " + con.getFirstName() + ": " + con.getPhoneNumber() + "\t" + con.getEmail());
}
}
catch(Exception e)
{
System.out.println("Exception caught");
}
}
Run Code Online (Sandbox Code Playgroud)
请不要建议做 …
我在java中使用treeMap.我想获得所有值的排序列表/集合.TreeMap.values()可以解决这个问题吗?
我得到的集合,它将基于keySet进行排序,或者此集合是随机的.
谢谢.
我试图为我的treemap(内存)使用firstKey()方法.我的代码看起来像:
import java.util.*;
//Code in the middle.
System.out.println(memory.firstKey());
Run Code Online (Sandbox Code Playgroud)
但它给了我这个错误:
GameLogic.java:276: cannot find symbol
symbol : method firstKey()
location: interface java.util.Map<java.lang.Integer,java.lang.Character>
System.out.println(memory.firstKey());
^
Run Code Online (Sandbox Code Playgroud)
所有建议都赞赏.如果我也使用lastKey(),也会发生同样的错误.
我将TreeMap填充到intent中:
private TreeMap<Long, Long> mSelectedItems;
...
mSelectedItems = new TreeMap<Long, Long>();
...
Intent intent = new Intent();
intent.putExtra("results", mSelectedItems);
setResult(RESULT_OK, intent);
Run Code Online (Sandbox Code Playgroud)
然后尝试在调用活动中将其读回:
TreeMap<Long, Long> results = (TreeMap<Long, Long>)
data.getSerializableExtra("results");
Run Code Online (Sandbox Code Playgroud)
什么导致:
E/AndroidRuntime(26868): Caused by: java.lang.ClassCastException: java.util.HashMap cannot be cast to java.util.TreeMap
Run Code Online (Sandbox Code Playgroud)
嗯,当然,但是因为我没有在代码中的任何地方使用HashMap,并且因为TreeMap实现了Serializable,所以这应该没有错误,不是吗?为什么android试图强迫我上课我不使用?
我刚刚开始在Java中实现数据结构,并且想知道我们可以有这样的情况.
Map<HashMap<String,String>,String> map = new HashMap<HashMap<String,String>,String>();
Run Code Online (Sandbox Code Playgroud)
如果是的话,请举一个小例子.
如果您没有找到相关问题,请在评论中提及,
我正在尝试修改一个代码,我不能在这里发布,所以我在这里有一个修剪版本.
我使用HashMap时输出不稳定.
HashMap<Integer, String> test= new HashMap<>();
test.put(1, "one");
test.put(2, "one");
test.put(3, "one");
test.put(4,"four");
test.put(5, "one");
test.put(6, "one");
test.put(10, "one");
test.put(19, "one");
test.put(20, "Sixteen");
System.out.println(test);
HashMap<Integer, String> test3= new HashMap<>(200);
test3.put(1, "one");
test3.put(2, "one");
test3.put(3, "one");
test3.put(4,"four");
test3.put(5, "one");
test3.put(6, "one");
test3.put(10, "one");
test3.put(19, "one");
test3.put(20, "Sixteen");
System.out.println(test3);
Run Code Online (Sandbox Code Playgroud)
输出
test --> {1=one, 19=one, 2=one, 3=one, 4=four, 20=Sixteen, 5=one, 6=one, 10=one}
test3--> {1=one, 2=one, 3=one, 4=four, 5=one, 6=one, 10=one, 19=one, 20=Sixteen}---> My desired output.
Run Code Online (Sandbox Code Playgroud)
即使输入值相同,为什么结果也不同.这种排序有何不同,即元素的存储?
我无法使用第二种方法,因为大小是动态的,它会根据应用程序不断变化.我可以使用TreeMap,并为所有值获得一致的输出.
这是java TreeMap实现的标题(1.8_071):
public class TreeMap<K,V>
extends AbstractMap<K,V>
implements NavigableMap<K,V>, Cloneable, java.io.Serializable
Run Code Online (Sandbox Code Playgroud)
为什么没有限制,像这样:
public class TreeMap<K extends Comparable<?>,V>
extends AbstractMap<K,V>
implements NavigableMap<K,V>, Cloneable, java.io.Serializable
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用PDFbox填充重复的表单。我正在使用TreeMap并使用单个记录填充表单。pdf格式的格式是在第一页上列出了六个记录,在第二页上插入了一个静态页。(对于大于六个记录的TreeMap,将重复此过程)。我得到的错误特定于TreeMap的大小。这就是我的问题。我不知道为什么当我用超过35个条目填充TreeMap时收到此警告:
2018年4月23日2:36:25 org.apache.pdfbox.cos.COSDocument最终定稿警告:警告:您尚未关闭PDF文档
public class test {
public static void main(String[] args) throws IOException, IOException {
// TODO Auto-generated method stub
File dataFile = new File("dataFile.csv");
File fi = new File("form.pdf");
Scanner fileScanner = new Scanner(dataFile);
fileScanner.nextLine();
TreeMap<String, String[]> assetTable = new TreeMap<String, String[]>();
int x = 0;
while (x <= 36) {
String lineIn = fileScanner.nextLine();
String[] elements = lineIn.split(",");
elements[0] = elements[0].toUpperCase().replaceAll(" ", "");
String key = elements[0];
key = key.replaceAll(" ", "");
assetTable.put(key, elements);
x++;
}
PDDocument …Run Code Online (Sandbox Code Playgroud)