java集合

mac2022-06-30  111

java集合

1. Map编写equals和hashCode

// java实例类 public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } } // 主函数 public class Main { public static void main(String[] args) { List<Person> list = Arrays.asList(new Person("ajing", 20), new Person("ling", 23), new Person("shang", 25)); Map<Person, String> map = new HashMap<>(); for (Person item : list) { map.put(item, item.getName()); } System.out.println(map.get(new Person("ajing", 20))); } }

执行结果:

null

注意: 做为key的对象必须正确覆写equals和hashCode, 一个类如果覆写了equals, 就必须覆写hashCode

在Person类中在增加如下方法:

@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Person person = (Person) o; return age == person.age && Objects.equals(name, person.name); } @Override public int hashCode() { return Objects.hash(name, age); }

在执行程序, 结果如下:

ajing

2. Properties

prpperties用于读写配置文件xxx.properties

properties文件可以使用ASCII编码

读写Properties时: 仅使用getProperty()/ setProperty()方法

代码实例:

import java.io.*; import java.util.Properties; /** * @ClassName PropertiesTest * @Description TODO * @Author lingxiangxiang * @Date 2:46 PM * @Version 1.0 **/ public class PropertiesTest { public static void main(String[] args) throws IOException { // 写入文件 Properties pro = new Properties(); pro.setProperty("java", "1"); pro.setProperty("c++", "2"); pro.setProperty("python", "3"); Writer w = new FileWriter("name.txt"); pro.store(w, null); w.close(); // 读取文件 Properties p = new Properties(); Reader r = new FileReader("name.txt"); p.load(r); r.close(); System.out.println(p); } }

3. Queue

3.1 Queue

Queue是一个接口, LinkedList实现了Deque, Deque继承了Queue

Queue实现一个先进先出(FIFO)的队列add/offer将元素添加到队尾remove/poll 从队首获取元素并删除element/ peek 从队首获取元素单不删除避免把null添加到队列 public class Person { private final String name; private final int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } @Override public String toString() { return "(Person: " + name + ", " + age + ")"; } } public class QTest { public static void main(String[] args) { Queue<Person> queue = new LinkedList<>(); queue.add(new Person("ajing", 23)); queue.add(new Person("lingjing", 25)); queue.add(new Person("shang", 27)); System.out.println(queue.size()); System.out.println(queue.poll()); System.out.println(queue.size()); System.out.println(queue.peek()); System.out.println(queue.size()); System.out.println(queue.peek()); } }

结果:

3 (Person: ajing, 23) 2 (Person: lingjing, 25) 2 (Person: lingjing, 25)

3.2PriorityQueue

PriorityQueue实现一个优先队列从队首获取元素是, 总是获取优先级最高的元素默认按照元素比较的顺序排序 public class PQTest { public static void main(String[] args) { Queue<Person> queue = new PriorityQueue<>(new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { return o1.getName().compareTo(o2.getName()); } }); queue.add(new Person("ajing", 23)); queue.add(new Person("lingjing", 25)); queue.add(new Person("shang", 27)); System.out.println(queue.poll()); System.out.println(queue.poll()); System.out.println(queue.poll()); } }

结果:

(Person: ajing, 23) (Person: lingjing, 25) (Person: shang, 27)

3.3 Deque

Deque实现一个双端队列(Double Ended Queue)

既可以添加到队尾, 也可以添加到队首既可以从队首获取, 有可以从队尾获取

Queue和Deque的对比

QueueDeque添加元素到队尾add(e)/ offer(e)addLast(e)/ offerLast(e)取队首元素并删除E remove()/ E poll()E removeFirst()/ E pollFirst()取队首元素但不删除E element()/ E peek()E getFirst()/ E peekFirst() Deque添加元素到队首addFirst(e)/ offerFirst(e)取队尾元素并删除E removeLast()/ E pollLast()取队尾元素但不删除E getLast()/ E peekLast()

示例:

import java.util.Deque; import java.util.LinkedList; /** * @ClassName DequeTestMain * @Description TODO * @Author lingxiangxiang * @Date 10:39 AM * @Version 1.0 **/ public class DequeTestMain { public static void main(String[] args) { Deque<String> deque = new LinkedList<>(); deque.offerLast("end"); deque.offerFirst("C"); deque.offerFirst("B"); deque.offerFirst("A"); // 最后的结果为: A, B, C, end System.out.println(deque.pollLast()); System.out.println(deque.pollLast()); System.out.println(deque.pollFirst()); System.out.println(deque.pollFirst()); } }

结果:

end C A B

4. Stack

栈(stack)是一种后进先出(LIFO)的数据结构

操作栈元素的方法:

​ push(E e): 压榨

​ pop(): 出栈

​ peek(): 取栈顶元素但不出栈

java使用Deque实现栈的功能, 注意只调用push/pop/peek, 避免调用其他Deque的其他方法

不用使用遗留类Stack

Deque<Character> stack = new LinkedList<>();

转载于:https://www.cnblogs.com/lingshang/p/10997531.html

最新回复(0)