简体   繁体   English

列表到哈希表

[英]List to Hashset

So my problem is that I dont want to have repeated names in the list of objects. 所以我的问题是我不想在对象列表中有重复的名称。

So I tried to convert the list to hashset but it still has rows with the same name. 因此,我尝试将list转换为hashset但它仍然具有相同名称的行。

Basically I only want to get 1 row of Flow2 and another of TestFlow NOT 4. http://prntscr.com/hylkoc 基本上,我只是想参加中级课程的第1行和另一TestFlow不是4的http://prntscr.com/hylkoc

public static Set<TestFlow> getFlows() {
    SessionFactory factory = HibernateUtil.GetSessionFactory();
    Session hibernateSession = factory.openSession();
    Transaction tx = null;
    List<TestFlow> testFlows;
    Set<TestFlow> testFlowSet = new HashSet<>();
    try {
        tx = hibernateSession.beginTransaction();
        testFlows = hibernateSession.createQuery("FROM TestFlow").list();
        testFlowSet = new HashSet<>(testFlows);
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null) tx.rollback();
        e.printStackTrace();
    } finally {
        hibernateSession.close();
    }
    return testFlowSet;
}

TestFlow is an object, so a record having the same name will not mean it equals the other object. TestFlow是一个对象,因此具有相同名称的记录并不意味着它等于另一个对象。

You can use a Map instead: 您可以改用Map:

Map<String, TestFlow>

The key will be the name, and the value will be the TestFlow record. 关键字将是名称,而值将是TestFlow记录。 That way if the name exists, you will not add the record to the map. 这样,如果名称存在,就不会将记录添加到地图中。

Another option is overriding the equals method, although if you use the object elsewhere and it has a bunch of fields, that may not be a good idea. 另一个选择是覆盖equals方法,尽管如果在其他地方使用该对象并且它具有许多字段,那么这可能不是一个好主意。

You can use TreeSet + Comparator : 您可以使用TreeSet + Comparator:

Comparator: 比较:

public class TestFlowComparator implements Comparator<TestFlow> {
  @Override
  public int compare(TestFlow o1, TestFlow o2) {
    return o1.getName().compareTo(o2.getName());
  }
}

Test : 测试:

@Test
@DisplayName("stack")
public void srack(){
    List<TestFlow> listTestFlow = new ArrayList<>();
    listTestFlow.add(new TestFlow("a"));
    listTestFlow.add(new TestFlow("a"));
    listTestFlow.add(new TestFlow("a"));
    listTestFlow.add(new TestFlow("a"));
    listTestFlow.add(new TestFlow("ъ"));
    listTestFlow.add(new TestFlow("ъ"));

    Set<TestFlow> set = new TreeSet<>(new TestFlowComparator());
    set.addAll(listTestFlow);
    System.out.println(set.toString());
}

sout : [TestFlow{name = a } , TestFlow{name = ъ } ] sout :[TestFlow {name = a},TestFlow {name =ъ}]

Inside TestFlow class override the two methods equals() and hashCode() . TestFlow类内部,重写了equals()hashCode()这两个方法。

For example if TestFlow class have int id property you can do that: 例如,如果TestFlow类具有int id属性,则可以执行以下操作:

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj instanceof TestFlow) {
        TestFlow tf = (TestFlow) obj;
        return tf.id == this.id;
    }
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM