简体   繁体   中英

How to print first element in ArrayList to HashSet in Java

I was wondering what is the easiest way to print out the first element in every row of an ArrayList . In my case, I would want to print the animal First name Only.

Example:

FirstName = Sam, lastName = brown, Tag = 5
FirstName = lucky, lastName = brown, Tag = 4
FirstName = Rocky, lastName = brown, Tag = 3
FirstName = Spike, lastName = brown, Tag = 1

I want to specifically make a HashSet out of the firstNames only. My ArrayList was generated by using a DataInputStream

Expected Output

FirstName = Sam,    
FirstName = lucky
FirstName = Rocky
FirstName = Spike

Sample Code: Animal Class

public class Animal implements Serializable
{
    public String firstName;
    public String lastName;
    public int tag Number;

     // 0 Argument Constructor 
     public Animal()
     {
         firstName = "";
         lastName = "";
         tagNumber = 0;
     }    

    // following that is a 3 argument constructor, then getters and a
    // toString. The toString and getters can't be edited.
}

I want to specifically make a HashSet out of the firstNames only

You could just iterate over your list and use the getter for the firstname and pass is to the HashSet 's add method.

Set<String> animalFirstnameSet = new HashSet();
for(Animal animal : animalList) {
    animalFirstnameSet.add(animal.getFirstname());
}

Note however, that subsequent equal firstnames will override the previous ones. for example if you have

FirstName = Sam, lastName = blue, Tag = 5

at the end of your list, it will override

FirstName = Sam, lastName = brown, Tag = 5

so you will end up a HashSet the size listsize - 1 .

If you want to prevent that, you may want to override hashcode() and equals() of your class Animal and add those instances to a Set<Animal> .

If you want to have a mapping by firstname and keep all Animals even though they have the same name then you may want to use a HashMap instead and have as value a List of Animal instances.

Use from custom objects and override its toString() method to return only last names. Here is the steps:

1. Create a custom object class and override its toString() method

public class ComplexObject
{
    private String firstName;
    private int tag;
    private String lastName;




    public ComplexObject(String firstName, int tag, String lastName)
    {
        this.firstName = firstName;
        this.tag = tag;
        this.lastName = lastName;

    }

    //Be sure to override the toString() like this
    public String toString()
    {
        return  this.firstName;
    }
}

2. Add an ArrayList of that object, here is a tested program

public class TestClass
{
    public static void main(String[] args)
    {
        ArrayList<ComplexObject> arr = new ArrayList<ComplexObject>();



        arr.add(new ComplexObject("Elham", 1, "Kohestani"));
        arr.add(new ComplexObject("Tom", 2, "Alexi"));
        arr.add(new ComplexObject("Max", 3, "Sharon"));



        for(int i =0; i<arr.size(); i++)
        {
            System.out.println(arr.get(i));
        }
    }
}

It gives you the first names Elham, Tom and Max as a result.
You can simply make a HashSet of first names inside the for loop as follow:

HashSet<ComplexObject> x = new HashSet<>();

        for(int i =0; i<arr.size(); i++)
        {
            x.add(arr.get(i));
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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