当前位置:网站首页>Set code exercise
Set code exercise
2022-06-27 21:39:00 【continueLR】
Catalog
List Set code :ArrayList And LinkedList
package com.java.jiheWork;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
/*
1.1、 The creation of each collection object (new)
1.2、 Add elements to the collection
1.3、 Take an element out of a collection
1.4、 Ergodic set
*/
public class ArrayListTest {
public static void main(String[] args) {
// Create a collection object
ArrayList<String> list = new ArrayList<>();
// Additive elements
list.add(" Zhang San ");
list.add(" Li Si ");
list.add(" Wang Wu ");
// Take an element out of a collection
// List Sets have subscripts
String firstElt = list.get(0);
System.out.println(firstElt);
System.out.println(list.get(2));
System.out.println("-------------------------------");
// Traverse ( Subscript mode )
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println("-------------------------------");
// Traverse ( Iterator mode , This one is universal , all Collection Can be used )
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
System.out.println("-------------------------------");
// while Change the loop to for loop
/*for(Iterator<String> it2 = list.iterator(); it2.hasNext(); ){
System.out.println("====>" + it2.next());
}*/
// Traverse (foreach The way )
for (String s : list) {
System.out.println(s);
}
System.out.println("-------------------------------");
LinkedList<Student> students = new LinkedList<Student>();
Student S1 = new Student(111," Zhang San ");
Student S2 = new Student(122," Li Si ");
Student S3 = new Student(123," Wang Wu ");
students.add(S1);
students.add(S2);
students.add(S3);
Iterator<Student> it2 = students.iterator();
while (it2.hasNext()){
System.out.println(it2.next());
}
System.out.println("-------------------------------");
for (Student ST:students) {
System.out.println(ST);
}
}
}
test result 
summary :ArrayList At the bottom of the collection is the array , You can find data directly by subscript , Easy to find and modify . But when you add or delete elements, you need to move other elements , Low efficiency .
LinkedList At the bottom of the collection is the linked list , There is no need to move elements when adding or deleting , Easy to add and delete . But every time you search, you have to go through the previous data , It's not easy to find .
HashSet Code
package com.bjpowernode.javase.review;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;
/*
1.1、 The creation of each collection object (new)
1.2、 Add elements to the collection
1.3、 Take an element out of a collection
1.4、 Ergodic set
1.5、 test HashSet Set characteristics : Disorder cannot be repeated .
*/
public class HashSetTest {
public static void main(String[] args) {
// Create a collection object
HashSet<String> set = new HashSet<>();
// Additive elements
set.add("abc");
set.add("def");
set.add("king");
set.add("abc");
System.out.println(set.size());//3
// Here we find set The number of elements is only 3 individual , Because duplicate elements cannot be added to set In the collection
System.out.println("------------------------");
// Traversal methods cannot use ordinary for loop , because set Set has no subscript
// Traversal methods 1: Iterator mode
Iterator<String> it = set.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
System.out.println("------------------------");
// Traversal methods 2:foreach The way
for (String s:set) {
System.out.println(s);
}
System.out.println("------------------------");
// establish Student Class collection objects
HashSet<Student> st = new HashSet<>();
// establish Student object , among s3 And s1 repeat
Student s1 = new Student(111," Zhang San ");
Student s2 = new Student(222," Li Si ");
Student s3 = new Student(111," Zhang San ");
st.add(s1);
st.add(s2);
st.add(s3);
Iterator<Student> Stu = st.iterator();
while (Stu.hasNext()){
System.out.println(Stu.next());
}
System.out.println("------------------------");
for (Student stu:st) {
System.out.println(stu);
}
}
}
class Student {
int no;
String name;
public Student() {
}
public Student(int no, String name) {
this.no = no;
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return no == student.no &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(no, name);
}
}
HashSet Set characteristics : The bottom layer is a hash table , Disorder cannot be repeated . Duplicate elements cannot be added to the collection .
TreeSet Code
package com.bjpowernode.javase.review;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
/*
1.1、 The creation of each collection object (new)
1.2、 Add elements to the collection
1.3、 Take an element out of a collection
1.4、 Ergodic set
1.5、 test TreeSet The elements in a collection are sortable .
1.6、 test TreeSet The type stored in the collection is custom .
1.7、 Test implementation Comparable How to interface
1.8、 Test implementation Comparator How to interface ( It's best to test the following anonymous inner classes )
*/
public class TreeSetTest {
public static void main(String[] args) {
// Set creation ( You can test the following TreeSet Store... In a collection String、Integer Of . These classes are SUN Written .)
//TreeSet<Integer> ts = new TreeSet<>();
// Writing a comparator can change the rules .
TreeSet<Integer> ts = new TreeSet<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1; // Automatic dismantling
}
});
// Additive elements
ts.add(1);
ts.add(100);
ts.add(10);
ts.add(10);
ts.add(10);
ts.add(10);
ts.add(0);
// Traverse ( Iterator mode )
Iterator<Integer> it = ts.iterator();
while(it.hasNext()) {
Integer i = it.next();
System.out.println(i);
}
// Traverse (foreach)
for(Integer x : ts){
System.out.println(x);
}
// TreeSet Collection to store custom types
TreeSet<A> atree = new TreeSet<>();
atree.add(new A(100));
atree.add(new A(200));
atree.add(new A(500));
atree.add(new A(300));
atree.add(new A(400));
atree.add(new A(1000));
// Traverse
for(A a : atree){
System.out.println(a);
}
//TreeSet<B> btree = new TreeSet<>(new BComparator());
// Anonymous inner class mode .
TreeSet<B> btree = new TreeSet<>(new Comparator<B>() {
@Override
public int compare(B o1, B o2) {
return o1.i - o2.i;
}
});
btree.add(new B(500));
btree.add(new B(100));
btree.add(new B(200));
btree.add(new B(600));
btree.add(new B(300));
btree.add(new B(50));
for(B b : btree){
System.out.println(b);
}
}
}
// The first way : Realization Comparable Interface
class A implements Comparable<A>{
int i;
public A(int i){
this.i = i;
}
@Override
public String toString() {
return "A{" +
"i=" + i +
'}';
}
@Override
public int compareTo(A o) {
//return this.i - o.i;
return o.i - this.i;
}
}
class B {
int i;
public B(int i){
this.i = i;
}
@Override
public String toString() {
return "B{" +
"i=" + i +
'}';
}
}
// The comparator
class BComparator implements Comparator<B> {
@Override
public int compare(B o1, B o2) {
return o1.i - o2.i;
}
}HashMap Code
package com.bjpowernode.javase.review;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/*
1.1、 The creation of each collection object (new)
1.2、 Add elements to the collection
1.3、 Take an element out of a collection
1.4、 Ergodic set
*/
public class HashMapTest {
public static void main(String[] args) {
// establish Map aggregate
Map<Integer, String> map = new HashMap<>();
// Additive elements
map.put(1, "zhangsan");
map.put(9, "lisi");
map.put(10, "wangwu");
map.put(2, "king");
map.put(2, "simth"); // key repeat value Will be covered .
// Get the number of elements
System.out.println(map.size());
// take key yes 2 The elements of
System.out.println(map.get(2)); // smith
// Traverse Map Assembly is important , You have to be able to .
// The first way : Get all of it first key, Traverse key When , adopt key obtain value
Set<Integer> keys = map.keySet();
for(Integer key : keys){
System.out.println(key + "=" + map.get(key));
}
// The second way : Yes, it will Map Set to Set aggregate ,Set Each element in the set is Node
// This Node Node has key and value
Set<Map.Entry<Integer,String>> nodes = map.entrySet();
for(Map.Entry<Integer,String> node : nodes){
System.out.println(node.getKey() + "=" + node.getValue());
}
}
}
Properties Code
package com.bjpowernode.javase.review;
import java.util.Properties;
public class PropertiesTest {
public static void main(String[] args) {
// Create objects
Properties pro = new Properties();
// save
pro.setProperty("username", "test");
pro.setProperty("password", "test123");
// take
String username = pro.getProperty("username");
String password = pro.getProperty("password");
System.out.println(username);
System.out.println(password);
}
}
Set inheritance structure diagram

边栏推荐
- 专题教程——选队长游戏
- 让马化腾失望了!Web3.0,毫无希望
- VMware vSphere esxi 7.0 installation tutorial
- After being forced to develop the app within 20 days, the group was laid off, and the technical director angrily criticized it: I wish "closure as soon as possible!"
- Acwing周赛57-最长连续子序列-(二分or树状数组)
- squid代理服務器
- Go从入门到实战——仅执行一次(笔记)
- Scrum和看板的区别
- Use the storcli tool to configure raid. Just collect this article
- Go from entry to practice - multiple selection and timeout control (notes)
猜你喜欢

Go从入门到实战——行为的定义和实现(笔记)

DO280OpenShift访问控制--security policy和章节实验

Go从入门到实战——Context与任务取消(笔记)

Go from entry to practice - multiple selection and timeout control (notes)

Go从入门到实战——错误机制(笔记)

释放开源数据库创新力量 | 【甘肃】openGauss Meetup圆满结束

Galaxy Kirin system LAN file sharing tutorial

Go从入门到实战——package(笔记)

After being forced to develop the app within 20 days, the group was laid off, and the technical director angrily criticized it: I wish "closure as soon as possible!"

Go从入门到实战——共享内存并发机制(笔记)
随机推荐
A set of system to reduce 10 times the traffic pressure in crowded areas
Go从入门到实战—— 多路选择和超时控制(笔记)
Golang 使用正则来匹配出子字符串函数
oss上传调用的是哪个方法
Flask----应用案例
于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
Icml2022 | scalable depth Gaussian Markov random field
linux下安装oracle11g 静默安装教程
Go from entry to practice - dependency management (notes)
Codeforces Round #716 (Div. 2)
图解基于AQS队列实现的CountDownLatch和CyclicBarrier
Use the storcli tool to configure raid. Just collect this article
The difference between scrum and Kanban
Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
OpenSSL 编程 一:基本概念
Full record of 2022 open source moment at Huawei partners and Developers Conference
Save method of JPA stepping pit series
KDD 2022 | graph neural network generalization framework under the paradigm of "pre training, prompting and fine tuning"
squid代理服務器
快速excel导出