在Java中防止具有多个字段的重复日期集
我目前正在使用一个程序,该程序每次运行时都会实例化三个数字变量。例如:在Java中防止具有多个字段的重复日期集,java,duplicates,dataset,Java,Duplicates,Dataset,我目前正在使用一个程序,该程序每次运行时都会实例化三个数字变量。例如: (companyID, groupID, hostID) The 1st time when I run it (int companyID = 1; int groupID = 1, int hostID = 1) 2nd time (1, 1, 1) 3rd time (1, 1, 1) 4th (1, 2, 1) 5th (6, 4, 3) ... 是否有一种方法可以在每次运行程序时将三个数字保存在数据集
(companyID, groupID, hostID)
The 1st time when I run it
(int companyID = 1; int groupID = 1, int hostID = 1)
2nd time
(1, 1, 1)
3rd time
(1, 1, 1)
4th
(1, 2, 1)
5th
(6, 4, 3)
...
是否有一种方法可以在每次运行程序时将三个数字保存在数据集中,同时防止相同顺序的重复数字存储在数据集中?在上面的示例中,仅存储1,1,1的一个实例
因此,数据集中存储的最终结果仅为(1,1,1)、(1,2,1)、(6,4,3)
谢谢。试试这个:
代码
import java.util.ArrayList;
import java.util.List;
public class DatasetUnique {
public static void main(String args[]){
// Data to insert
int[] one = {1,2,3};
int[] two = {2,3,4};
int[] three = {1,2,3};
int[] four = {7,8,9};
int[] five = {2,3,4};
// Data inserted
List<dataSet> allContent = new ArrayList<>();
// 1 - Insert data
insert(new dataSet(one), allContent);
insert(new dataSet(two), allContent);
insert(new dataSet(three), allContent);
insert(new dataSet(four), allContent);
insert(new dataSet(five), allContent);
// 2 - Print data
System.out.println(allContent.toString());
}
// Insert a dataset inside list if doesnt exists
private static void insert(dataSet dataSetToInsert, List<dataSet> allContent){
if(allContent.size() == 0){
allContent.add(dataSetToInsert);
}else{
for(dataSet ds: allContent){
if(dataSetToInsert.equals(ds)){
return;
}
}
allContent.add(dataSetToInsert);
}
}
// Class for each dataSet
private static class dataSet{
int firstNumber;
int secondNumber;
int thirdNumber;
public dataSet(int[] dataSet) {
super();
this.firstNumber = dataSet[0];
this.secondNumber = dataSet[1];
this.thirdNumber = dataSet[2];
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
dataSet other = (dataSet) obj;
if (firstNumber != other.firstNumber)
return false;
if (secondNumber != other.secondNumber)
return false;
if (thirdNumber != other.thirdNumber)
return false;
return true;
}
@Override
public String toString() {
return "\n" + firstNumber + " " + secondNumber + " "+ thirdNumber + "\n";
}
}
}
试试这个:
代码
import java.util.ArrayList;
import java.util.List;
public class DatasetUnique {
public static void main(String args[]){
// Data to insert
int[] one = {1,2,3};
int[] two = {2,3,4};
int[] three = {1,2,3};
int[] four = {7,8,9};
int[] five = {2,3,4};
// Data inserted
List<dataSet> allContent = new ArrayList<>();
// 1 - Insert data
insert(new dataSet(one), allContent);
insert(new dataSet(two), allContent);
insert(new dataSet(three), allContent);
insert(new dataSet(four), allContent);
insert(new dataSet(five), allContent);
// 2 - Print data
System.out.println(allContent.toString());
}
// Insert a dataset inside list if doesnt exists
private static void insert(dataSet dataSetToInsert, List<dataSet> allContent){
if(allContent.size() == 0){
allContent.add(dataSetToInsert);
}else{
for(dataSet ds: allContent){
if(dataSetToInsert.equals(ds)){
return;
}
}
allContent.add(dataSetToInsert);
}
}
// Class for each dataSet
private static class dataSet{
int firstNumber;
int secondNumber;
int thirdNumber;
public dataSet(int[] dataSet) {
super();
this.firstNumber = dataSet[0];
this.secondNumber = dataSet[1];
this.thirdNumber = dataSet[2];
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
dataSet other = (dataSet) obj;
if (firstNumber != other.firstNumber)
return false;
if (secondNumber != other.secondNumber)
return false;
if (thirdNumber != other.thirdNumber)
return false;
return true;
}
@Override
public String toString() {
return "\n" + firstNumber + " " + secondNumber + " "+ thirdNumber + "\n";
}
}
}
您将需要一些包装类。您可以存储3个整数,也可以存储一个整数数组,这取决于您。 您需要实现接口。
如果您将使用Set类来完成此任务,请注意这不会起作用,因为它不是基于自然排序的。威尔。 下面是您的类的简单代码示例。
当然,这不包括将类存储在文件中的部分,但允许您在工作时管理重复项。然后您可以将集合中的所有元素写入文件,它们将是唯一的。并在开始时将元素从文件加载到集合
import java.util.Set;
import java.util.TreeSet;
public class Test {
public static final class Tuple implements Comparable<Tuple> {
public int x1;
public int x2;
public int x3;
public Tuple(int x1, int x2, int x3) {
this.x1 = x1;
this.x2 = x2;
this.x3 = x3;
}
/**
* You don't need equal method to work with sets (and this is not even overriding basic equals method,
* but it's just an example of how you can easily compare your tuples
*/
public boolean equals(Tuple obj) {
return this.x1 == obj.x1 && this.x2 == obj.x2 && this.x3 == obj.x3;
}
/**
* Check javadoc for this, but it's just a way to tell if one tuple is more, equal or less then another
*/
@Override
public int compareTo(Tuple o) {
if (o.x1 != this.x1) {
return new Integer(o.x1).compareTo(this.x1);
}
if (o.x2 != this.x2) {
return new Integer(o.x2).compareTo(this.x2);
}
return new Integer(o.x3).compareTo(this.x3);
}
@Override
public String toString() {
return "[" + x1 + " " + x2 + " " + x3 + "]";
}
}
public static void main(String[] args) {
/**
* Note, that HashSet won't work because it doesn't depend on natural ordering of elements.
* TreeSet does, and it will work fine for your purpose.
*/
Set<Tuple> tuples = new TreeSet<>();
tuples.add(new Tuple(1, 1, 1));
tuples.add(new Tuple(1, 2, 3));
tuples.add(new Tuple(1, 1, 1));
tuples.add(new Tuple(2, 3, 4));
tuples.add(new Tuple(2, 3, 4));
tuples.add(new Tuple(1, 2, 3));
for (Tuple tuple: tuples) {
System.out.println(tuple);
}
}
}`
集合中没有放置重复项。您将需要一些包装类。您可以存储3个整数,也可以存储一个整数数组,这取决于您。 您需要实现接口。
如果您将使用Set类来完成此任务,请注意这不会起作用,因为它不是基于自然排序的。威尔。 下面是您的类的简单代码示例。
当然,这不包括将类存储在文件中的部分,但允许您在工作时管理重复项。然后您可以将集合中的所有元素写入文件,它们将是唯一的。并在开始时将元素从文件加载到集合
import java.util.Set;
import java.util.TreeSet;
public class Test {
public static final class Tuple implements Comparable<Tuple> {
public int x1;
public int x2;
public int x3;
public Tuple(int x1, int x2, int x3) {
this.x1 = x1;
this.x2 = x2;
this.x3 = x3;
}
/**
* You don't need equal method to work with sets (and this is not even overriding basic equals method,
* but it's just an example of how you can easily compare your tuples
*/
public boolean equals(Tuple obj) {
return this.x1 == obj.x1 && this.x2 == obj.x2 && this.x3 == obj.x3;
}
/**
* Check javadoc for this, but it's just a way to tell if one tuple is more, equal or less then another
*/
@Override
public int compareTo(Tuple o) {
if (o.x1 != this.x1) {
return new Integer(o.x1).compareTo(this.x1);
}
if (o.x2 != this.x2) {
return new Integer(o.x2).compareTo(this.x2);
}
return new Integer(o.x3).compareTo(this.x3);
}
@Override
public String toString() {
return "[" + x1 + " " + x2 + " " + x3 + "]";
}
}
public static void main(String[] args) {
/**
* Note, that HashSet won't work because it doesn't depend on natural ordering of elements.
* TreeSet does, and it will work fine for your purpose.
*/
Set<Tuple> tuples = new TreeSet<>();
tuples.add(new Tuple(1, 1, 1));
tuples.add(new Tuple(1, 2, 3));
tuples.add(new Tuple(1, 1, 1));
tuples.add(new Tuple(2, 3, 4));
tuples.add(new Tuple(2, 3, 4));
tuples.add(new Tuple(1, 2, 3));
for (Tuple tuple: tuples) {
System.out.println(tuple);
}
}
}`
未在集合中放置重复项。最简单的方法是将这些ID写入一个文件,您的程序将在启动时读取该文件。当程序即将结束时,它只将数据集的所有内容写入该文件。这样你就有了一个非常基本的数据库,它只存储在一个文件中。@RAZ_Muh_Taz谢谢。但即使在这种情况下,我如何确保没有重复写入。有没有一种方法可以轻松跟踪三个数字的组合?您可以将元组存储在一个类中(该类只包含3个元素)。重写#比较,或者使用您自己的方法比较两个类。然后按照@RAZ_Muh_Taz的建议将它们存储在星星之间的文件中。在程序中,您可以将它们存储在任何结构中,例如列表。此外,您还可以实现接口并使用任何集合(如LinkedHashSet)。通过这种方式,您可以使用set#contains轻松查看该元素是否已在集合中method@mr.Tropez谢谢你的澄清。基于第二种方法,我是否只需要将它们存储在int数组int[1,1,1]中,然后将该数组添加到Set中?@000000000000000,不,您仍然需要使用一些包装器类来进行设置。我在一个答案中发布了一个set用法的代码示例。最简单的方法是将这些id写入一个文件,您的程序将在启动时读取该文件。当程序即将结束时,它只将数据集的所有内容写入该文件。这样你就有了一个非常基本的数据库,它只存储在一个文件中。@RAZ_Muh_Taz谢谢。但即使在这种情况下,我如何确保没有重复写入。有没有一种方法可以轻松跟踪三个数字的组合?您可以将元组存储在一个类中(该类只包含3个元素)。重写#比较,或者使用您自己的方法比较两个类。然后按照@RAZ_Muh_Taz的建议将它们存储在星星之间的文件中。在程序中,您可以将它们存储在任何结构中,例如列表。此外,您还可以实现接口并使用任何集合(如LinkedHashSet)。通过这种方式,您可以使用set#contains轻松查看该元素是否已在集合中method@mr.Tropez谢谢你的澄清。基于第二种方法,我是否只需要将它们存储在int数组int[1,1,1]中,然后将该数组添加到Set中?@000000000000000,不,您仍然需要使用一些包装器类来进行设置。我在回答中发布了一个集合用法的代码示例。