集合框架
为什么使用集合框架
Java集合框架包含的内容
集合框架的接口
List接口的实现类
ArrayList集合类
List接口常用方法
LinkedList集合类
迭代器Iterator
Set&HashSet
HashMap
Collections主要方法
基础例子
MAP TEST
新闻 NEWS
package New;
public class lllxxx implements Comparable {
private int id;
private String title;
private String content;
private String hh;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getHh() {
return hh;
}
public void setHh(String hh) {
this.hh = hh;
}
public lllxxx() {
super();
// TODO Auto-generated constructor stub
}
public lllxxx(int id, String title, String content, String hh) {
super();
this.id = id;
this.title = title;
this.content = content;
this.hh = hh;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
lllxxx other = (lllxxx) obj;
if (id != other.id)
return false;
return true;
}
@Override
public int compareTo(Object obj) {
// TODO Auto-generated method stub
News news=(News)obj;
if(this.id>news.getId()){
return 1;
}else if(this.id==news.getId()){
return 0;}
else {
return -1;}
}
}
package New;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class lx {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
lllxxx news=new lllxxx( 4,"体育比赛","斯诺克d","2016-1-1");
lllxxx news1=new lllxxx(1,"体育比赛","斯诺克w","2016-1-1");
lllxxx news2=new lllxxx(3,"体育比3","斯诺克","2016-1-1");
lllxxx news3=new lllxxx(3,"体育比2","斯诺克3","2016-1-1");
Set set=new HashSet();
set.add(news);
set.add(news1);
set.add(news2);
set.add(news3);
Iterator it=set.iterator();
while (it.hasNext()) {
lllxxx ne=(lllxxx)it.next();
String s=ne.getId()+"";
s+=ne.getTitle();
s+=ne.getContent();
s+=ne.getHh();
System.out.println(s);
}
}
}
package New;
public class News implements Comparable {
private int id;
private String title;
private String content;
private String hh;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getHh() {
return hh;
}
public void setHh(String hh) {
this.hh = hh;
}
public News(int id, String title, String content, String hh) {
super();
this.id = id;
this.title = title;
this.content = content;
this.hh = hh;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((title == null) ? 0 : title.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
News other = (News) obj;
if (id != other.id)
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
return true;
}
public News() {
// TODO Auto-generated constructor stub
}
@Override
public int compareTo(Object obj) {
// TODO Auto-generated method stub
News news=(News)obj;
if(this.id>news.getId()){
return 1;
}else if(this.id==news.getId()){
return 0;} //+1-1 互换倒序
else {
return -1;}
}
}
package New;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class SetEntityTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
News news=new News( 1,"体育比赛11","斯诺克d","2016-1-1");
News news1=new News(3,"体育比赛44","斯诺克w4","2016-1-1");
News news2=new News(5,"体育比33","斯诺克","2016-1-1");
News news3=new News(3,"体育比44444","斯诺克4444","2016-1-1");
Set set=new HashSet();
set.add(news);
set.add(news1);
set.add(news2);
set.add(news3);
Iterator it=set.iterator();
while (it.hasNext()) {
News ne=(News)it.next();
String s=ne.getId()+"";
s+=ne.getTitle();
s+=ne.getContent();
s+=ne.getHh();
System.out.println(s);
}
}
}
作者:冲天之峰 20160601