求大神指导如何用JAVA计算出一个List里的相似信息
发布网友
发布时间:2022-05-15 22:27
我来回答
共4个回答
热心网友
时间:2023-08-02 07:39
这个相似是要求相似度是多少?建议去重写TestMessage类的equals和hash方法。
package com;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
class TestMessage{
private int messageId;
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getMessageId() {
return messageId;
}
public void setMessageId(int messageId) {
this.messageId = messageId;
}
@Override
public int hashCode() {
if(null == this.message){
return super.hashCode();
}else{
return this.message.hashCode();
}
}
@Override
public boolean equals(Object obj) {
if(null == obj){
return false;
}else if(obj instanceof TestMessage){
TestMessage tempObj = (TestMessage)obj;
return StringUtils.equalsIgnoreCase(message, tempObj.getMessage());
}else{
return false;
}
}
}
public class Test {
public static void main(String[] args) {
List<TestMessage> list1 = new ArrayList<TestMessage>();
TestMessage yq1 = new TestMessage();
yq1.setMessageId(11);
yq1.setMessage("this is a test message");
TestMessage yq2 = new TestMessage();
yq2.setMessageId(12);
yq2.setMessage("this is a test message");
TestMessage yq3 = new TestMessage();
yq3.setMessageId(13);
yq3.setMessage("hello world");
TestMessage yq4 = new TestMessage();
yq4.setMessageId(14);
yq4.setMessage("this is a test message");
list1.add(yq1);
list1.add(yq2);
list1.add(yq3);
list1.add(yq4);
Map<TestMessage,List<TestMessage>> data = new HashMap<TestMessage,List<TestMessage>>();
for(int i=0;i<list1.size();i++){
TestMessage key = list1.get(i);
if(!data.containsKey(key)){
data.put(key, new ArrayList<TestMessage>());
}else{
continue;
}
List<TestMessage> temp = data.get(key);
for(int j=i+1;j<list1.size();j++){
TestMessage value = list1.get(j);
if(temp.contains(value)){
continue;
}else{
temp.add(value);
}
}
}
System.out.println("OK");
}
}
热心网友
时间:2023-08-02 07:39
你加个if判断map集合的value集合中是否含有message值,当message首次出现时,就先加到map集合中,key为首次出现message的TestMessage对象,value为空;
当message已存在时,则在对应的map值集合中添加此message值
热心网友
时间:2023-08-02 07:40
package com.www;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestMessage {
private int messageId;
private String message;
public int getMessageId() {
return messageId;
}
public void setMessageId(int messageId) {
this.messageId = messageId;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public boolean sameMessage(TestMessage test){
if(this == null){
return test == null;
}
if(test != null){
if(test.getMessage() != null){
return test.getMessage().equals(this.message);
}else if(this.message == null){
return true;
}
}
return false;
}
public static void main(String[] args) {
List<TestMessage> list1 = new ArrayList<TestMessage>();
TestMessage yq1 = new TestMessage();
yq1.setMessageId(11);
yq1.setMessage("this is a test message");
TestMessage yq2 = new TestMessage();
yq2.setMessageId(12);
yq2.setMessage("this is a test message");
TestMessage yq3 = new TestMessage();
yq3.setMessageId(13);
yq3.setMessage("hello world");
TestMessage yq4 = new TestMessage();
yq4.setMessageId(14);
yq4.setMessage("this is a test message");
list1.add(yq1);
list1.add(yq2);
list1.add(yq3);
list1.add(yq4);
Map<TestMessage, List<TestMessage>> map = new HashMap<TestMessage, List<TestMessage>>();
for(TestMessage t : list1){
boolean has = false;
for(TestMessage m : map.keySet()){
if(m.sameMessage(t)){
map.get(m).add(t);
has = true;
break;
}
}
if(!has){
map.put(t, new ArrayList<TestMessage>());
}
}
}
}
热心网友
时间:2023-08-02 07:40
根据什么分组呢 你自己看懂自己写什么吗?追问根据message相同的分组