1

Assume I have this arrayList :

ArrayList<Personne> personnes = new ArrayList<Personne>();
 Personne p1 = new Personne("001590842","51862499", "N5+", "1", "20170201","0");
 Personne p2 = new Personne("001590842","51862499", "X0", "1", "20150529", "1");
 Personne p3 = new Personne("001639055","51862517", "G3", "1", "20170201", "2");
 Personne p4 = new Personne("001639055","51862517", "G3", "1", "20170201", "2");
 Personne p5 = new Personne("001597135","51862517", "G3", "1", "20170201", "2");
 Personne p6 = new Personne("001597135","51862517", "G3", "1", "20170201", "2");
 Personne p7 = new Personne("002804935","00006178","G4","1","19870101","1");
 Personne p8 = new Personne("002804935","00009118","X0","1","19861201","1");
 Personne p9 = new Personne("002804935","00009957","N4+","1","19861229","1");
 Personne p10 = new Personne("002804935","00012970","B3++","1","20100227","1");
 personnes.add(p1);
 personnes.add(p2);
 personnes.add(p3);
 personnes.add(p4);
 personnes.add(p5);
 personnes.add(p6);
 personnes.add(p7);
 personnes.add(p8);
 personnes.add(p9);
 personnes.add(p10);

Then I want to get only distinct values of the first column. So the nedeed result should be :

001590842
001639055
002804935

Note that my Personne class is defined as :

public class Personne {
 private String IDT_GCT;
 private String IDC_PSE_PCL;
 private String IDC_CD_NOT;
 private String DA_PRM_CTR_ORDER;
 private String IDT_ETT_PSE;
 private String CD_NOT;
 public Personne(String IDT_GCT, String IDC_PSE_PCL, String IDC_CD_NOT,
 String DA_PRM_CTR_ORDER, String IDT_ETT_PSE, String CD_NOT) {
 this.IDT_GCT = IDT_GCT;
 this.IDC_PSE_PCL = IDC_PSE_PCL;
 this.IDC_CD_NOT = IDC_CD_NOT;
 this.DA_PRM_CTR_ORDER = DA_PRM_CTR_ORDER;
 this.IDT_ETT_PSE = IDT_ETT_PSE;
 this.CD_NOT = CD_NOT;
 }
 public String getIDC_CD_NOT() {
 return this.IDC_CD_NOT;
 }
 public String getIDC_PSE_PCL() {
 return this.IDC_PSE_PCL;
 }
 public String getDA_PRM_CTR_ORDER() {
 return this.DA_PRM_CTR_ORDER;
 }
 public String getIDT_ETT_PSE() {
 return this.IDT_ETT_PSE;
 }
 public String getCD_NOT() {
 return this.CD_NOT;
 }
 public String getIDT_GCT() {
 return this.IDT_GCT;
 }
}

So I need to get distinct values of IDT_GCT variable.

So I created this function to return this :

private static List DistinctValues (ArrayList<Personne> listPersonnes)
{
 List <String> outList = listPersonnes.stream().map(m -> m.get("IDT_GCT")).distinct().collect(Collectors.toList());
 return(outList);
}

But this returns an error :

Multiple markers at this line
 - Cannot infer type argument(s) for <R> map(Function<? super T,? extends R>)
 - The method get(String) is undefined for the type Personne
 - The type List is not generic; it cannot be parameterized with arguments <String>

How can I resolve it please ?

Pavel Smirnov
4,8293 gold badges22 silver badges32 bronze badges
asked Jul 24, 2019 at 20:32
1
  • 1
    Override equals and hashcode for the class Personne and use HashSet instead of ArrayList. Commented Jul 24, 2019 at 20:34

2 Answers 2

1

The method get(String) is undefined for the type Personne

you should use getIDT_GCT() instead

answered Jul 24, 2019 at 21:21
Sign up to request clarification or add additional context in comments.

1 Comment

This is the answer.
0

private List DistinctValues(ArrayList listPersonnes) { Set uniqueColumnValues = new HashSet <>(); // hash set contains unique values listPersonnes.forEach(item -> uniqueColumnValues.add(item.getIDT_GCT())); // add values to HashSet return uniqueColumnValues.stream().collect(Collectors.toList()); }

answered Jul 24, 2019 at 21:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.