1 需求
使用TreeSet集合存储多个学生信息(姓名,语文成绩,数学成绩),并遍历该集合
要求:按照总分从高到底出现
思路:
1、定义学生类
2、创建TreeSet集合对象,通过比较器排序进行排序
3、创建学生对象
4、把学生对象添加到集合
5、遍历集合
实战演练
Student.java
package TreeSet2;
public class Student {
private String name;
private int chinese;
private int math;
public Student() {
}
public Student(String name, int chinese, int math) {
this.name = name;
this.chinese = chinese;
this.math = math;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getSum() {
return this.chinese + this.getMath();
}
}
TreeSetDemo.java
package TreeSet2;
import sun.reflect.generics.tree.Tree;
import java.util.Comparator;
import java.util.TreeSet;
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s2.getSum() - s1.getSum();
int num2 = num == 0 ? s1.getChinese() - s2.getChinese() : num;
int num3 = num2 == 0 ? s1.getName().compareTo(s2.getName()) : num2;
return num3;
}
});
Student s1 = new Student("灰太狼", 100, 92);
Student s2 = new Student("喜洋洋", 92, 99);
Student s3 = new Student("有勇气的牛排", 99, 100);
Student s4 = new Student("导演", 96, 97);
Student s5 = new Student("懒洋洋", 96, 96);
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
for (Student s : ts) {
System.out.println(s.getName() + "," + s.getChinese() + "," + s.getMath() + "," + s.getSum());
}
}
}
<h2><a id="1__0"></a>1 需求</h2>
<p>使用TreeSet集合存储多个学生信息(姓名,语文成绩,数学成绩),并遍历该集合</p>
<p>要求:按照总分从高到底出现</p>
<p>思路:</p>
<p>1、定义学生类</p>
<p>2、创建TreeSet集合对象,通过比较器排序进行排序</p>
<p>3、创建学生对象</p>
<p>4、把学生对象添加到集合</p>
<p>5、遍历集合</p>
<h2><a id="_18"></a>实战演练</h2>
<p>Student.java</p>
<pre><div class="hljs"><code class="lang-java"><span class="hljs-keyword">package</span> TreeSet2;
<span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Student</span> {
<span class="hljs-keyword">private</span> String name;
<span class="hljs-keyword">private</span> <span class="hljs-type">int</span> chinese;
<span class="hljs-keyword">private</span> <span class="hljs-type">int</span> math;
<span class="hljs-keyword">public</span> <span class="hljs-title function_">Student</span><span class="hljs-params">()</span> {
}
<span class="hljs-keyword">public</span> <span class="hljs-title function_">Student</span><span class="hljs-params">(String name, <span class="hljs-type">int</span> chinese, <span class="hljs-type">int</span> math)</span> {
<span class="hljs-built_in">this</span>.name = name;
<span class="hljs-built_in">this</span>.chinese = chinese;
<span class="hljs-built_in">this</span>.math = math;
}
<span class="hljs-keyword">public</span> String <span class="hljs-title function_">getName</span><span class="hljs-params">()</span> {
<span class="hljs-keyword">return</span> name;
}
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setName</span><span class="hljs-params">(String name)</span> {
<span class="hljs-built_in">this</span>.name = name;
}
<span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">getChinese</span><span class="hljs-params">()</span> {
<span class="hljs-keyword">return</span> chinese;
}
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setChinese</span><span class="hljs-params">(<span class="hljs-type">int</span> chinese)</span> {
<span class="hljs-built_in">this</span>.chinese = chinese;
}
<span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">getMath</span><span class="hljs-params">()</span> {
<span class="hljs-keyword">return</span> math;
}
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setMath</span><span class="hljs-params">(<span class="hljs-type">int</span> math)</span> {
<span class="hljs-built_in">this</span>.math = math;
}
<span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">getSum</span><span class="hljs-params">()</span> {
<span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.chinese + <span class="hljs-built_in">this</span>.getMath();
}
}
</code></div></pre>
<p>TreeSetDemo.java</p>
<pre><div class="hljs"><code class="lang-java"><span class="hljs-keyword">package</span> TreeSet2;
<span class="hljs-keyword">import</span> sun.reflect.generics.tree.Tree;
<span class="hljs-keyword">import</span> java.util.Comparator;
<span class="hljs-keyword">import</span> java.util.TreeSet;
<span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">TreeSetDemo</span> {
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> {
<span class="hljs-comment">// 创建Tree集合对象,通过比较器排序进行排序</span>
TreeSet<Student> ts = <span class="hljs-keyword">new</span> <span class="hljs-title class_">TreeSet</span><>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Comparator</span><Student>() {
<span class="hljs-meta">@Override</span>
<span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">compare</span><span class="hljs-params">(Student s1, Student s2)</span> {
<span class="hljs-comment">// int num = s2.getMath()+s2.getChinese()-s1.getMath()-s1.getChinese();</span>
<span class="hljs-comment">// 总成绩</span>
<span class="hljs-type">int</span> <span class="hljs-variable">num</span> <span class="hljs-operator">=</span> s2.getSum() - s1.getSum();
<span class="hljs-comment">// 语文成绩升序</span>
<span class="hljs-type">int</span> <span class="hljs-variable">num2</span> <span class="hljs-operator">=</span> num == <span class="hljs-number">0</span> ? s1.getChinese() - s2.getChinese() : num;
<span class="hljs-comment">// 姓名不一样</span>
<span class="hljs-type">int</span> <span class="hljs-variable">num3</span> <span class="hljs-operator">=</span> num2 == <span class="hljs-number">0</span> ? s1.getName().compareTo(s2.getName()) : num2;
<span class="hljs-keyword">return</span> num3;
}
});
<span class="hljs-comment">// 创建学生对象</span>
<span class="hljs-type">Student</span> <span class="hljs-variable">s1</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Student</span>(<span class="hljs-string">"灰太狼"</span>, <span class="hljs-number">100</span>, <span class="hljs-number">92</span>);
<span class="hljs-type">Student</span> <span class="hljs-variable">s2</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Student</span>(<span class="hljs-string">"喜洋洋"</span>, <span class="hljs-number">92</span>, <span class="hljs-number">99</span>);
<span class="hljs-type">Student</span> <span class="hljs-variable">s3</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Student</span>(<span class="hljs-string">"有勇气的牛排"</span>, <span class="hljs-number">99</span>, <span class="hljs-number">100</span>);
<span class="hljs-type">Student</span> <span class="hljs-variable">s4</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Student</span>(<span class="hljs-string">"导演"</span>, <span class="hljs-number">96</span>, <span class="hljs-number">97</span>);
<span class="hljs-type">Student</span> <span class="hljs-variable">s5</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Student</span>(<span class="hljs-string">"懒洋洋"</span>, <span class="hljs-number">96</span>, <span class="hljs-number">96</span>);
<span class="hljs-comment">// 把学生添加到集合</span>
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
<span class="hljs-comment">// 遍历集合</span>
<span class="hljs-keyword">for</span> (Student s : ts) {
System.out.println(s.getName() + <span class="hljs-string">","</span> + s.getChinese() + <span class="hljs-string">","</span> + s.getMath() + <span class="hljs-string">","</span> + s.getSum());
}
}
}
</code></div></pre>
留言