Java中,在HashMap可以用put(Key, Value)添加元素, 如何在LinkedHashMap中添加(Key, Value)?

Java中,在HashMap可以用put(Key, Value)添加元素, 如何在LinkedHashMap中添加(Key, Value)?,第1张

和HashMap方法一样,也是用put添加元素,LinkedHashMap也是java.util.Map的实现类

区别在于

Hashmap 是一个最常用的Map,它根据键的HashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度,遍历时,取得数据的顺序是完全随机的。 HashMap最多只允许一条记录的键为Null允许多条记录的值为 NullHashMap不支持线程的同步,即任一时刻可以有多个线程同时写HashMap可能会导致数据的不一致。如果需要同步,可以用 Collections的synchronizedMap方法使HashMap具有同步的能力,或者使用ConcurrentHashMap。

LinkedHashMap 是HashMap的一个子类,保存了记录的插入顺序,在用Iterator遍历LinkedHashMap时,先得到的记录肯定是先插入的.也可以在构造时用带参数,按照应用次数排序。在遍历的时候会比HashMap慢,不过有种情况例外,当HashMap容量很大,实际数据较少时,遍历起来可能会比 LinkedHashMap慢,因为LinkedHashMap的遍历速度只和实际数据有关,和容量无关,而HashMap的遍历速度和他的容量有关。

TreeMap实现SortMap接口,能够把它保存的记录根据键排序,默认是按键值的升序排序,也可以指定排序的比较器,当用Iterator 遍历TreeMap时,得到的记录是排过序的。

不知道 你具体需求是什么 HashMap有put方法。假设你要加入的key-value对是<Object, HashMap>,可以这么写:

HashMap<Object, HashMap>map1 = new HashMap()

Object obj = new Object()

HashMap<Object, Object>map2 = new HashMap()

map1.put(obj, map2)

分类: 电脑/网络 >>程序设计 >>其他编程语言

问题描述:

要用hashMap方法,我一用PUT就出错,帮看看哪里有错啊;最好讲讲HASHMAP的用法import javax.swing.*import java.util.*public class StudetHashMap{public static void main(String args[]){HashMap stu=new HashMap()System.out.println ("欢迎进入学生系统")System.out.println ("1.添加学生")System.out.println ("2.查询学生")System.out.println ("3.删除学生")int c=0do{int choice=Integer.parseInt(JOptionPane.showInputDialog(null,"请输入你的选择"))switch(choice){case 1:Student newstu=nullString code=JOptionPane.showInputDialog(null,"请你输入学号")String name=JOptionPane.showInputDialog(null,"请你输入姓名")newstu=new Student(code,name)breakcase 2:String codes=JOptionPane.showInputDialog(null,"请你输入学号")Student temp=nullfor (int i = 0i<stu.size()i++){temp=(Student)stu.get(i)if(temp.getCode().equals(codes)){break}}System.out.println (temp.getName())breakcase 3:String codee=JOptionPane.showInputDialog(null,"请输入学号")Student temps=nullint i=0for ( i = 0i<stu.size()i++){temps=(Student)stu.get(i)if(temps.getCode().equals(codee)){break}}stu.remove(i)}c=Integer.parseInt(JOptionPane.showInputDialog(null,"继续吗?1.继续\n2.不继续"))}while(c==1)}}class Student{private String nameprivate String codepublic Student(String code,String name){this.name=namethis.code=code}public void setCode(String code){this.code=code}public void setName(String name){this.name=name}public String getName(){return name}public String getCode(){return code}}

解析:

import javax.swing.*

import java.awt.*

import java.awt.event.*

import java.util.*

import .bruceeckel.swing.*

public class TrackEvent extends JApplet {

private HashMap h = new HashMap()

private String[] event = {

"focusGained", "focusLost", "keyPressed",

"keyReleased", "keyTyped", "mouseClicked",

"mouseEntered", "mouseExited", "mousePressed",

"mouseReleased", "mouseDragged", "mouseMoved"

}

private MyButton

b1 = new MyButton(Color.BLUE, "test1"),

b2 = new MyButton(Color.RED, "test2")

class MyButton extends JButton {

void report(String field, String msg) {

((JTextField)h.get(field)).setText(msg)

}

FocusListener fl = new FocusListener() {

public void focusGained(FocusEvent e) {

report("focusGained", e.paramString())

}

public void focusLost(FocusEvent e) {

report("focusLost", e.paramString())

}

}

KeyListener kl = new KeyListener() {

public void keyPressed(KeyEvent e) {

report("keyPressed", e.paramString())

}

public void keyReleased(KeyEvent e) {

report("keyReleased", e.paramString())

}

public void keyTyped(KeyEvent e) {

report("keyTyped", e.paramString())

}

}

MouseListener ml = new MouseListener() {

public void mouseClicked(MouseEvent e) {

report("mouseClicked", e.paramString())

}

public void mouseEntered(MouseEvent e) {

report("mouseEntered", e.paramString())

}

public void mouseExited(MouseEvent e) {

report("mouseExited", e.paramString())

}

public void mousePressed(MouseEvent e) {

report("mousePressed", e.paramString())

}

public void mouseReleased(MouseEvent e) {

report("mouseReleased", e.paramString())

}

}

MouseMotionListener mml = new MouseMotionListener() {

public void mouseDragged(MouseEvent e) {

report("mouseDragged", e.paramString())

}

public void mouseMoved(MouseEvent e) {

report("mouseMoved", e.paramString())

}

}

public MyButton(Color color, String label) {

super(label)

setBackground(color)

addFocusListener(fl)

addKeyListener(kl)

addMouseListener(ml)

addMouseMotionListener(mml)

}

}

public void init() {

Container c = getContentPane()

c.setLayout(new GridLayout(event.length + 1, 2))

for(int i = 0i <event.lengthi++) {

JTextField t = new JTextField()

t.setEditable(false)

c.add(new JLabel(event[i], JLabel.RIGHT))

c.add(t)

h.put(event[i], t)

}

c.add(b1)

c.add(b2)

}

public static void main(String[] args) {

Console.run(new TrackEvent(), 700, 500)

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11450836.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-16
下一篇2023-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存